D idioms list
ketmar via Digitalmars-d-announce
digitalmars-d-announce at puremagic.com
Thu Jan 8 21:52:37 PST 2015
On Thu, 08 Jan 2015 22:25:11 +0100
Artur Skawina via Digitalmars-d-announce
<digitalmars-d-announce at puremagic.com> wrote:
> On 01/08/15 21:23, ketmar via Digitalmars-d-announce wrote:
> > i'm not sure, but maybe it worth renaming "struct inheritance" to
> > "extending a struct"? or even something completely different. what it
> > does is actually extending/augmenting the struct, but not
> > OO-inheritance, as one cannot pass "augmented" struct to the function
> > which expects original struct. at least without hackery.
>
> 'alias this' is just the D syntax for implicit conversions.
> The feature /is/ crippled, but there's no need for "hackery";
> at least not for simple things like that.
>
> struct A { int a; }
> struct B { A a; alias a this; string b; }
>
> int f(A a) { return a.a+1; }
> int g(ref A a) { return a.a+1; }
> ref A h(ref A a) { return a; }
>
> int main() {
> B b;
> return f(b)+g(b)+h(b).a;
> }
>
> artur
mea culpa. i completely forgot about that feature of `alias this`, and
was pretty sure that the code above is invalid. i never bothered to
really check it. sorry.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-announce/attachments/20150109/b14a9626/attachment.sig>
More information about the Digitalmars-d-announce
mailing list