November 26, 2012 Re: 2 problems I can't get my head around | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | > So that solves one of my problems for the time being... can anyone comment
> on the module-looks-like-a-variable problem?
Couldn't you just add !is(typeof(std) == void) to your checks, since variables can't have void type?
|
November 26, 2012 Re: 2 problems I can't get my head around | ||||
---|---|---|---|---|
| ||||
Posted in reply to jerro Attachments:
| On 26 November 2012 15:56, jerro <a@a.com> wrote:
> That's sooo brutal! But it works, I'll use the first one, the second
>> depends on i being an int.
>>
>
> You can also check if it is known at compile time like this:
>
> template knownAtCompileTime(alias a)
> {
> enum knownAtCompileTime = is(typeof({ enum e = a; }));
> }
>
That doesn't work, immutable breaks that test.
|
November 26, 2012 Re: 2 problems I can't get my head around | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | On Monday, 26 November 2012 at 13:57:09 UTC, Manu wrote: > So that solves one of my problems for the time being... can anyone comment > on the module-looks-like-a-variable problem? I responded here: http://forum.dlang.org/post/fbbgjywmjefsemarbvxu@forum.dlang.org Somehow the item got duplicated. Hope it helps. Thanks Dan |
November 26, 2012 Re: 2 problems I can't get my head around | ||||
---|---|---|---|---|
| ||||
Posted in reply to jerro Attachments:
| On 26 November 2012 16:01, jerro <a@a.com> wrote:
> So that solves one of my problems for the time being... can anyone comment
>> on the module-looks-like-a-variable problem?
>>
>
> Couldn't you just add !is(typeof(std) == void) to your checks, since
> variables can't have void type?
...genius! :)
Although also seriously nasty. Why does a module have a type at all?
|
November 26, 2012 Re: 2 problems I can't get my head around | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | > That doesn't work, immutable breaks that test.
You're right. I didn't expect that. It seems that this compiles:
void foo()
{
immutable a = 1;
enum b = a;
}
But this doesn't, obviously:
void foo(immutable int a)
{
enum b = a;
}
I wonder, is this considered a bug? It seems very inconsistent to me.
|
November 26, 2012 Re: 2 problems I can't get my head around | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paulo Pinto | On 11/26/2012 02:01 PM, Paulo Pinto wrote: > On Monday, 26 November 2012 at 12:40:06 UTC, Andrej Mitrovic wrote: >> On 11/26/12, Manu <turkeyman@gmail.com> 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. >> >> It's not an enum, it's a manifest constant. > > This is one issue I have with D's enums, they look like C++ constant > tricks instead of proper enumerations. > enum Numbers{ Zero, One, Two, Three, } > Everytime I see just code, I always think why const is not used instead. > Because 'const' already denotes 'readonly'. :o) |
November 26, 2012 Re: 2 problems I can't get my head around | ||||
---|---|---|---|---|
| ||||
Attachments:
| On 26 November 2012 14:31, Manu <turkeyman@gmail.com> 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.
>
>
> 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.
>
And a new one!
3.
Properties look like variables. How do I distinguish properties from proper
variables?
struct S
{
@property int P() { return 10; }
}
pragma(msg, is(typeof(S.P) == function)) // false?!
|
November 26, 2012 Re: 2 problems I can't get my head around | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | On Monday, 26 November 2012 at 15:39:35 UTC, Manu wrote:
> Properties look like variables. How do I distinguish properties from proper variables?
I think this will do it:
import std.traits;
bool isProperty = (functionAttributes!(S.P) & FunctionAttributes.property) ? true : false
The way that works in the implementation is checking the mangled name of the function. I wouldn't try anything else because any other difference between a property and a variable is arguably a bug since they are supposed to be mostly interchangable!
|
November 26, 2012 Re: 2 problems I can't get my head around | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | On Monday, 26 November 2012 at 15:39:35 UTC, Manu wrote: > On 26 November 2012 14:31, Manu <turkeyman@gmail.com> 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. >> >> >> 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. >> > > And a new one! > > 3. > > Properties look like variables. How do I distinguish properties from proper > variables? > struct S > { > @property int P() { return 10; } > } > > pragma(msg, is(typeof(S.P) == function)) // false?! http://dlang.org/phobos/std_traits.html#functionAttributes |
November 26, 2012 Re: 2 problems I can't get my head around | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe Attachments:
| On 26 November 2012 17:44, Adam D. Ruppe <destructionator@gmail.com> wrote:
> On Monday, 26 November 2012 at 15:39:35 UTC, Manu wrote:
>
>> Properties look like variables. How do I distinguish properties from proper variables?
>>
>
> I think this will do it:
>
> import std.traits;
> bool isProperty = (functionAttributes!(S.P) & FunctionAttributes.property)
> ? true : false
>
>
>
> The way that works in the implementation is checking the mangled name of the function. I wouldn't try anything else because any other difference between a property and a variable is arguably a bug since they are supposed to be mostly interchangable!
>
That's weird. Why would I call functionAttributes in something that I can't even identify as a function? That's almost self-defeating... but as long as functionAttributes will tolerate being called with basically any argument, function or otherwise, then I guess this is okay...
|
Copyright © 1999-2021 by the D Language Foundation