4 ms·
You can pass methods as functions or closures by Type::method. The first argument will be the self argument. I don't see much of an alternative to panic on bou
by kzrdude 11y ago
You can pass methods as functions or closures by Type::method. The first argument will be the self argument.
I don't see much of an alternative to panic on bounds checked indexing error, short of not providing indexing at all or developing a system of proven indices that would be extremely unfriendly to newcomers, see this project: https://github.com/bluss/indexing https://github.com/bluss/indexing
- EugeneOZ 11y agoNo, you can't do it when you need &self.
- kzrdude 11y agoYou have me guessing you want a bound method where self is implicitly supplied. Sure, there's no sugar for that.
- dbaupp 11y agoYou can, e.g. struct Foo; impl Foo { fn bar(&self) {} } fn main() { let x = Foo; Foo::bar(&x); } What you may be encountering is you can't pass a `Foo` when a `&Foo` is expected (why I needed to write &x), because the types differ. Calls with method syntax don't see this because they automatically borrow the receiver if necessary, but explicit function calls do not do this sort of borrowing and so the types need to line up right.
- masklinn 11y ago> I don't see much of an alternative to panic on bounds checked indexing error Returning an Option. That's what Elm does: head : List a -> Maybe a get : Int -> Array a -> Maybe a It tends to drive people towards not indexing collection (which is often good).
- killercup 11y agoRust's std::slice has a `get` method that does exactly that: http://doc.rust-lang.org/std/primitive.slice.html#method.get http://doc.rust-lang.org/std/primitive.slice.html#method.get
- Manishearth 11y agoIdiomatically you should be using iterators or monadic indexing (i.e. the methods which return an Option). Regular indexing is exposed for convenience, but I don't think it should be used as often. Perhaps we should have a clippy lint for this.
- kzrdude 11y agoLol, you must live in a different world, lots of code is using regular bounds checked indexing and it is completely idiomatic. So idiomatic in fact that there is no nonpanicking slicing method for slices or strings, for example.
- Manishearth 11y agoYou don't need one, since you can use `.chars().nth()` (indexing a string needs you to iterate over it anyway, because utf8). Though I do feel that having a direct get() API would be nice I have seen quite a bit of regular `[]` indexing, however it's almost always in very obvious situations, and most of the places where you might index in C/++ (iteration, or with a bounds check) I've seen people using iterators or the monadic way. But yeah, it's not that unidiomatic. It's still used a lot, just not to the extent it's used elsewhere. (For example, we have thrice as much indexing in our DOM binding generation python code (`components/script/dom/bindings/codegen`) in Servo than we do in all of the non-test rust files (`components/`) in the main repo combined)
- Gankro 11y agoDon't. This would be a terrible lint. It's perfectly reasonable to expect an index to be in bounds for various reasons.
- mcguire 11y agoDependent typing, dude. It's the way of the future.
- Manishearth 11y agoIt could be a pedantic lint (off by default). There may be ways to lint in case of an `if x.len()... x[...]` though. That should be done monadically; given that panics aren't supposed to be caught in most situations, we should minimize the number of ways programmer error can lead to a panic. Looking through servo's code a lot of the cases are where we use fixed-length arrays, or when dealing with graphicsy buffers. One use case which needs indexing is when we use an index as an "id". No easy way to fix that without introducing Rc or something.
- kzrdude 11y agoThat alternative is already present, yet panicking indexing is listed as a drawback. It remains the alternative to not provide indexing then.
- deleted 11y ago[deleted]