Thread overview | ||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 26, 2012 Re: 2 problems I can't get my head around | ||||
---|---|---|---|---|
| ||||
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.
|
November 26, 2012 Re: 2 problems I can't get my head around | ||||
---|---|---|---|---|
| ||||
Attachments:
| On 26 November 2012 14:39, Andrej Mitrovic <andrej.mitrovich@gmail.com>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. > Well that's certainly not intuitive. I've never even heard that term before. It looks awfully like an enum, with the whole 'enum' keyword and all ;) How do I detect that then? |
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:46:10 UTC, Manu wrote:
> On 26 November 2012 14:39, Andrej Mitrovic <andrej.mitrovich@gmail.com>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.
>>
>
> Well that's certainly not intuitive. I've never even heard that term
> before. It looks awfully like an enum, with the whole 'enum' keyword and
> all ;)
> How do I detect that then?
The term enum (AFAIK) is comes from an old C++ hack, where you'd create an actual enum to represent variable that's useable compile-time.
Anyways, when you declare an actual enumerate, you have to declare:
1) The enum type
2) The enum values
3) The enum variable
So in your case, it would have to be:
--------
enum Enumerate
{
state1 = 10,
state2
}
Enumerate i = Enumerate.state1;
-----
|
November 26, 2012 Re: 2 problems I can't get my head around | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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.
Everytime I see just code, I always think why const is not used instead.
--
Paulo
|
November 26, 2012 Re: 2 problems I can't get my head around | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra Attachments:
| On 26 November 2012 15:00, monarch_dodra <monarchdodra@gmail.com> wrote:
> On Monday, 26 November 2012 at 12:46:10 UTC, Manu wrote:
>
>> On 26 November 2012 14:39, Andrej Mitrovic <andrej.mitrovich@gmail.com>** 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.
>>>
>>>
>> Well that's certainly not intuitive. I've never even heard that term
>> before. It looks awfully like an enum, with the whole 'enum' keyword and
>> all ;)
>> How do I detect that then?
>>
>
> The term enum (AFAIK) is comes from an old C++ hack, where you'd create an actual enum to represent variable that's useable compile-time.
>
> Anyways, when you declare an actual enumerate, you have to declare:
> 1) The enum type
> 2) The enum values
> 3) The enum variable
>
> So in your case, it would have to be:
> --------
> enum Enumerate
> {
> state1 = 10,
> state2
> }
> Enumerate i = Enumerate.state1;
> -----
>
I'm not looking for a hot-to use enums, I need to know how to form an expression to make the pragma show true in precisely that context.
|
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:08:57 UTC, Manu wrote:
> On 26 November 2012 15:00, monarch_dodra <monarchdodra@gmail.com> wrote:
>
>> On Monday, 26 November 2012 at 12:46:10 UTC, Manu wrote:
>>
>>> On 26 November 2012 14:39, Andrej Mitrovic <andrej.mitrovich@gmail.com>**
>>> 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.
>>>>
>>>>
>>> Well that's certainly not intuitive. I've never even heard that term
>>> before. It looks awfully like an enum, with the whole 'enum' keyword and
>>> all ;)
>>> How do I detect that then?
>>>
>>
>> The term enum (AFAIK) is comes from an old C++ hack, where you'd create an
>> actual enum to represent variable that's useable compile-time.
>>
>> Anyways, when you declare an actual enumerate, you have to declare:
>> 1) The enum type
>> 2) The enum values
>> 3) The enum variable
>>
>> So in your case, it would have to be:
>> --------
>> enum Enumerate
>> {
>> state1 = 10,
>> state2
>> }
>> Enumerate i = Enumerate.state1;
>> -----
>>
>
> I'm not looking for a hot-to use enums, I need to know how to form an
> expression to make the pragma show true in precisely that context.
Is see, but (unfortunatly), as already mentioned, the keyword "enum" is also used to create a manifest constant. That means the tests you are giving your i are irrelevant.
If you really want to test a manifest constant, then I guess you can test for the "lvalue-ness" of your variable:
enum i = 10;
int j = 10;
pragma(msg, __traits(compiles, (ref typeof(i) x) { } (i)));
pragma(msg, __traits(compiles, (ref typeof(j) x) { } (j)));
of the "Compile time knowability"
enum i = 10;
int j = 10;
pragma(msg, __traits(compiles, () {int[i] a;}));
pragma(msg, __traits(compiles, () {int[j] a;}));
That said, it is not an "iff" relation. I do not know of any way to test if something is *only* a manifest constant...
|
November 26, 2012 Re: 2 problems I can't get my head around | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra Attachments:
| On 26 November 2012 15:27, monarch_dodra <monarchdodra@gmail.com> wrote:
> On Monday, 26 November 2012 at 13:08:57 UTC, Manu wrote:
>
>> On 26 November 2012 15:00, monarch_dodra <monarchdodra@gmail.com> wrote:
>>
>> On Monday, 26 November 2012 at 12:46:10 UTC, Manu wrote:
>>>
>>> On 26 November 2012 14:39, Andrej Mitrovic <andrej.mitrovich@gmail.com
>>>> >**
>>>>
>>>> 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.
>>>>>
>>>>>
>>>>> Well that's certainly not intuitive. I've never even heard that term
>>>> before. It looks awfully like an enum, with the whole 'enum' keyword and
>>>> all ;)
>>>> How do I detect that then?
>>>>
>>>>
>>> The term enum (AFAIK) is comes from an old C++ hack, where you'd create
>>> an
>>> actual enum to represent variable that's useable compile-time.
>>>
>>> Anyways, when you declare an actual enumerate, you have to declare:
>>> 1) The enum type
>>> 2) The enum values
>>> 3) The enum variable
>>>
>>> So in your case, it would have to be:
>>> --------
>>> enum Enumerate
>>> {
>>> state1 = 10,
>>> state2
>>> }
>>> Enumerate i = Enumerate.state1;
>>> -----
>>>
>>>
>> I'm not looking for a hot-to use enums, I need to know how to form an expression to make the pragma show true in precisely that context.
>>
>
> Is see, but (unfortunatly), as already mentioned, the keyword "enum" is also used to create a manifest constant. That means the tests you are giving your i are irrelevant.
>
> If you really want to test a manifest constant, then I guess you can test
> for the "lvalue-ness" of your variable:
> enum i = 10;
> int j = 10;
> pragma(msg, __traits(compiles, (ref typeof(i) x) { } (i)));
> pragma(msg, __traits(compiles, (ref typeof(j) x) { } (j)));
>
> of the "Compile time knowability"
> enum i = 10;
> int j = 10;
> pragma(msg, __traits(compiles, () {int[i] a;}));
> pragma(msg, __traits(compiles, () {int[j] a;}));
>
> That said, it is not an "iff" relation. I do not know of any way to test if something is *only* a manifest constant...
>
That's sooo brutal! But it works, I'll use the first one, the second depends on i being an int.
|
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:45:54 UTC, Manu wrote: > > That's sooo brutal! But it works, I'll use the first one, the second > depends on i being an int. Just so you know, the first one comes from a proposed "isLvalue" trait: https://github.com/denis-sh/phobos/commit/7c64953a907cece7759ec66ebceeb12b49910374 The one in the pull is a bit more complete. |
November 26, 2012 Re: 2 problems I can't get my head around | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | > 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; }));
}
|
November 26, 2012 Re: 2 problems I can't get my head around | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra Attachments:
| On 26 November 2012 15:54, monarch_dodra <monarchdodra@gmail.com> wrote:
> On Monday, 26 November 2012 at 13:45:54 UTC, Manu wrote:
>
>>
>> That's sooo brutal! But it works, I'll use the first one, the second depends on i being an int.
>>
>
> Just so you know, the first one comes from a proposed "isLvalue" trait:
> https://github.com/denis-sh/**phobos/commit/**
> 7c64953a907cece7759ec66ebceeb1**2b49910374<https://github.com/denis-sh/phobos/commit/7c64953a907cece7759ec66ebceeb12b49910374>
>
> The one in the pull is a bit more complete.
>
Well that'll be good.
So that solves one of my problems for the time being... can anyone comment on the module-looks-like-a-variable problem?
|
Copyright © 1999-2021 by the D Language Foundation