6 ms·
> There the borrow makes more sense since the value is being created by calling a function on the current solver state. Agreed. And yes, a Cow is better in th
by Manishearth 11y ago
> There the borrow makes more sense since the value is being created by calling a function on the current solver state.
Agreed. And yes, a Cow is better in this case though of course there's a slight runtime cost. In such a case I would heavily document the new invariants with warning comments though :P
> I linked to an active rfc about constraints that cannot currently be expressed in where clauses
Those are the constraints that cannot currently be expressed in bounds, not where clauses. Bounds are the stuff within <>, where clauses were the new thing proposed to supplement them.
> In cases where a function takes two arguments it isn't always obvious which argument should be self.
I'm not sure why you seem to be troubled by self (here and in the post). It might be due to you looking at method dispatch as a sugar for function calls -- it sort of is (because of UFCS), but it really isn't.
The second argument can never be self. It's always the first. (nitpick: functions can't have self args, methods can, but that's just terminology).
For direct impls, you do a method without `self` when you wish it to be a static method. Eg `impl Foo {fn x(){}}` will be a method called as `Foo::x()` and will work independently of any instance of the type. `impl Foo{fn x(self){}}` needs to be called as `foo.x()`, where `foo` is a `Foo`. Here, the method is able to access the state of `foo`.
The behavior so far is the same for most languages like python or java (java has implicit self and uses `static` to say "no self").
Now, traits just let you provide a way to classify objects based on what methods -- both self and nonself/static -- they have. So, a trait `Cloneable` would be `trait Cloneable { fn clone(&self) -> Self}`, and its implementation would be `impl Cloneable for Foo {fn clone(&self) -> Foo {....}}`. This would mean that "given a `Foo`, I should be able to get another `Foo` out of it by calling `foo.clone()`". On the other hand, sometimes you want to run an operation on the type itself. I.e. `trait TypeName {fn type_name() -> &'static str}` and is implemented as `impl TypeName for Foo {fn type_name() -> &'static str {"Foo"}}`. In this case, it's no longer "given a `Foo`, do X", it's "given a type that implements TypeName, do X".
Generally nonself methods on traits don't make much sense unless you're doing type gymnastics (in fact, Java doesn't even allow static methods in interfaces). For example, they're used in hyper for dynamic dispatch on strongly typed headers (http://hyper.rs/hyper/hyper/header/trait.Header.html http://hyper.rs/hyper/hyper/header/trait.Header.html)
Also, `where Foo<A,B>` doesn't make sense, where clauses need a colon; `where Something: SomethingElse`.
- jamii 11y ago> Those are the constraints that cannot currently be expressed in bounds, not where clauses. Yeah, I got that whole thing totally wrong. I've removed that part of the post and linked to this discussion instead. Thankyou for de-confusing me :) > The second argument can never be self. It's always the first. I think we are talking past each other on this point. I'm thinking of the design-time choice between eg: trait Observe<Observee> { fn observe(self, observee: Observee); } trait Observe<Observer> { fn observe(self, observer: Observer); } trait Observe<Observer, Observee> { fn observe(observer: Observer, observee: Observee); } Analogous to the typical OO problem where it's not clear which class a method should belong too. Because I mistakenly believed that where clauses are less powerful I thought that the above choice had additional significance beyond code organisation, because it would affect the kind of constraints I could write. But they aren't so it doesn't matter :)
- Manishearth 11y agoOh, I get it now. Yeah, any of these choices will work^. Well, the last choice shouldn't be a trait, really, just a standalone function. Associated types would also help simplify this (note, if you have a trait Foo<A>, the trait can be implemented multiple times on the same type, with different A. If you have a trait Foo with associated type A, only one implementation will be allowed, the associated type is a property of the implementation) ^ In more complicated situations coherence may disallow one or more of those choices.
- jamii 11y ago> the trait can be implemented multiple times on the same type, with different A That was the intention - a function that dispatches on the type of both arguments (it probably shows that I secretly think of traits as typeclasses). I'm still trying to figure out what the implications of the above choices are. > In more complicated situations coherence may disallow one or more of those choices. I hadn't thought of that. If the trait is in another crate, does coherence require that the self type is in this crate or that all the types are in this crate? How do the coherence rules work if I don't have self type?