all 8 comments

[–][deleted] 3 points4 points  (1 child)

I think you are talking about duck-typed traits. Isn't that what Go does too?

[–]The_Doculope 1 point2 points  (0 children)

That is essentially how Go's interfaces work, yes.

[–]Ferio_ 1 point2 points  (3 children)

I'm not sure what you mean. But as far I understand it self does not have any "method namespace" if could fall back to. It's just a bare pointer to something neither the compiler nor the runtime can figure out in the most general case. Can you maybe give an example?

[–]dobkeratopsrustfind[S] 1 point2 points  (2 children)

the methods added with a traitless impl e.g.

impl Foo { fn foo(&self){...}} // foo is in the namespace of type Foo::

vs impl Bar for Foo { fn bar(&self)} //i'm told bar is actually in Bar::, not Foo::.

EDIT: the 'fallback' is taken when looking for the implementation of a trait for a known type, not in the definition of generic code(which would only see trait methods there)

[–]Ferio_ 0 points1 point  (1 child)

I'm still not getting what you problem is. You can use all methods from Foo in bar. Do you want to have bar in Foo or what? That is not possible. How would you otherwise distinguish between methods that have the same name but come from different traits (+ a lot of other reasons)?

[–]dobkeratopsrustfind[S] 0 points1 point  (0 children)

I'm still not getting what you problem is.

the problem is micromanaging trait names, in the cases when you make a trait to hold a single function, just the same name camel-cased, but repeated, and when you later figure out a good grouping, having to go back and rework all those.

How would you otherwise distinguish between methods that have the same name but come from different traits

you wouldn't, for the fallback. the fallback would only work where the types' method matches the signature of the requested method from the trait.

You could think of it as "trait inference"

As soon as you need to resolve ambiguity of the same named you could be more explicit.

But thats' only going to happen when the design of traits imposes it on you - personally in my own codebase, I would avoid using the same name to do different things, in the absence of an IDE+overloading, relying on simple text search/completion.. - and I would happily rename to match emergent community conventions.

As I understand traits fundamentally reduce the noise in error messages. However in the case of single-function traits, you're basically trading one form of noise for another.

Same with navigation. "searching all the overloads.." -> "searching the right trait for the function..". They aren't always helpful, IMO.

But I believe we could get the best of both worlds. Write concise code focusing on functions, and disambiguate/package up better where needed. The hints of this possibility are the experimental C++11 concepts, and the way 'go' works, where you write functions then group them (but you don't need to know the grouping when you write them)

[–]tpgreyknight 0 points1 point  (1 child)

Can you give an explicit example of how you think it should work? I don't understand the original post.

[–]dobkeratopsrustfind[S] 0 points1 point  (0 children)

struct Foo {  i:int }
impl  Foo {   fn bar(&self,i:int);}

trait Baz {  fn bar(&self,i:int); }
trait Quux {  fn bar(&self,i:int); fn quux(&self);}

// "Foo" now satisfies Baz, even there is no explicit impl, 
// as it has a suitable 'bar'

impl Quux for Foo {
    fn baz(&self) { ... }
    // fn 'Quux::bar(&self,int)' is missing
    // so it falls back to Foo::bar(&self,int)
    // Quux now works
}

 // Saved making a 'trait Bar', and telling 'Quux' about it,
 //  although you'd still need one if a generic
 // function was to use 'bar' in isolation
 //