digitalmars.D.learn - Deduct and return class type
- =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= (32/32) Jan 22 2021 Context:
- =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= (30/34) Jan 22 2021 Of course, we can use 'factory':
- Paul Backus (3/6) Jan 22 2021 You can't; there's no type deduction for constructors.
- tsbockman (6/10) Jan 22 2021 This is the correct way to do it in D; it's annoying, I know.
- =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= (21/28) Jan 22 2021 I want 'this( T )( T data )' deduction:
- Paul Backus (4/6) Jan 23 2021 Here:
- =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= (2/8) Jan 23 2021 Thank!
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 ?
Jan 22 2021
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 );
Jan 22 2021
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.
Jan 22 2021
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.)
Jan 22 2021
On Saturday, 23 January 2021 at 05:54:09 UTC, Виталий Фадеев wrote:On Saturday, 23 January 2021 at 05:39:18 UTC, Виталий Фадеев wrote: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 ?Context: data + GUI List Goal: auto list = new List( data );
Jan 22 2021
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
Jan 23 2021
On Saturday, 23 January 2021 at 19:19:29 UTC, Paul Backus wrote:On Saturday, 23 January 2021 at 07:17:38 UTC, Виталий Фадеев wrote:Thank!Where source ? Where deduction implementation ?Here: https://github.com/dlang/dmd/blob/v2.095.0/src/dmd/dtemplate.d#L1308
Jan 23 2021