January 05, 2012 Re: Specialization - Major hole in the spec? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trass3r | On 5/01/12 9:37 PM, Trass3r wrote: > On Thursday, 5 January 2012 at 15:11:13 UTC, Timon Gehr wrote: >> On 01/05/2012 10:14 AM, Peter Alexander wrote: >>> In any case, it is surely a bug. I have *explicitly* specified that T >>> must be int, yet it has called a version with T == float. >> >> No it has called a version with T : float. ":" means "implicitly >> converts to". This is by design. > > I think we should rename this to something like 'type constraint' > instead of specialization (and add the explanation that it means > implicitly converts). As this thread shows C++ programmers will be > confused otherwise. Upon further investigation, the website disagrees with what you've said. http://dlang.org/templates-revisited.html ------------------------------------------------------------------------ Each can have default values, and type parameters can have (a limited form of) constraints: class B { ... } interface I { ... } class Foo( R, // R can be any type P:P*, // P must be a pointer type T:int, // T must be int type S:T*, // S must be pointer to T C:B, // C must be of class B or derived // from B U:I, // U must be a class that // implements interface I string str = "hello", // string literal, // default is "hello" alias A = B // A is any symbol // (including template symbols), // defaulting to B ) { ... } ------------------------------------------------------------------------ On the T:int line it says "T must be int type", not "T must implicitly convert to int". Where did you read that : means implicitly converts to? I can't find it anywhere on the site, or in TDPL. |
January 05, 2012 Re: Specialization - Major hole in the spec? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Peter Alexander | On Jan 5, 2012, at 3:22 PM, Peter Alexander wrote:
>
> On the T:int line it says "T must be int type", not "T must implicitly convert to int".
>
> Where did you read that : means implicitly converts to? I can't find it anywhere on the site, or in TDPL.
That's how "is" expressions work. I think the rules for templates are different though. For example, you can't do T=float to specialize a template.
|
January 06, 2012 Re: Specialization - Major hole in the spec? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Peter Alexander | On Thursday, 5 January 2012 at 23:11:31 UTC, Peter Alexander wrote:
> http://dlang.org/templates-revisited.html
>
> ------------------------------------------------------------------------
> Each can have default values, and type parameters can have (a limited form of) constraints:
>
> class B { ... }
> interface I { ... }
>
> class Foo(
> R, // R can be any type
> P:P*, // P must be a pointer type
> T:int, // T must be int type
> S:T*, // S must be pointer to T
Hmm that's tricky. On the one hand the above doesn't make sense. If : is indeed just a constraint and it was supposed to mean _no implicit conversions_ you could just leave T out and replace it with int in the first place.
On the other hand if you want to implement C++-like specialization you probably need explicit types.
Both issues could still be solved either way with the use of template constraints.
However, I think it's correct to have : mean implicit conversion.
1. it's a bit more consistent with is expressions.
2. you can always use overloading of non-template and template functions which is much clearer anyway (resp. for now use the workaround Sean mentioned: void foo()(int* t)).
|
January 06, 2012 Re: Specialization - Major hole in the spec? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trass3r | I think another good question is alias this (and thus also our future library typedef). Should 'void foo(T:void*)(T int)' really reject a 'struct A {void* b; alias b this;}'? Don't think so. |
January 06, 2012 Re: Specialization - Major hole in the spec? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Peter Alexander | On 01/06/2012 12:22 AM, Peter Alexander wrote: > On 5/01/12 9:37 PM, Trass3r wrote: >> On Thursday, 5 January 2012 at 15:11:13 UTC, Timon Gehr wrote: >>> On 01/05/2012 10:14 AM, Peter Alexander wrote: >>>> In any case, it is surely a bug. I have *explicitly* specified that T >>>> must be int, yet it has called a version with T == float. >>> >>> No it has called a version with T : float. ":" means "implicitly >>> converts to". This is by design. >> >> I think we should rename this to something like 'type constraint' >> instead of specialization (and add the explanation that it means >> implicitly converts). As this thread shows C++ programmers will be >> confused otherwise. > > Upon further investigation, the website disagrees with what you've said. > > http://dlang.org/templates-revisited.html > Interesting. I think this is an error in the website. Walter? > > On the T:int line it says "T must be int type", not "T must implicitly > convert to int". > > Where did you read that : means implicitly converts to? I can't find it > anywhere on the site, or in TDPL. It is what it means in 'is' expressions, and what it means in DMD. |
January 06, 2012 Re: Specialization - Major hole in the spec? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On Friday, January 06, 2012 01:47:03 Timon Gehr wrote:
> On 01/06/2012 12:22 AM, Peter Alexander wrote:
> > On 5/01/12 9:37 PM, Trass3r wrote:
> >> On Thursday, 5 January 2012 at 15:11:13 UTC, Timon Gehr wrote:
> >>> On 01/05/2012 10:14 AM, Peter Alexander wrote:
> >>>> In any case, it is surely a bug. I have *explicitly* specified
> >>>> that T
> >>>> must be int, yet it has called a version with T == float.
> >>>
> >>> No it has called a version with T : float. ":" means "implicitly converts to". This is by design.
> >>
> >> I think we should rename this to something like 'type constraint' instead of specialization (and add the explanation that it means implicitly converts). As this thread shows C++ programmers will be confused otherwise.
> >
> > Upon further investigation, the website disagrees with what you've said.
> >
> > http://dlang.org/templates-revisited.html
>
> Interesting. I think this is an error in the website. Walter?
I'm certain that it's an error in the website. : means implicitly convertible, no matter by what means that implicit conversion occurs (built-in, alias this, etc.). You use == if you want the types to match exactly (including modifiers such as const or shared).
- Jonathan M Davis
|
Copyright © 1999-2021 by the D Language Foundation