Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

"The strange alias _curr this is a lovely feature of D known as subtyping. It basically means that any property that doesn't exist at the struct's scope will we forwarded to _curr, e.g., when I write myCtx.foo and myCtx has no member named foo, the code is rewritten as myCtx._curr.foo."

That's a great feature. I don't see many languages investing enough focus into this kind of "delegation wiring."



Yes. It makes it easy to do things like, for example, create your own "int" type without having to duplicate all the behaviors of int. Just override the behaviors you'd like to change, then forward the rest to the wrapped int field.


I love this feature! It's also recursive, you can have the following:

struct A { int a; alias a this; } struct B { A a; alias a this; } struct C { B b; alias b this; }

static assert(C(42) == 42);

Combined with D's support for compile-time protocols, this allows for some very expressive, powerful and lighting fast code.


If I understand correctly you can achieve something similar in many other languages by implementing a dereference operator. You'll have to be explicit when you use the object though (o.foo vs. o->foo for instance). I tend to prefer these kinds of explicit constructs over compiler magic, but it's a matter of taste really.


The built-in version has the big advantage that it integrates well with tools: you can statically determine the target of the reference and jump to it.


Go has something similar, but it's implicit with type embedding, you don't have to explicitly alias anything; also the Plan 9 C dialect has it too (and was used extensively in the Go runtime until recently). One nice idiom in Plan 9 is to be able to call Lock on various structures that embed a lock.


Can you give a code example in Go?


Quick example on the playground, if this is what you were asking about: http://play.golang.org/p/zShJKp0t3n


So subtyping automatically happens for all members of a struct? I'm not sure I like that but I guess I'd have to see how well it works in practice.


No, it only happens if you embed the type, a.i. the member is anonymous.

http://golang.org/doc/effective_go.html#embedding

http://golang.org/ref/spec#Struct_types

Note that there's also interface embedding which is a different thing.


Ah, that makes a lot more sense. Thank you for explaining.



Interesting but this is not the same feature. Go implements what seems like multiple inheritance, D simply dispatch calls to a member, which can be a pointer to T instead of T. So you can implement custom pointer types.


I have provided a better example: http://play.golang.org/p/yYg8okmN6a

Yes, it's a different feature, but is not multiple inheritance either (although I understand why, at a first glance, you'd think it would seems like that). It's just composition. In this new example, look how you can do b.myInt, even though b is now a bar, not a foo. In fact this happened in the old example too, but it was invisible. This is how the Lock function accessed the mutex.


Lua metatables do that.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: