3
votes

I'm looking for a way to make Jalv2-like pseudo variables in C using the C18 compiler. A pseudo variable is something that acts like a variable but actually is a function.

In Jalv2, it's possible to make a pseudo variable like this:

function the_var'get() return byte is
    -- some code
end function

procedure the_var'set(byte in value) is
    -- some code
end procedure

Now one can read and write to the_var, while actually those functions are executed:

the_var = 0x40         -- actually executes the_var'set(0x40)
doSomething(the_var)   -- actually executes doSomething(the_var'get)

Is there something similar for C?

1
It sounds like you are asking for "getters and setters" in C. No, this is not possible.Dietrich Epp
@DietrichEpp is there a workaround?user1544337
Sure, just call the functions explicitly.Dietrich Epp
@DietrichEpp not that it looks like the last code example?user1544337
@modifiablelvalue I didn't know yet if C the feature had, so I asked.user1544337

1 Answers

3
votes

No, it's not possible with C. It's not even possible with the preprocessor. The = operator always does the exact same thing in C, and there are no ways to customize it.

If you want to do things like that, you'll have to pick a different language. Like C++, for example, which lets you override operator = (for a setter) and operator int (for a getter).