Thread overview
function pointer as the enum base type
Oct 05, 2008
mumba
Oct 05, 2008
mumba
Oct 05, 2008
Denis Koroskin
Oct 05, 2008
Denis Koroskin
Oct 06, 2008
Robert Fraser
Oct 05, 2008
mumba
Oct 05, 2008
BCS
October 05, 2008
Idea: to make an enum type with function pointers as it's elements. Problem: no more than one element can be inserted!

$echo '
void foo() {}
void bar() {}

enum Enum : void function() {
    FOO = &foo,
    BAR = &bar
}

int main() { return 0;	}' > enum.d
$dmd --help
Digital Mars D Compiler v2.014
Copyright (c) 1999-2008 by Digital Mars written by Walter Bright
Documentation: http://www.digitalmars.com/d/2.0/index.html
...
$dmd enum.d
enum.d(5): Error: Integer constant expression expected instead of (& bar) < (& foo)
enum.d(5): Error: Integer constant expression expected instead of (& bar) > (& foo)


I don't understand the error message. First - why integer constant expression expected, if it's supposed to be the function based enum? Second - instead of what? (& bar) expression, which is smaller than (& foo), or boolean expression (& bar) < (& foo)? Third - in both cases: if only one of these messages appeard - it would be just strange for me. But as they are together and both refer to the same line - I'm knocked out. What logic leads to such mutually exclusive pieces of information?

Please someone explain it to me.

By the way:

enum Enum : void function() {
    FOO = &foo
}

works fine.


cheers

October 05, 2008
mumba Wrote:

uh, sorry :( . The error message refers to the 7th line. To this, to be precise:

BAR = &bar



October 05, 2008
On Mon, 06 Oct 2008 03:21:09 +0400, mumba <qniol@o2.pl> wrote:

> Idea: to make an enum type with function pointers as it's elements.
> Problem: no more than one element can be inserted!
>
> $echo '
> void foo() {}
> void bar() {}
>
> enum Enum : void function() {
>     FOO = &foo,
>     BAR = &bar
> }
>
> int main() { return 0;	}' > enum.d
> $dmd --help
> Digital Mars D Compiler v2.014
> Copyright (c) 1999-2008 by Digital Mars written by Walter Bright
> Documentation: http://www.digitalmars.com/d/2.0/index.html
> ...
> $dmd enum.d
> enum.d(5): Error: Integer constant expression expected instead of (& bar) < (& foo)
> enum.d(5): Error: Integer constant expression expected instead of (& bar) > (& foo)
>
>
> I don't understand the error message.
> First - why integer constant expression expected, if it's supposed to be the function based enum? Second - instead of what? (& bar) expression, which is smaller than (& foo), or boolean expression (& bar) < (& foo)?
> Third - in both cases: if only one of these messages appeard - it would be just strange for me. But as they are together and both refer to the same line - I'm knocked out. What logic leads to such mutually exclusive pieces of information?
>
> Please someone explain it to me.
>
> By the way:
>
> enum Enum : void function() {
>     FOO = &foo
> }
>
> works fine.
>
>
> cheers
>


Looks like it can't sort the enum values at compile time. That's because &foo and &bar are not known until codegen phase.
October 05, 2008
On Sun, Oct 5, 2008 at 7:26 PM, Denis Koroskin <2korden@gmail.com> wrote:
> On Mon, 06 Oct 2008 03:21:09 +0400, mumba <qniol@o2.pl> wrote:
>
>> Idea: to make an enum type with function pointers as it's elements. Problem: no more than one element can be inserted!
>>
>> $echo '
>> void foo() {}
>> void bar() {}
>>
>> enum Enum : void function() {
>>    FOO = &foo,
>>    BAR = &bar
>> }
>>
>> int main() { return 0;  }' > enum.d
>> $dmd --help
>> Digital Mars D Compiler v2.014
>> Copyright (c) 1999-2008 by Digital Mars written by Walter Bright
>> Documentation: http://www.digitalmars.com/d/2.0/index.html
>> ...
>> $dmd enum.d
>> enum.d(5): Error: Integer constant expression expected instead of (& bar)
>> < (& foo)
>> enum.d(5): Error: Integer constant expression expected instead of (& bar)
>> > (& foo)
>>
>>
>> I don't understand the error message.
>> First - why integer constant expression expected, if it's supposed to be
>> the function based enum? Second - instead of what? (& bar) expression, which
>> is smaller than (& foo), or boolean expression (& bar) < (& foo)?
>> Third - in both cases: if only one of these messages appeard - it would be
>> just strange for me. But as they are together and both refer to the same
>> line - I'm knocked out. What logic leads to such mutually exclusive pieces
>> of information?
>>
>> Please someone explain it to me.
>>
>> By the way:
>>
>> enum Enum : void function() {
>>    FOO = &foo
>> }
>>
>> works fine.
>>
>>
>> cheers
>>
>
>
> Looks like it can't sort the enum values at compile time. That's because &foo and &bar are not known until codegen phase.
>

Nothing of the sort.  Enums must derive from an integer or boolean type, which function pointers certainly are not.  It's perfectly fine to say:

const FOO = &foo;
October 05, 2008
Reply to mumba,

> Idea: to make an enum type with function pointers as it's elements.

If some way was included to enforce the values being in the set.. Vote++;


October 05, 2008
On Mon, 06 Oct 2008 03:33:05 +0400, Jarrett Billingsley <jarrett.billingsley@gmail.com> wrote:

> On Sun, Oct 5, 2008 at 7:26 PM, Denis Koroskin <2korden@gmail.com> wrote:
>> On Mon, 06 Oct 2008 03:21:09 +0400, mumba <qniol@o2.pl> wrote:
>>
>>> Idea: to make an enum type with function pointers as it's elements.
>>> Problem: no more than one element can be inserted!
>>>
>>> $echo '
>>> void foo() {}
>>> void bar() {}
>>>
>>> enum Enum : void function() {
>>>    FOO = &foo,
>>>    BAR = &bar
>>> }
>>>
>>> int main() { return 0;  }' > enum.d
>>> $dmd --help
>>> Digital Mars D Compiler v2.014
>>> Copyright (c) 1999-2008 by Digital Mars written by Walter Bright
>>> Documentation: http://www.digitalmars.com/d/2.0/index.html
>>> ...
>>> $dmd enum.d
>>> enum.d(5): Error: Integer constant expression expected instead of (& bar)
>>> < (& foo)
>>> enum.d(5): Error: Integer constant expression expected instead of (& bar)
>>> > (& foo)
>>>
>>>
>>> I don't understand the error message.
>>> First - why integer constant expression expected, if it's supposed to be
>>> the function based enum? Second - instead of what? (& bar) expression, which
>>> is smaller than (& foo), or boolean expression (& bar) < (& foo)?
>>> Third - in both cases: if only one of these messages appeard - it would be
>>> just strange for me. But as they are together and both refer to the same
>>> line - I'm knocked out. What logic leads to such mutually exclusive pieces
>>> of information?
>>>
>>> Please someone explain it to me.
>>>
>>> By the way:
>>>
>>> enum Enum : void function() {
>>>    FOO = &foo
>>> }
>>>
>>> works fine.
>>>
>>>
>>> cheers
>>>
>>
>>
>> Looks like it can't sort the enum values at compile time. That's because
>> &foo and &bar are not known until codegen phase.
>>
>
> Nothing of the sort.  Enums must derive from an integer or boolean
> type, which function pointers certainly are not.  It's perfectly fine
> to say:
>
> const FOO = &foo;

Yeah, of course. But why the following works?

import std.stdio;

int foo() { return 42; }

enum Enum : int function() {
    FOO = &foo,
//  BAR = &foo    // uncomment the line and it stops working
}

int main()
{
	writefln(Enum.FOO());
	return 0;
}
October 05, 2008
Denis Koroskin Wrote:

> Looks like it can't sort the enum values at compile time. That's because &foo and &bar are not known until codegen phase.

That's another thing I'm meditating on. Why do we need any kind of ordering in enum types? Isn't it supposed to be the SET of possible values? Set containing not necessarily orderable elements?
October 06, 2008
Denis Koroskin wrote:
> On Mon, 06 Oct 2008 03:33:05 +0400, Jarrett Billingsley <jarrett.billingsley@gmail.com> wrote:
> 
>> On Sun, Oct 5, 2008 at 7:26 PM, Denis Koroskin <2korden@gmail.com> wrote:
>>> On Mon, 06 Oct 2008 03:21:09 +0400, mumba <qniol@o2.pl> wrote:
>>>
>>>> Idea: to make an enum type with function pointers as it's elements.
>>>> Problem: no more than one element can be inserted!
>>>>
>>>> $echo '
>>>> void foo() {}
>>>> void bar() {}
>>>>
>>>> enum Enum : void function() {
>>>>    FOO = &foo,
>>>>    BAR = &bar
>>>> }
>>>>
>>>> int main() { return 0;  }' > enum.d
>>>> $dmd --help
>>>> Digital Mars D Compiler v2.014
>>>> Copyright (c) 1999-2008 by Digital Mars written by Walter Bright
>>>> Documentation: http://www.digitalmars.com/d/2.0/index.html
>>>> ...
>>>> $dmd enum.d
>>>> enum.d(5): Error: Integer constant expression expected instead of (& bar)
>>>> < (& foo)
>>>> enum.d(5): Error: Integer constant expression expected instead of (& bar)
>>>> > (& foo)
>>>>
>>>>
>>>> I don't understand the error message.
>>>> First - why integer constant expression expected, if it's supposed to be
>>>> the function based enum? Second - instead of what? (& bar) expression, which
>>>> is smaller than (& foo), or boolean expression (& bar) < (& foo)?
>>>> Third - in both cases: if only one of these messages appeard - it would be
>>>> just strange for me. But as they are together and both refer to the same
>>>> line - I'm knocked out. What logic leads to such mutually exclusive pieces
>>>> of information?
>>>>
>>>> Please someone explain it to me.
>>>>
>>>> By the way:
>>>>
>>>> enum Enum : void function() {
>>>>    FOO = &foo
>>>> }
>>>>
>>>> works fine.
>>>>
>>>>
>>>> cheers
>>>>
>>>
>>>
>>> Looks like it can't sort the enum values at compile time. That's because
>>> &foo and &bar are not known until codegen phase.
>>>
>>
>> Nothing of the sort.  Enums must derive from an integer or boolean
>> type, which function pointers certainly are not.  It's perfectly fine
>> to say:
>>
>> const FOO = &foo;
> 
> Yeah, of course. But why the following works?
> 
> import std.stdio;
> 
> int foo() { return 42; }
> 
> enum Enum : int function() {
>     FOO = &foo,
> //  BAR = &foo    // uncomment the line and it stops working
> }
> 
> int main()
> {
>     writefln(Enum.FOO());
>     return 0;
> }

I would call that a "bug".
October 06, 2008
"Jarrett Billingsley" wrote
> On Sun, Oct 5, 2008 at 7:26 PM, Denis Koroskin <2korden@gmail.com> wrote:
>> On Mon, 06 Oct 2008 03:21:09 +0400, mumba <qniol@o2.pl> wrote:
>>
>>> Idea: to make an enum type with function pointers as it's elements. Problem: no more than one element can be inserted!
>>>
>>> $echo '
>>> void foo() {}
>>> void bar() {}
>>>
>>> enum Enum : void function() {
>>>    FOO = &foo,
>>>    BAR = &bar
>>> }
>>>
>>> int main() { return 0;  }' > enum.d
>>> $dmd --help
>>> Digital Mars D Compiler v2.014
>>> Copyright (c) 1999-2008 by Digital Mars written by Walter Bright
>>> Documentation: http://www.digitalmars.com/d/2.0/index.html
>>> ...
>>> $dmd enum.d
>>> enum.d(5): Error: Integer constant expression expected instead of (&
>>> bar)
>>> < (& foo)
>>> enum.d(5): Error: Integer constant expression expected instead of (&
>>> bar)
>>> > (& foo)
>>>
>>>
>>> I don't understand the error message.
>>> First - why integer constant expression expected, if it's supposed to be
>>> the function based enum? Second - instead of what? (& bar) expression,
>>> which
>>> is smaller than (& foo), or boolean expression (& bar) < (& foo)?
>>> Third - in both cases: if only one of these messages appeard - it would
>>> be
>>> just strange for me. But as they are together and both refer to the same
>>> line - I'm knocked out. What logic leads to such mutually exclusive
>>> pieces
>>> of information?
>>>
>>> Please someone explain it to me.
>>>
>>> By the way:
>>>
>>> enum Enum : void function() {
>>>    FOO = &foo
>>> }
>>>
>>> works fine.
>>>
>>>
>>> cheers
>>>
>>
>>
>> Looks like it can't sort the enum values at compile time. That's because &foo and &bar are not known until codegen phase.
>>
>
> Nothing of the sort.  Enums must derive from an integer or boolean type, which function pointers certainly are not.  It's perfectly fine to say:
>
> const FOO = &foo;

AFAIK, enums can derive from any type in D2, including non-integer types.  I think the request is reasonable for D2, where enum wears many hats.

-Steve