www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - ICTI and ISTI

reply "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
IFTI -- implicit function template instantiation -- is a pretty nifty 
feature in D, which allows code that calls templated functions to be 
written in a nice, clear way.

Would it be possible to extend it to also work for class/struct 
construction, so that the type of a templated class can be deduced from 
its constructor's arguments? Here's an example of what I mean:

   class Foo(T)
   {
       T bar;
       this(T t) { bar = t; }
   }

   auto foo = new Foo(1.0);  // The type of foo is now Foo!double

I've found myself wishing for this in several cases. Sometimes the 
template specification has been so involved that I've ended up writing 
factory methods instead of ordinary constructors. But then, the classes 
in question become inconsistent with the rest of my code, and I hate 
inconsistencies.

I've only seen this mentioned once before, in bug report 1856, but none 
of the comments in that report say anything more about it.

   http://d.puremagic.com/issues/show_bug.cgi?id=1856

Are there specific reasons why ICTI and ISTI wouldn't work?

-Lars
Nov 09 2009
parent dsimcha <dsimcha yahoo.com> writes:
== Quote from Lars T. Kyllingstad (public kyllingen.NOSPAMnet)'s article
 IFTI -- implicit function template instantiation -- is a pretty nifty
 feature in D, which allows code that calls templated functions to be
 written in a nice, clear way.
 Would it be possible to extend it to also work for class/struct
 construction, so that the type of a templated class can be deduced from
 its constructor's arguments? Here's an example of what I mean:
    class Foo(T)
    {
        T bar;
        this(T t) { bar = t; }
    }
    auto foo = new Foo(1.0);  // The type of foo is now Foo!double
 I've found myself wishing for this in several cases. Sometimes the
 template specification has been so involved that I've ended up writing
 factory methods instead of ordinary constructors. But then, the classes
 in question become inconsistent with the rest of my code, and I hate
 inconsistencies.
 I've only seen this mentioned once before, in bug report 1856, but none
 of the comments in that report say anything more about it.
    http://d.puremagic.com/issues/show_bug.cgi?id=1856
 Are there specific reasons why ICTI and ISTI wouldn't work?
 -Lars

I guess this is just yet another symptom of c'tors being "special". However, IDK how we c'tors **not** special because they have the exclusive privilege of setting immutable data. This should probably be considered as part of the "what to do about new" discussion.
Nov 09 2009