Thread overview | |||||
---|---|---|---|---|---|
|
November 26, 2012 2 problems I can't get my head around | ||||
---|---|---|---|---|
| ||||
Attachments:
| 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. |
November 26, 2012 Re: 2 problems I can't get my head around | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | 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)); |
November 26, 2012 Re: 2 problems I can't get my head around | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | 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));
}
|
Copyright © 1999-2021 by the D Language Foundation