www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - Multiple alias this is coming.

reply "IgorStepanov" <wazar mail.ru> writes:
I've created pull request, which introduces multiple alias this.
https://github.com/D-Programming-Language/dmd/pull/3998
Please see the additional tests and comment it.
Sep 18 2014
next sibling parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
On 18/09/2014 11:20 p.m., IgorStepanov wrote:
 I've created pull request, which introduces multiple alias this.
 https://github.com/D-Programming-Language/dmd/pull/3998
 Please see the additional tests and comment it.
Awesome was waiting for something like this! Also did we ever consider alias this = ...; syntax?
Sep 18 2014
parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Thursday, 18 September 2014 at 12:51:48 UTC, Rikki Cattermole 
wrote:
 On 18/09/2014 11:20 p.m., IgorStepanov wrote:
 I've created pull request, which introduces multiple alias 
 this.
 https://github.com/D-Programming-Language/dmd/pull/3998
 Please see the additional tests and comment it.
Awesome was waiting for something like this! Also did we ever consider alias this = ...; syntax?
It clashes with (not yet supported) aliasing of constructors.
Sep 18 2014
parent reply "ponce" <contact gam3sfrommars.fr> writes:
On Thursday, 18 September 2014 at 13:44:10 UTC, Marc Schütz wrote:
 On Thursday, 18 September 2014 at 12:51:48 UTC, Rikki 
 Cattermole wrote:
 On 18/09/2014 11:20 p.m., IgorStepanov wrote:
 I've created pull request, which introduces multiple alias 
 this.
 https://github.com/D-Programming-Language/dmd/pull/3998
 Please see the additional tests and comment it.
Awesome was waiting for something like this! Also did we ever consider alias this = ...; syntax?
It clashes with (not yet supported) aliasing of constructors.
Call me unimaginative, but I'm struggling to see any use case for multiple alias this, and I struggle even more for such constructors aliasing.
Sep 19 2014
parent reply "Dicebot" <public dicebot.lv> writes:
On Friday, 19 September 2014 at 09:34:22 UTC, ponce wrote:
 Call me unimaginative, but I'm struggling to see any use case 
 for multiple alias this, and I struggle even more for such 
 constructors aliasing.
Pretty much every single time you have ever wanted to do multiple inheritance of implementation multiple `alias this` was the proper tool for a job. I notice myself thinking "this could have been done much cleaner with multiple alias this" at least once a month :)
Sep 19 2014
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 9/19/14, 3:21 AM, Dicebot wrote:
 On Friday, 19 September 2014 at 09:34:22 UTC, ponce wrote:
 Call me unimaginative, but I'm struggling to see any use case for
 multiple alias this, and I struggle even more for such constructors
 aliasing.
Pretty much every single time you have ever wanted to do multiple inheritance of implementation multiple `alias this` was the proper tool for a job. I notice myself thinking "this could have been done much cleaner with multiple alias this" at least once a month :)
Yah, multiple subtyping of structs is terrific to have. Thanks Igor for this work! -- Andrei
Sep 19 2014
parent reply "IgorStepanov" <wazar mail.ru> writes:
On Friday, 19 September 2014 at 17:19:09 UTC, Andrei Alexandrescu
wrote:
 On 9/19/14, 3:21 AM, Dicebot wrote:
 On Friday, 19 September 2014 at 09:34:22 UTC, ponce wrote:
 Call me unimaginative, but I'm struggling to see any use case 
 for
 multiple alias this, and I struggle even more for such 
 constructors
 aliasing.
Pretty much every single time you have ever wanted to do multiple inheritance of implementation multiple `alias this` was the proper tool for a job. I notice myself thinking "this could have been done much cleaner with multiple alias this" at least once a month :)
Yah, multiple subtyping of structs is terrific to have. Thanks Igor for this work! -- Andrei
You are welcome :) BTW. Please comment (approve or correct) semantic rules, which used for conflict resolving. I've written it early and reflected it in the PR tests.
Sep 19 2014
parent "IgorStepanov" <wazar mail.ru> writes:
On Friday, 19 September 2014 at 18:46:14 UTC, IgorStepanov wrote:
 On Friday, 19 September 2014 at 17:19:09 UTC, Andrei 
 Alexandrescu
 wrote:
 On 9/19/14, 3:21 AM, Dicebot wrote:
 On Friday, 19 September 2014 at 09:34:22 UTC, ponce wrote:
 Call me unimaginative, but I'm struggling to see any use 
 case for
 multiple alias this, and I struggle even more for such 
 constructors
 aliasing.
Pretty much every single time you have ever wanted to do multiple inheritance of implementation multiple `alias this` was the proper tool for a job. I notice myself thinking "this could have been done much cleaner with multiple alias this" at least once a month :)
Yah, multiple subtyping of structs is terrific to have. Thanks Igor for this work! -- Andrei
You are welcome :) BTW. Please comment (approve or correct) semantic rules, which used for conflict resolving. I've written it early and reflected it in the PR tests.
Further, could this also be used to somehow simplify
hierarchically defined enumerators? Typically the enumerators and predicates related to the enumeration WordKind defined here enum can have a class as base type: import std.stdio; class Word { this(string type) { this.type = type; } property abstract string wordClass(); override size_t toHash() trusted { string s = toString(); return typeid(string).getHash(&s); } override string toString() trusted nothrow { try { return wordClass() ~ ":" ~ type; } catch(Throwable th) { assert(0); } } string type; } class Noun : Word { this (string type) { super(type); } property override string wordClass() { return "noun"; } } class Verb : Word { this (string type) { super(type); } property override string wordClass() { return "verb"; } } enum WordKind : Word { Unknown = null, Numeric = new Noun("Numeric"), //... Present = new Verb("Present"), } import std.stdio; void main() { string[][WordKind] dictionary; dictionary[WordKind.Numeric] ~= "one"; dictionary[WordKind.Numeric] ~= "two"; dictionary[WordKind.Present] ~= "go"; writeln(dictionary); } Does this code can help you?
Sep 19 2014
prev sibling next sibling parent Bruno Medeiros <bruno.do.medeiros+dng gmail.com> writes:
On 18/09/2014 12:20, IgorStepanov wrote:
 I've created pull request, which introduces multiple alias this.
 https://github.com/D-Programming-Language/dmd/pull/3998
 Please see the additional tests and comment it.
http://cdn.meme.li/instances/500x/54451116.jpg In before the rest... -- Bruno Medeiros https://twitter.com/brunodomedeiros
Sep 18 2014
prev sibling next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 09/18/2014 04:20 AM, IgorStepanov wrote:
 I've created pull request, which introduces multiple alias this.
 https://github.com/D-Programming-Language/dmd/pull/3998
 Please see the additional tests and comment it.
Awesome! This is the feature that I thought would never get implemented. :) Ali
Sep 18 2014
parent "Foo" <Foo test.de> writes:
On Thursday, 18 September 2014 at 18:38:57 UTC, Ali Çehreli wrote:
 On 09/18/2014 04:20 AM, IgorStepanov wrote:
 I've created pull request, which introduces multiple alias 
 this.
 https://github.com/D-Programming-Language/dmd/pull/3998
 Please see the additional tests and comment it.
Awesome! This is the feature that I thought would never get implemented. :) Ali
No, these are rvalue references. ;)
Sep 18 2014
prev sibling next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
IgorStepanov:
 I've created pull request, which introduces multiple alias this.
Can someone show one or more usage cases? Thank you, bye, bearophile
Sep 18 2014
next sibling parent reply "IgorStepanov" <wazar mail.ru> writes:
On Thursday, 18 September 2014 at 19:52:42 UTC, bearophile wrote:
 IgorStepanov:
 I've created pull request, which introduces multiple alias 
 this.
Can someone show one or more usage cases? Thank you, bye, bearophile
Do you ask about alias this or about it multiple usage. Multiple usage is similar to single, but multiple:) struct Foo { string s; int i; alias s this; alias i this; } Foo f = {"foo", 42}; string s = f; //s == "foo" int i = f; //i == 42 If there are many different ways to resolve alias this then error is raised: struct Bar { double d; int i; alias d this; alias i this; } Foo f = {1.0, 42}; double d = f; //Error: compiler doesn't know, f.d or f.i do you want. In the next expamle, compiler can resolve conflict: struct Base1 { int i; alias i this; } struct Base2 { int i; alias i this; } struct Derived { Base1 b1; Base2 b2; int i; alias b1 this; alias b2 this; alias i this; } Derived d = Derived(Base1(1), Base2(2), 3); int i = d; //i == 3; This done because Derived author know, how to cast his struct to int, and if he say alias i this; this alias hide aliases in base types. However, if base type contains alias this to another acceptable type, and derived typ hasn't exactly castable alias this then error will be raised: struct Base1 { short s; alias s this; } struct Base2 { int i; alias i this; } struct Derived { Base1 b1; Base2 b2; int i; alias b1 this; alias b2 this; alias i this; } Derived d = Derived(Base1(1), Base2(2), 3); int i = d; //Ok i == 3; long l = d; //Error: what do you want? d.i or d.b1.s? For additional info you can see examples in my pull request.
Sep 18 2014
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
IgorStepanov:

 Do you ask about alias this or about it multiple usage. 
 Multiple usage is similar to single, but multiple:)
I meant the multiple usage. And none of your examples here are use cases :-( I'd like to see one use case, or more. Bye, bearophile
Sep 18 2014
next sibling parent "IgorStepanov" <wazar mail.ru> writes:
On Thursday, 18 September 2014 at 22:16:23 UTC, bearophile wrote:
 IgorStepanov:

 Do you ask about alias this or about it multiple usage. 
 Multiple usage is similar to single, but multiple:)
I meant the multiple usage. And none of your examples here are use cases :-( I'd like to see one use case, or more. Bye, bearophile
For example, you can define struct (value-type with automatic storage class) which implements interfaces: intarface InputStream { ... } intarface OutputStream { ... } struct File { this(string name, string options) { impl = new FileImpl(name, options); } ~this() { impl.close(); } property InputStream inputStream() { return new class(impl) InputStream { //InputStream implementation ... } } property OutputStream outputStream() { return new class(impl) OutputStream { //OutputStream implementation ... } } alias inputStream this; alias seekable this; FileImpl* impl; } Record[] readData(InputStream); void storeData(OutputStream, Record[]); Record[] extractRecords() { File f = File("records.bin", "rw"); auto data = readData(f); ///split to extracted and remaining f.truncate(); writeData(f, remaining); return extracted; }
Sep 18 2014
prev sibling parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
19-Sep-2014 02:16, bearophile пишет:
 IgorStepanov:

 Do you ask about alias this or about it multiple usage. Multiple usage
 is similar to single, but multiple:)
I meant the multiple usage. And none of your examples here are use cases :-( I'd like to see one use case, or more.
For instance one could implement OOP and polymorphism on structs, all in library code. struct Base { .... } struct Itnerface { .... } struct Derived{ Base _super; Interface _iface; alias this _super; alias this _iface; } I recall trying something like that but stopping because I needed multiple alias this. -- Dmitry Olshansky
Sep 19 2014
prev sibling parent reply "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Thursday, 18 September 2014 at 20:11:12 UTC, IgorStepanov 
wrote:
 Do you ask about alias this or about it multiple usage. 
 Multiple usage is similar to single, but multiple:)
Just an FYI, bearophile is very knowledgeable about D and one of the oldest community members, he holds the record for most bugs opened. This doesn't mean he knows everything, but it certainly makes it clear he was asking, "why would you want to use multiple alias this." As others mention kind of like "why would you want to use multiple inheritance."
Sep 19 2014
parent "IgorStepanov" <wazar mail.ru> writes:
On Friday, 19 September 2014 at 18:49:57 UTC, Jesse Phillips 
wrote:
 On Thursday, 18 September 2014 at 20:11:12 UTC, IgorStepanov 
 wrote:
 Do you ask about alias this or about it multiple usage. 
 Multiple usage is similar to single, but multiple:)
Just an FYI, bearophile is very knowledgeable about D and one of the oldest community members, he holds the record for most bugs opened. This doesn't mean he knows everything, but it certainly makes it clear he was asking, "why would you want to use multiple alias this." As others mention kind of like "why would you want to use multiple inheritance."
Sorry, I'll note it.:)
why would you want to use multiple inheritance.
I've written example with interface inherinatce. Structs can be preferable then classes, when I want to manage them allocating manually. When you use D as "better C", powerfull struct's can be a you choise. Multiple alias this (and therefore multiply inheritance) makes structs almost as powerful as classes. Another my motivation of doing this work is a implementing alias this inheritance (from base class). Situation when B can be casted to C and D, A can be casted to B and C but cannot be casted to D is wrong: class B : C { alias d this; D d; } class A : B { } A a = new A; D d = a; //NG -> OK
Sep 19 2014
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 09/18/2014 12:52 PM, bearophile wrote:

 Can someone show one or more usage cases?
I can't claim that I have experience with multiple alias this ;) but I like the following example that I had come up with: class TeachingAssistant { Student studentIdentity; Teacher teacherIdentity; this(string name, string subject) { this.studentIdentity = new Student(name); this.teacherIdentity = new Teacher(name, subject); } /* The following two 'alias this' declarations will enable * this type to be used both as a Student and as a Teacher. */ alias teacherIdentity this; alias studentIdentity this; } Ali [1] http://ddili.org/ders/d.en/alias_this.html
Sep 18 2014
parent ketmar via Digitalmars-d-announce <digitalmars-d-announce puremagic.com> writes:
On Thu, 18 Sep 2014 15:20:04 -0700
Ali =C3=87ehreli via Digitalmars-d-announce
<digitalmars-d-announce puremagic.com> wrote:

 [1] http://ddili.org/ders/d.en/alias_this.html
heh, i just wanted to point at your book. ;-)
Sep 18 2014
prev sibling next sibling parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Thursday, 18 September 2014 at 11:20:49 UTC, IgorStepanov 
wrote:
 I've created pull request, which introduces multiple alias this.
 https://github.com/D-Programming-Language/dmd/pull/3998
 Please see the additional tests and comment it.
Exciting! What more good things than a better behaving Nullable(T) with T being a polymorphic class will this enable?
Sep 18 2014
parent reply "IgorStepanov" <wazar mail.ru> writes:
On Thursday, 18 September 2014 at 21:29:10 UTC, Nordlöw wrote:
 On Thursday, 18 September 2014 at 11:20:49 UTC, IgorStepanov 
 wrote:
 I've created pull request, which introduces multiple alias 
 this.
 https://github.com/D-Programming-Language/dmd/pull/3998
 Please see the additional tests and comment it.
Exciting! What more good things than a better behaving Nullable(T) with T being a polymorphic class will this enable?
Is Nullable!(T) with polymorphic type disallowed now? This PR also allows to use inherited alias thises: interface Intable { property int getInt(); } class Foo : Intable { int i; this (int i) { this.i = i; } override property int getInt() { return i; } } auto f = new Foo(42); int i = f; //i now 42 This option was disabled early.
Sep 18 2014
parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Thursday, 18 September 2014 at 22:13:14 UTC, IgorStepanov 
wrote:
 Is Nullable!(T) with polymorphic type disallowed now?
Sorry, I meant NotNull(T) Here's a module https://github.com/nordlow/justd/blob/master/notnull.d a bit tweak from the original design by Adam D Ruppe.
Sep 19 2014
parent reply "IgorStepanov" <wazar mail.ru> writes:
On Friday, 19 September 2014 at 11:25:03 UTC, Nordlöw wrote:
 On Thursday, 18 September 2014 at 22:13:14 UTC, IgorStepanov 
 wrote:
 Is Nullable!(T) with polymorphic type disallowed now?
Sorry, I meant NotNull(T) Here's a module https://github.com/nordlow/justd/blob/master/notnull.d a bit tweak from the original design by Adam D Ruppe.
What does a troubles with your NotNull implementation you have with old alias this? Do you want to implicit cast from NotNull!(T) to all other NotNull!(B) where B is basetype of T?
Sep 19 2014
next sibling parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Friday, 19 September 2014 at 13:24:53 UTC, IgorStepanov wrote:
 On Friday, 19 September 2014 at 11:25:03 UTC, Nordlöw wrote:
 On Thursday, 18 September 2014 at 22:13:14 UTC, IgorStepanov 
 wrote:
 Is Nullable!(T) with polymorphic type disallowed now?
Sorry, I meant NotNull(T) Here's a module https://github.com/nordlow/justd/blob/master/notnull.d a bit tweak from the original design by Adam D Ruppe.
What does a troubles with your NotNull implementation you have with old alias this? Do you want to implicit cast from NotNull!(T) to all other NotNull!(B) where B is basetype of T?
That makes me think of a question: Will the compiler instantiate a template member function, if it is specified as alias this? struct MyStruct { T convertTo(T)() if(...) { // implementation } alias convertTo this; } MyStruct s; int a = s; float b = s; string c = s; Will this work?
Sep 19 2014
parent "IgorStepanov" <wazar mail.ru> writes:
On Friday, 19 September 2014 at 14:23:55 UTC, Marc Schütz wrote:
 On Friday, 19 September 2014 at 13:24:53 UTC, IgorStepanov 
 wrote:
 On Friday, 19 September 2014 at 11:25:03 UTC, Nordlöw wrote:
 On Thursday, 18 September 2014 at 22:13:14 UTC, IgorStepanov 
 wrote:
 Is Nullable!(T) with polymorphic type disallowed now?
Sorry, I meant NotNull(T) Here's a module https://github.com/nordlow/justd/blob/master/notnull.d a bit tweak from the original design by Adam D Ruppe.
What does a troubles with your NotNull implementation you have with old alias this? Do you want to implicit cast from NotNull!(T) to all other NotNull!(B) where B is basetype of T?
That makes me think of a question: Will the compiler instantiate a template member function, if it is specified as alias this? struct MyStruct { T convertTo(T)() if(...) { // implementation } alias convertTo this; } MyStruct s; int a = s; float b = s; string c = s; Will this work?
No, this isn't work. You can start discussion about this feature and if it be approved, it can be implemented in future.
Sep 19 2014
prev sibling parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Friday, 19 September 2014 at 13:24:53 UTC, IgorStepanov wrote:
 What does a troubles with your NotNull implementation you have 
 with old alias this?
 Do you want to implicit cast from NotNull!(T) to all other 
 NotNull!(B) where B is basetype of T?
Yes, that is what I want, but last time I checked DMD didn't allow me to.
Sep 19 2014
parent "IgorStepanov" <wazar mail.ru> writes:
On Friday, 19 September 2014 at 17:59:44 UTC, Nordlöw wrote:
 On Friday, 19 September 2014 at 13:24:53 UTC, IgorStepanov 
 wrote:
 What does a troubles with your NotNull implementation you have 
 with old alias this?
 Do you want to implicit cast from NotNull!(T) to all other 
 NotNull!(B) where B is basetype of T?
Yes, that is what I want, but last time I checked DMD didn't allow me to.
This PR doesn't allow template alias this, however you can iterate all subtypes for you class T (D meta-programming features allow you to do it now) and mixin different alias this declaration for all of this types.
Sep 19 2014
prev sibling next sibling parent reply "deadalnix" <deadalnix gmail.com> writes:
On Thursday, 18 September 2014 at 11:20:49 UTC, IgorStepanov 
wrote:
 I've created pull request, which introduces multiple alias this.
 https://github.com/D-Programming-Language/dmd/pull/3998
 Please see the additional tests and comment it.
What is the policy to resolve conflict ? BTW, SDC already have multiple alias this :)
Sep 18 2014
parent "IgorStepanov" <wazar mail.ru> writes:
On Friday, 19 September 2014 at 05:52:53 UTC, deadalnix wrote:
 On Thursday, 18 September 2014 at 11:20:49 UTC, IgorStepanov 
 wrote:
 I've created pull request, which introduces multiple alias 
 this.
 https://github.com/D-Programming-Language/dmd/pull/3998
 Please see the additional tests and comment it.
What is the policy to resolve conflict ?
I wrote about that:
struct Foo
{
    string s;
    int i;

    alias s this;
    alias i this;
}

Foo f = {"foo", 42};
string s = f; //s == "foo"
int i = f; //i == 42

If there are many different ways to resolve alias this then 
error is raised:

struct Bar
{
    double d;
    int i;

    alias d this;
    alias i this;
}

Foo f = {1.0, 42};
double d = f; //Error: compiler doesn't know, f.d or f.i do you 
want.

In the next expamle, compiler can resolve conflict:

struct Base1
{
    int i;

    alias i this;
}

struct Base2
{
    int i;

    alias i this;
}

struct Derived
{
    Base1 b1;
    Base2 b2;
    int i;

    alias b1 this;
    alias b2 this;
    alias i this;
}

Derived d = Derived(Base1(1), Base2(2), 3);
int i = d; //i == 3;
This done because Derived author know, how to cast his struct to 
int, and if he say alias i this; this alias hide aliases in base 
types.

However, if base type contains alias this to another acceptable 
type, and derived typ hasn't exactly castable alias this then 
error will be raised:

struct Base1
{
    short s;

    alias s this;
}

struct Base2
{
    int i;

    alias i this;
}

struct Derived
{
    Base1 b1;
    Base2 b2;
    int i;

    alias b1 this;
    alias b2 this;
    alias i this;
}

Derived d = Derived(Base1(1), Base2(2), 3);
int i = d; //Ok i == 3;
long l = d; //Error: what do you want? d.i or d.b1.s?
Sep 19 2014
prev sibling next sibling parent "Dicebot" <public dicebot.lv> writes:
On Thursday, 18 September 2014 at 11:20:49 UTC, IgorStepanov 
wrote:
 I've created pull request, which introduces multiple alias this.
 https://github.com/D-Programming-Language/dmd/pull/3998
 Please see the additional tests and comment it.
It may help also providing side PR to dlang.org with relevant documentation adjustments (mentioning exact symbol resolution / disambiguation rules is especially important). Aside from that - very impressive work done! I totally love when suddenly implements long awaited features in covert ops mode :)
Sep 19 2014
prev sibling next sibling parent =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Thursday, 18 September 2014 at 11:20:49 UTC, IgorStepanov 
wrote:
 I've created pull request, which introduces multiple alias this.
 https://github.com/D-Programming-Language/dmd/pull/3998
 Please see the additional tests and comment it.
Further, could this also be used to somehow simplify hierarchically defined enumerators? Typically the enumerators and predicates related to the enumeration WordKind defined here https://github.com/nordlow/justd/blob/master/languages.d#L485
Sep 19 2014
prev sibling parent Michelle Long <HappyDance321 gmail.com> writes:
On Thursday, 18 September 2014 at 11:20:49 UTC, IgorStepanov 
wrote:
 I've created pull request, which introduces multiple alias this.
 https://github.com/D-Programming-Language/dmd/pull/3998
 Please see the additional tests and comment it.
Haha, that is funny!
Oct 25 2018