www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Deduct and return class type

reply =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= writes:
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
parent reply =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= writes:
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
next sibling parent Paul Backus <snarwin gmail.com> writes:
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
prev sibling next sibling parent tsbockman <thomas.bockman gmail.com> writes:
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
prev sibling parent reply =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= writes:
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 ?
Jan 22 2021
parent reply Paul Backus <snarwin gmail.com> writes:
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
parent =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= writes:
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!
Jan 23 2021