Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
January 23, 2021 Deduct and return class type | ||||
---|---|---|---|---|
| ||||
Context: data + GUI List Goal: auto list = new List( data ); Concept: class is created in the usual way : new List( data ) store inside the class : T data; type T deducted : ( T )( T data ) Tried way: template List( T ) { class List { T data; this( T data ) { this.data = data; } // data usage... } } void main() { string[] extensions = [ ".d", ".di" ]; auto list = new List( extensions ); } Source: https://run.dlang.io/is/Bw2zHB Question: How to implement on D beauty clean flexible code ? like a: auto list = new List( data ); How to return from 'List( data )' class type ? |
January 23, 2021 Re: Deduct and return class type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Виталий Фадеев | On Saturday, 23 January 2021 at 05:39:18 UTC, Виталий Фадеев wrote: > Context: > data + GUI List > > Goal: > auto list = new List( data ); Of course, we can use 'factory': import std.stdio; template List( alias T ) { class List { T data; this( T data ) { this.data = data; } // data usage... } } auto listFactory( T )( T data ) { return new List!( T )( data ); } void main() { string[] extensions = [ ".d", ".di" ]; auto list = listFactory( extensions ); //auto list = new List( extensions ); } source: https://run.dlang.io/is/y167tu But, how to create class instance with type deduction in usual way ? auto list = new List( extensions ); |
January 23, 2021 Re: Deduct and return class type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Виталий Фадеев | On Saturday, 23 January 2021 at 05:54:09 UTC, Виталий Фадеев wrote:
> But, how to create class instance with type deduction in usual way ?
>
> auto list = new List( extensions );
You can't; there's no type deduction for constructors.
|
January 23, 2021 Re: Deduct and return class type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Виталий Фадеев | On Saturday, 23 January 2021 at 05:54:09 UTC, Виталий Фадеев wrote:
> auto listFactory( T )( T data )
> {
> return new List!( T )( data );
> }
This is the correct way to do it in D; it's annoying, I know. (Personally, I usually shorten `listFactory` to just `list` to make the name as similar as possible to the type name, without actually causing a collision.)
|
January 23, 2021 Re: Deduct and return class type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Виталий Фадеев | On Saturday, 23 January 2021 at 05:54:09 UTC, Виталий Фадеев wrote:
> On Saturday, 23 January 2021 at 05:39:18 UTC, Виталий Фадеев wrote:
>> Context:
>> data + GUI List
>>
>> Goal:
>> auto list = new List( data );
I want 'this( T )( T data )' deduction:
class A( T )
{
this( T )( T data )
{
// ...
}
}
What way to implement this ?
Rules:
1. if class is template
2. if ctor is template
3. if ctor template arg have same name with class template arg name (example: T )
4. deduct T of ctor
5. set type T for class template ( or put in suggestion queue )
Verify, please.
Where source ?
Where deduction implementation ?
|
January 23, 2021 Re: Deduct and return class type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Виталий Фадеев | On Saturday, 23 January 2021 at 07:17:38 UTC, Виталий Фадеев wrote: > Where source ? > Where deduction implementation ? Here: https://github.com/dlang/dmd/blob/v2.095.0/src/dmd/dtemplate.d#L1308 |
January 24, 2021 Re: Deduct and return class type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | On Saturday, 23 January 2021 at 19:19:29 UTC, Paul Backus wrote:
> On Saturday, 23 January 2021 at 07:17:38 UTC, Виталий Фадеев wrote:
>> Where source ?
>> Where deduction implementation ?
>
> Here:
>
> https://github.com/dlang/dmd/blob/v2.095.0/src/dmd/dtemplate.d#L1308
Thank!
|
Copyright © 1999-2021 by the D Language Foundation