www.digitalmars.com         C & C++   DMDScript  

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

reply Manu <turkeyman gmail.com> writes:
--047d7bdc09ee2b037204cf651e48
Content-Type: text/plain; charset=UTF-8

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.

--047d7bdc09ee2b037204cf651e48
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div>1.</div><div><br></div>enum i =3D 10;<div>pragma(msg, is(i =3D=3D enum=
) || is(typeof(i) =3D=3D enum)); // &lt;- false?!<br></div><div><br></div><=
div>I can&#39;t find a way to identify that i is an enum, not a variable; c=
an not be assigned, has no address, etc.</div>
<div><br></div><div><br></div><div>2.</div><div><br></div><div>import std.s=
tdio;</div><div>pragma(msg, !is(std) &amp;&amp; is(typeof(std))); // &lt;- =
true?!</div><div><br></div><div>std.stdio is a module, it looks like a vari=
able. typeof(std) =3D=3D void... What the? Why does it even have a type?</d=
iv>
<div>I can&#39;t find a sensible way to distinguish std from any other regu=
lar variable.</div>

--047d7bdc09ee2b037204cf651e48--
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