September 18, 2014 Re: Multiple alias this is coming. | ||||
---|---|---|---|---|
| ||||
Posted in reply to IgorStepanov | 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
|
September 18, 2014 Re: Multiple alias this is coming. | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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 |
September 18, 2014 Re: Multiple alias this is coming. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli Attachments: | On Thu, 18 Sep 2014 15:20:04 -0700 Ali Çehreli 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. ;-) |
September 18, 2014 Re: Multiple alias this is coming. | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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;
}
|
September 19, 2014 Re: Multiple alias this is coming. | ||||
---|---|---|---|---|
| ||||
Posted in reply to IgorStepanov | 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 :)
|
September 19, 2014 Re: Multiple alias this is coming. | ||||
---|---|---|---|---|
| ||||
Posted in reply to IgorStepanov | 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 :)
|
September 19, 2014 Re: Multiple alias this is coming. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | 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.
|
September 19, 2014 Re: Multiple alias this is coming. | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | 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? |
September 19, 2014 Re: Multiple alias this is coming. | ||||
---|---|---|---|---|
| ||||
Posted in reply to ponce | 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 :)
|
September 19, 2014 Re: Multiple alias this is coming. | ||||
---|---|---|---|---|
| ||||
Posted in reply to IgorStepanov | 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. |
Copyright © 1999-2021 by the D Language Foundation