www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - 2 problems I can't get my head around

reply Manu <turkeyman gmail.com> writes:
1.

enum i = 10;
pragma(msg, is(i == enum) || is(typeof(i) == enum)); // <- false?!

I can't find a way to identify that i is an enum, not a variable; can not
be assigned, has no address, etc.


2.

import std.stdio;
pragma(msg, !is(std) && is(typeof(std))); // <- true?!

std.stdio is a module, it looks like a variable. typeof(std) == void...
What the? Why does it even have a type?
I can't find a sensible way to distinguish std from any other regular
variable.
Nov 26 2012
next sibling parent "John Chapman" <johnch_atms hotmail.com> writes:
On Monday, 26 November 2012 at 12:32:08 UTC, Manu wrote:
 1.

 enum i = 10;
 pragma(msg, is(i == enum) || is(typeof(i) == enum)); // <- 
 false?!

 I can't find a way to identify that i is an enum, not a 
 variable; can not
 be assigned, has no address, etc.
Because 'i' is not an enum, it's actually a manifest constant. Unless you specify an enum name, no new type is created. http://dlang.org/enum.html Compare: enum E { i = 10 } pragma(msg, is(typeof(E.i) == enum));
Nov 26 2012
prev sibling parent "Dan" <dbdavidson yahoo.com> writes:
On Monday, 26 November 2012 at 12:32:08 UTC, Manu wrote:
 2.

 import std.stdio;
 pragma(msg, !is(std) && is(typeof(std))); // <- true?!

 std.stdio is a module, it looks like a variable. typeof(std) == 
 void...
 What the? Why does it even have a type?
 I can't find a sensible way to distinguish std from any other 
 regular
 variable.
traits uses something like this (see fullyQualifiedName) Thanks Dan import std.stdio; template isPackage(alias name) { static if(name.stringof.length >= 9 && name.stringof[0..8] == "package ") { enum isPackage = true; } else { enum isPackage = false; } } void main() { pragma(msg, "Is std a package:", isPackage!std); pragma(msg, "Is std.stdio a package:", isPackage!(std.stdio)); }
Nov 26 2012