digitalmars.D.learn - Why __traits(compile,...) fails here?
- Zhenya (11/11) Aug 07 2012 import std.stdio;
- David (2/13) Aug 07 2012 http://dpaste.dzfl.pl/bd54413f
- Artur Skawina (6/18) Aug 07 2012 Template alias parameters do not accept built-in types.
- Zhenya (2/23) Aug 07 2012 Thank you)
- Philippe Sigaud (21/22) Aug 07 2012 The trick is:
- Timon Gehr (8/17) Aug 08 2012 Yes, but note that this distinction does not make any sense.
- Philippe Sigaud (3/10) Aug 08 2012 Yes, alias has different semantics depending on its use (in template
- Simen Kjaeraas (8/15) Aug 08 2012 I'm not sure it is 'clearly' a symbol. To the compiler, there is no
import std.stdio; template isType(alias s) { enum isType = !__traits(compiles,mixin("typeof(s)")); } void main() { // writeln(isType!int);// Error: template instance isType!(int) isType!(int) does not match template declaration isType(alias s) writeln(__traits(compiles,mixin("typeof(int)")));//write: false }
Aug 07 2012
Am 07.08.2012 09:51, schrieb Zhenya:import std.stdio; template isType(alias s) { enum isType = !__traits(compiles,mixin("typeof(s)")); } void main() { // writeln(isType!int);// Error: template instance isType!(int) isType!(int) does not match template declaration isType(alias s) writeln(__traits(compiles,mixin("typeof(int)")));//write: false }http://dpaste.dzfl.pl/bd54413f
Aug 07 2012
On 08/07/12 09:51, Zhenya wrote:import std.stdio; template isType(alias s) { enum isType = !__traits(compiles,mixin("typeof(s)")); } void main() { // writeln(isType!int);// Error: template instance isType!(int) isType!(int) does not match template declaration isType(alias s) writeln(__traits(compiles,mixin("typeof(int)")));//write: false }Template alias parameters do not accept built-in types. template isType(s) /*...*/ would compile. artur
Aug 07 2012
On Tuesday, 7 August 2012 at 09:47:58 UTC, Artur Skawina wrote:On 08/07/12 09:51, Zhenya wrote:Thank you)import std.stdio; template isType(alias s) { enum isType = !__traits(compiles,mixin("typeof(s)")); } void main() { // writeln(isType!int);// Error: template instance isType!(int) isType!(int) does not match template declaration isType(alias s) writeln(__traits(compiles,mixin("typeof(int)")));//write: false }Template alias parameters do not accept built-in types. template isType(s) /*...*/ would compile. artur
Aug 07 2012
On Tue, Aug 7, 2012 at 12:42 PM, Zhenya <zheny list.ru> wrote:The trick is: - A template type parameter (like (T)) recognizes types - A template alias parameter(like (alias a)) recognizes names, symbols. A built-in type is 'purely' a type. It's a keyword, and hence it's not a symbol ('int' cannot be a D identifier) A user-defined type is *both* a type and a symbol (because it has a name) So, given: class C {} template isType(T) { ... } template isName(alias name) { ... } then isType!int => accepted isType!C => accepted isName!int => not accepted isName!C => accepted which also means you can design a template that accepts - only built-in types - only user-defined types - both - neither (with other template parameters...)Template alias parameters do not accept built-in types.
Aug 07 2012
On 08/07/2012 03:52 PM, Philippe Sigaud wrote:On Tue, Aug 7, 2012 at 12:42 PM, Zhenya<zheny list.ru> wrote:Yes, but note that this distinction does not make any sense. alias int Int; // works isType!Int // 'Int' is clearly a symbol, yet the instantiation fails. alias declarations should just accept built-in types. If someone for some obscure reason needs to exclude them, template constraints are good enough. There is no reason to make built-in types behave specially here.The trick is: - A template type parameter (like (T)) recognizes types - A template alias parameter(like (alias a)) recognizes names, symbols. A built-in type is 'purely' a type. It's a keyword, and hence it's not a symbol ('int' cannot be a D identifier) A user-defined type is *both* a type and a symbol (because it has a name) ...Template alias parameters do not accept built-in types.
Aug 08 2012
On Wed, Aug 8, 2012 at 11:13 AM, Timon Gehr <timon.gehr gmx.ch> wrote:Yes, but note that this distinction does not make any sense. alias int Int; // works isType!Int // 'Int' is clearly a symbol, yet the instantiation fails. alias declarations should just accept built-in types. If someone for some obscure reason needs to exclude them, template constraints are good enough. There is no reason to make built-in types behave specially here.Yes, alias has different semantics depending on its use (in template param list or as a statement).
Aug 08 2012
On Wed, 08 Aug 2012 11:13:30 +0200, Timon Gehr <timon.gehr gmx.ch> wrote:Yes, but note that this distinction does not make any sense. alias int Int; // works isType!Int // 'Int' is clearly a symbol, yet the instantiation fails.I'm not sure it is 'clearly' a symbol. To the compiler, there is no difference between int and your Int - they're just different names for the exact same thing. Depending on when the translation occurs, it may not be sensible to think of them as separate.alias declarations should just accept built-in types. If someone for some obscure reason needs to exclude them, template constraints are good enough. There is no reason to make built-in types behave specially here.Agreed. -- Simen
Aug 08 2012