Page 1 of 1

Const specific for certain variable only.

Posted: Sun Jun 11, 2017 3:45 pm
by Bugala
I was wondering how hard would it be to make Consts that would only work with certain Variables?

An Example of what I am after:

Code: Select all

Const myvariable #ON = 1

Code: Select all

myvariable = #ON
depbugprint(myvariable)

mysecondvariable = #ON
debugprint(mysecondvariable)
Result:
- 1
- error


Idea behind this is Triple reason.
First of all, to avoid hitting reserved words. For example using something like #ON or #OFF is soemthing that I would be afraid of using since they could easily become reserved in future Hollywood versions (unless they already are).

Second is, that there can sometimes be situations when it could be handy, two silly examples that will however give you the idea:

Code: Select all

const switch1 #ON = 1
const switch2 #ON = 0

switch1 = #ON
switch2 = #ON

Code: Select all

const defcon #ALARM = 5
const microwave #ALARM = 0

table_checkforalerts = { defcon = 1, microwave = 100 }

Repeat

defcon=defcon+1
microwave=microwave-1

alert = false
foreach (table_checkforalerts, function (key, item)
                                                     if key=#ALERT then alert = true
                                              endfunction)
until alert=true
debuprint("ALERT!")
And third is that something like #ON and #OFF could be useful at many places, but their numerical values might differ, for example if there are more states than just ON and OFF.

Re: Const specific for certain variable only.

Posted: Mon Jun 12, 2017 8:03 pm
by SamuraiCrow
It sounds like you are trying to describe enumerations. C allows some rather complicated uses and abuses of the "enum" keyword. Or perhaps making a const table would be more appropriate using a perfect hash function of some sort. http://cmph.sourceforge.net might be one type of that.