Thread overview
[Issue 1997] New: Better type inference for templated types
Apr 16, 2008
d-bugmail
Mar 10, 2010
Jesse Phillips
Sep 16, 2011
Simen Kjaeraas
April 16, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1997

           Summary: Better type inference for templated types
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: simen.kjaras@gmail.com


In the following code, a shortcoming of current type inference is pointed out:

struct foo(T)
{
  T data;

  static foo!(T) opCall(T t)
  {
    foo!(T) result;
    result.data = t;
    return result;
  }
}

auto f = foo(4);

The line with 'f = foo(4)' gives a template instantiation error, while it should be possible for the compiler to infer the template parameters from the call.

auto g = foo!(int)(4);

This works, but is nowhere near as pretty.


-- 

March 10, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=1997


Jesse Phillips <Jesse.K.Phillips+D@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |Jesse.K.Phillips+D@gmail.co
                   |                            |m
            Version|unspecified                 |2.041


--- Comment #1 from Jesse Phillips <Jesse.K.Phillips+D@gmail.com> 2010-03-10 08:46:35 PST ---
DMD includes private methods when it tries to match for type inference even though the private function is not available.

.\test.d(10): Error: template bar.Bar(T) does not match any function template
declaration
.\test.d(10): Error: template bar.Bar(T) cannot deduce template function from
argument types !()(void delegate())

------------------
module bar;

import foo;

void Bar(T)(void delegate(T) call) {
}

void main() {
   auto foo = new Foo();
   Bar(&foo.fish);
}

------------------
module foo;

class Foo {
   private void fish() {
   }
   public void fish(string color) {
   }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 16, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=1997



--- Comment #2 from Simen Kjaeraas <simen.kjaras@gmail.com> 2011-09-16 08:17:12 PDT ---
It is worth noting here that the workaround is to use a free function:

struct Foo( T ) {
    T data;
    this( T value ) {
        data = value;
    }
}

Foo!T foo( T )( T value ) {
    return Foo!T( value );
}

void main( ) {
    foo( 4 );
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------