1. Forward Referencing

Will Euphoria 4.2.0 have support for forward referencing in enums?

This feature would come in handy for wrapping libraries.

new topic     » topic index » view message » categorize

2. Re: Forward Referencing

I'm intrigued: I cannot think of any reason why/where that would be useful, can you give an example?

new topic     » goto parent     » topic index » view message » categorize

3. Re: Forward Referencing

petelomax said...

I'm intrigued: I cannot think of any reason why/where that would be useful, can you give an example?

Well here is one example.

public enum type SDL_Keymod 
	SDL_KMOD_NONE = 0x0000, 
	SDL_KMOD_LSHIFT = 0x0001, 
	SDL_KMOD_RSHIFT = 0x0002, 
	SDL_KMOD_LCTRL = 0x0040, 
	SDL_KMOD_RCTRL = 0x0080, 
	SDL_KMOD_LALT = 0x0100, 
	SDL_KMOD_RALT = 0x0200, 
	SDL_KMOD_LGUI = 0x0400, 
	SDL_KMOD_RGUI = 0x0800, 
	SDL_KMOD_NUM= 0x1000, 
	SDL_KMOD_CAPS = 0x2000, 
	SDL_KMOD_MODE = 0x4000, 
	SDL_KMOD_SCROLL = 0x8000, 
	 
	SDL_KMOD_RESERVED = SDL_KMOD_SCROLL 
end type 
 
--These should be part of the enum, but since forward referencing isn't allowed in Euphoria, I had to declare them outside of the enum 
 
public constant SDL_KMOD_CTRL = or_all({SDL_KMOD_LCTRL,SDL_KMOD_RCTRL}) 
public constant SDL_KMOD_SHIFT = or_all({SDL_KMOD_LSHIFT,SDL_KMOD_RSHIFT}) 
public constant SDL_KMOD_ALT = or_all({SDL_KMOD_LALT,SDL_KMOD_RALT}) 
public constant SDL_KMOD_GUI = or_all({SDL_KMOD_LGUI,SDL_KMOD_RGUI}) 

Maybe its not the biggest deal, but it would be nice to have.

new topic     » goto parent     » topic index » view message » categorize

4. Re: Forward Referencing

Icy_Viking said...

Will Euphoria 4.2.0 have support for forward referencing in enums?

This feature would come in handy for wrapping libraries.

I think the "forward referencing" message given by the interpreter is a bit of a red herring. It's more to do with enums only allowing literal numeric values, as opposed to constants which have always allowed any value and computing values at runtime. The docs specifically state:

docs said...

You can change the value of any one item by assigning it a numeric value. Enums can only take numeric values. You cannot set the starting value to an expression or other variable. Subsequent values are always the previous value plus one, unless they too are assigned a default value.

Icy_Viking said...

Well here is one example.

Maybe its not the biggest deal, but it would be nice to have.

In this case I would simply pre-compute the OR'd values and include them in the enum block.

public enum type SDL_Keymod 
	SDL_KMOD_NONE = 0x0000, 
	SDL_KMOD_LSHIFT = 0x0001, 
	SDL_KMOD_RSHIFT = 0x0002, 
	SDL_KMOD_LCTRL = 0x0040, 
	SDL_KMOD_RCTRL = 0x0080, 
	SDL_KMOD_LALT = 0x0100, 
	SDL_KMOD_RALT = 0x0200, 
	SDL_KMOD_LGUI = 0x0400, 
	SDL_KMOD_RGUI = 0x0800, 
	SDL_KMOD_NUM = 0x1000, 
	SDL_KMOD_CAPS = 0x2000, 
	SDL_KMOD_MODE = 0x4000, 
	SDL_KMOD_SCROLL = 0x8000, 
	SDL_KMOD_SHIFT = 0x0003, -- SDL_KMOD_LSHIFT | SDL_KMOD_RSHIFT 
	SDL_KMOD_CTRL = 0x00C0, -- SDL_KMOD_LCTRL | SDL_KMOD_RCTRL 
	SDL_KMOD_ALT = 0x0300, -- SDL_KMOD_LALT | SDL_KMOD_RALT 
	SDL_KMOD_GUI = 0x0C00, -- SDL_KMOD_LGUI | SDL_KMOD_RGUI 
	SDL_KMOD_RESERVED = SDL_KMOD_SCROLL 
end type 

I typically use the Windows calculator "Programmer" mode for this, but you could also do it in code, e.g.

printf(1, "SDL_KMOD_SHIFT = 0x%04x\n", or_bits(SDL_KMOD_LSHIFT,SDL_KMOD_RSHIFT) ) 

Not entirely related, but I'd also suggest avoiding or_all() when you only have two values, as or_bits() is a builtin and or_all() comes from std/math.e. It just makes for cleaner code and avoids unnecessary include files (assuming you don't need std/math.e elsewhere).

-Greg

new topic     » goto parent     » topic index » view message » categorize

5. Re: Forward Referencing

ghaberek said...
Icy_Viking said...

Will Euphoria 4.2.0 have support for forward referencing in enums?

This feature would come in handy for wrapping libraries.

I think the "forward referencing" message given by the interpreter is a bit of a red herring. It's more to do with enums only allowing literal numeric values, as opposed to constants which have always allowed any value and computing values at runtime. The docs specifically state:

docs said...

You can change the value of any one item by assigning it a numeric value. Enums can only take numeric values. You cannot set the starting value to an expression or other variable. Subsequent values are always the previous value plus one, unless they too are assigned a default value.

Icy_Viking said...

Well here is one example.

Maybe its not the biggest deal, but it would be nice to have.

In this case I would simply pre-compute the OR'd values and include them in the enum block.

public enum type SDL_Keymod 
	SDL_KMOD_NONE = 0x0000, 
	SDL_KMOD_LSHIFT = 0x0001, 
	SDL_KMOD_RSHIFT = 0x0002, 
	SDL_KMOD_LCTRL = 0x0040, 
	SDL_KMOD_RCTRL = 0x0080, 
	SDL_KMOD_LALT = 0x0100, 
	SDL_KMOD_RALT = 0x0200, 
	SDL_KMOD_LGUI = 0x0400, 
	SDL_KMOD_RGUI = 0x0800, 
	SDL_KMOD_NUM = 0x1000, 
	SDL_KMOD_CAPS = 0x2000, 
	SDL_KMOD_MODE = 0x4000, 
	SDL_KMOD_SCROLL = 0x8000, 
	SDL_KMOD_SHIFT = 0x0003, -- SDL_KMOD_LSHIFT | SDL_KMOD_RSHIFT 
	SDL_KMOD_CTRL = 0x00C0, -- SDL_KMOD_LCTRL | SDL_KMOD_RCTRL 
	SDL_KMOD_ALT = 0x0300, -- SDL_KMOD_LALT | SDL_KMOD_RALT 
	SDL_KMOD_GUI = 0x0C00, -- SDL_KMOD_LGUI | SDL_KMOD_RGUI 
	SDL_KMOD_RESERVED = SDL_KMOD_SCROLL 
end type 

I typically use the Windows calculator "Programmer" mode for this, but you could also do it in code, e.g.

printf(1, "SDL_KMOD_SHIFT = 0x%04x\n", or_bits(SDL_KMOD_LSHIFT,SDL_KMOD_RSHIFT) ) 

Not entirely related, but I'd also suggest avoiding or_all() when you only have two values, as or_bits() is a builtin and or_all() comes from std/math.e. It just makes for cleaner code and avoids unnecessary include files (assuming you don't need std/math.e elsewhere).

-Greg

Thanks for the tips, Greg. Helpful advice as always.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu