www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 7529] New: IFTI does not support aliases

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7529

           Summary: IFTI does not support aliases
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: kyfolee gmail.com


--- Comment #0 from Kyle Foley <kyfolee gmail.com> 2012-02-16 21:35:01 EST ---
DMD 2.058

template Type(T) { alias T Type; }
void f(T)(T, Type!(T)) {}

void main()
{    
    f(0, 0); // fail
    f!(int)(0, 0); // success
}

---

.\ifti_alias.d(6): Error: template ifti_alias.f(T) does not match any function
template declaration
.\ifti_alias.d(6): Error: template ifti_alias.f(T) cannot deduce template
function from argument types !()(int,int)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 16 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7529


timon.gehr gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |timon.gehr gmx.ch
            Summary|IFTI does not support       |IFTI does not support
                   |aliases                     |template argument dependent
                   |                            |template alias instances as
                   |                            |parameter types
           Severity|normal                      |enhancement


--- Comment #1 from timon.gehr gmx.ch 2012-02-16 20:23:46 PST ---
This is an enhancement, a very desirable one.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 16 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7529


dawg dawgfoto.de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dawg dawgfoto.de


--- Comment #2 from dawg dawgfoto.de 2012-02-16 20:49:38 PST ---
The issue is what do you make of related cases.

// not deducible
void f(T)(Type!T) {}
f(0);

// ?
void f(T)(Type!T, T) {}
f(0, 0);

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 16 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7529



--- Comment #3 from timon.gehr gmx.ch 2012-02-16 21:24:43 PST ---
Both of those(In reply to comment #2)
 The issue is what do you make of related cases.
 
 // not deducible
 void f(T)(Type!T) {}
 f(0);
 
 // ?
 void f(T)(Type!T, T) {}
 f(0, 0);
Both of those should work. The enhancement demands special treatment of templated aliases. For example, this would work too: template LList(T){alias Lazy!(List!T)) LList;} alias f(T)(LList!T){} f(new LList); -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 16 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7529



--- Comment #4 from timon.gehr gmx.ch 2012-02-16 21:25:43 PST ---
I meant
void f(T)(LList!T){}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 16 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7529



--- Comment #5 from dawg dawgfoto.de 2012-02-16 22:03:04 PST ---
The reason why this works with structs but not with templates is that a struct
preserves the full type information while an alias template transforms a type.

void bar(Ty)(Ty) { pragma(msg, Ty); }

struct SList(T) {}
template TList(T) { alias T TList; }

void main()
{
    bar(SList!int.init); // type SList!int => SList!int is preserved
    bar(TList!int.init); // type int       => TList!int is lost
}

Now if you take a function void foo(T)(TList!T) {} and gives you 'TList(T) =
int'.
What your asking for is an inverted template 'TList^-1(int) = T'.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 16 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7529



--- Comment #6 from timon.gehr gmx.ch 2012-02-17 04:34:10 PST ---
(In reply to comment #5)
 The reason why this works with structs but not with templates is that a struct
 preserves the full type information while an alias template transforms a type.
 
 void bar(Ty)(Ty) { pragma(msg, Ty); }
 
 struct SList(T) {}
 template TList(T) { alias T TList; }
 
 void main()
 {
     bar(SList!int.init); // type SList!int => SList!int is preserved
     bar(TList!int.init); // type int       => TList!int is lost
 }
 
 Now if you take a function void foo(T)(TList!T) {} and gives you 'TList(T) =
 int'.
 What your asking for is an inverted template 'TList^-1(int) = T'.
Well, yes. But only for a simple set of cases where this is workable. Basically the alias would need to be expanded before IFTI matching. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 17 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7529



--- Comment #7 from dawg dawgfoto.de 2012-02-17 06:52:19 PST ---
void foo(T)(Unsigned!T val)
{
}

foo(2u);

???

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 17 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7529



--- Comment #8 from timon.gehr gmx.ch 2012-02-17 06:54:08 PST ---
Unsigned is not an alias template. Your example is unrelated to this
enhancement.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 17 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7529



--- Comment #9 from Kenji Hara <k.hara.pg gmail.com> 2012-02-17 07:10:54 PST ---
I think this is invalid issue.
Reduced case:

void f(T)(T, T) { pragma(msg, T); }
void main()
{
  f(0L, 0);   // first argument is long, and second one is int
  // Error: template test.f(T) does not match any function template declaration
}

In IFTI, arguments don't have any dependencies each other about the deduction.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 17 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7529



--- Comment #10 from timon.gehr gmx.ch 2012-02-17 07:20:59 PST ---
If you think it is invalid you misunderstand the issue. This is about extending
IFTI so that it can match templated aliases.

template Alias(T){ alias Foo!T Alias;}

void foo(T)(Alias!T x){ pragma(msg, T); }

foo(Foo!int); // prints 'int'

What effectively would need to be done at the compiler side:

- If a template parameter type is a template, check whether it is an eponymous
alias template (like template Alias(T){ alias Foo!T Alias; }
- If so, expand the template without knowing the type it is instantiated with

void foo(T)(Alias!T x){ ... } => void foo(T)(Foo!T x){ ... }

This is always a valid transformation for eponymous alias templates.

This does not introduce any dependencies across parameters.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 17 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7529



--- Comment #11 from dawg dawgfoto.de 2012-02-17 07:25:35 PST ---
Is that what you mean by alias template.
template Wrap(T) { alias T Wrap; } // no members, no static if

This has a bidirectional mapping, but you can't change the type any longer.
'void foo(T)(Wrap!T val)' would always be equal to 'void foo(T)(T val)'

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 17 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7529



--- Comment #12 from timon.gehr gmx.ch 2012-02-17 07:35:44 PST ---
An alias template is like a function template, just for aliases.

Those are all alias templates:

template A(T){alias T A;}
template B(T){alias Foo!T B;}
template C(T){alias foo.bar.Qux!T C;}
template D(T){alias Foo!(Bar!(Qux!T)) D;}

Those are functions that use them:
void fooa(T)(A!T a){ ... }
void foob(T)(B!T b){ ... }
void fooc(T)(C!T c){ ... }
void food(T)(D!T d){ ... }

Those are the versions that do exactly the same thing but work with IFTI:
void fooa(T)(T a){ ... }
void foob(T)(Foo!T b){ ... }
void fooc(T)(foo.bar.Qux!T c){ ... }
void food(T)(Foo!(Bar!(Qux!T)) d){ ... }

What I am asking for is for IFTI to work the same for the former and latter
versions of the functions. This is easy to implement and matches what most
programmers would expect the language to be capable of.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 17 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7529



--- Comment #13 from dawg dawgfoto.de 2012-02-17 07:47:33 PST ---
MidAir collision but thanks for the clarification.

Most of those cases can be handled by aliasing
the template declaration rather than the instance.

alias Foo Alias;
void foo(T)(Alias!T x){ pragma(msg, T); }
foo(Foo!int.init);

Which wouldn't work for recursion.
template D(T){alias Foo!(Bar!(Qux!T)) D;}

It seems to me that what you want is an AST macro not a template expansion.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 17 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7529



--- Comment #14 from Kenji Hara <k.hara.pg gmail.com> 2012-02-17 07:59:59 PST
---
(In reply to comment #12)
 An alias template is like a function template, just for aliases.
 
 Those are all alias templates:
 
 template A(T){alias T A;}
 template B(T){alias Foo!T B;}
 template C(T){alias foo.bar.Qux!T C;}
 template D(T){alias Foo!(Bar!(Qux!T)) D;}
 
 Those are functions that use them:
 void fooa(T)(A!T a){ ... }
 void foob(T)(B!T b){ ... }
 void fooc(T)(C!T c){ ... }
 void food(T)(D!T d){ ... }
 
 Those are the versions that do exactly the same thing but work with IFTI:
 void fooa(T)(T a){ ... }
 void foob(T)(Foo!T b){ ... }
 void fooc(T)(foo.bar.Qux!T c){ ... }
 void food(T)(Foo!(Bar!(Qux!T)) d){ ... }
 
 What I am asking for is for IFTI to work the same for the former and latter
 versions of the functions. This is easy to implement and matches what most
 programmers would expect the language to be capable of.
OK, I almost understand what you expects. When the two declarations exist,
 template B(T){alias Foo!T B;}
 void foob(T)(B!T b){ ... }
and calling foob with IFTI like follows: Foo!int x; foob(x); // foob(T)(B!T) is converted to foob(T)(Foo!T) // *with expanding template B*, then foob deduces T as int. OK? ----- There is some problems about this enhancement. 1) This enhancement requires adding a phase to expanding eponymous templates used as function parameter type. This makes IFTI process more complicate. 2) Some eponymous templates are not one liner. template X(T) { template Y(U) { ... } // massive type calculation alias Y!T X; // also eponymous template } void foo(T)(X!T) { ... } Should compiler calculate T from X!T? It is almost impossible! If it only works with one-linear eponymous template, it is less benefit than the semantic complexity. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 17 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7529



--- Comment #15 from timon.gehr gmx.ch 2012-02-17 08:05:50 PST ---
(In reply to comment #14)
 
 OK?
 
Yes.
 -----
 There is some problems about this enhancement.
 
 1) This enhancement requires adding a phase to expanding eponymous templates
 used as function parameter type. This makes IFTI process more complicate.
 
It can presumably be done while semantically analyzing the parameter types.
 2) Some eponymous templates are not one liner.
 
 template X(T) {
    template Y(U) { ... }  // massive type calculation
    alias Y!T X;   // also eponymous template
 }
 void foo(T)(X!T) { ... }
 
 Should compiler calculate T from X!T? It is almost impossible!
 
It shouldn't, because it *is* impossible.
 If it only works with one-linear eponymous template, it is less benefit than
 the semantic complexity.
I disagree. There is almost no semantic complexity. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 17 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7529



--- Comment #16 from timon.gehr gmx.ch 2012-02-17 08:06:44 PST ---
(In reply to comment #13)
 MidAir collision but thanks for the clarification.
 
 Most of those cases can be handled by aliasing
 the template declaration rather than the instance.
 
 alias Foo Alias;
 void foo(T)(Alias!T x){ pragma(msg, T); }
 foo(Foo!int.init);
 
 Which wouldn't work for recursion.
 template D(T){alias Foo!(Bar!(Qux!T)) D;}
 
 It seems to me that what you want is an AST macro not a template expansion.
IFTI matching already works that way. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 17 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7529



--- Comment #17 from dawg dawgfoto.de 2012-02-17 08:44:58 PST ---
IFTI matching already works that way.
No it does not. With IFTI and structs you recursively match template arguments. Foo!(Bar!(Baz!T))) Foo!(Bar!(Baz!int))) With the alias you need to instantiate the template, probably using a bottom type and then do the above. Foo!(Bar!(Baz!(T))) => Result!T Result!int -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 17 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7529


Steven Schveighoffer <schveiguy yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy yahoo.com


--- Comment #18 from Steven Schveighoffer <schveiguy yahoo.com> 2012-02-17
12:52:32 PST ---
Is this not a duplicate of bug 1807?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 17 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7529


timon.gehr gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |DUPLICATE


--- Comment #19 from timon.gehr gmx.ch 2012-02-17 14:20:37 PST ---
Good catch.

*** This issue has been marked as a duplicate of issue 1807 ***

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