Any alternative's to C's "#define"?

I’m looking for an alternative to C/C++'s “#define” preprocessor directive so I can keep my code optimized and clean. Inline variables are close to what I am looking for, but they can only accept constants. Macros also seem close to what I want, but my knowledge on them is pretty lacking (I haven’t found too much documentation on it), and from what I’ve seen, they have to be written as a function and can’t be written as a variable, making my code not as readable. If there’s any alternatives that’ll both improve performance and readability, please inform me. Thanks.

Could you provide an example of what you are trying to achieve?

I’m just gonna throw some resources on at ya for now and hope it’s helpful.

Maybe you are looking for something like conditional compilation? Conditional Compilation - Haxe - The Cross-platform Toolkit

Also if you haven’t seen it there are some samples of macros here: Haxe Macros articles overview - Macros - Haxe programming language cookbook

Another option might be meta data and reflection. Metadata - Haxe - The Cross-platform Toolkit

You mention inline variables so I’m just gonna throw some examples on Abstracts your way: Haxe Abstract types articles overview - Abstract types - Haxe programming language cookbook

I apologize if I’m not being specific enough for what I’m looking for, but this is the best example I can give in C:

#include <stdio.h>

int x = 64;
#define formula x * 3.14

int main()
{
    printf("%f", formula);
    return 0;
}

After a bit of experimentation, I found out something closer to what I was looking for was static variables with an inline getter function:

class Test
{
    public static var x:Int = 64;
    public static var formula(get, never):Float;

    public static inline function get_formula()
    {
      	return x * 3.14;
    }

    static function main()
    {
      	trace(formula);
    }
}

At this point though, I may as well just use an inline function instead unless there was a way to one-line it.

In all honesty, I should probably just stick to inline variables and inline functions for now, since they aren’t that unreadable and can still provide the optimizations I need. Besides, Haxe isn’t C anyways, and that’s mainly why I like it, I just think C’s “#define” is nice since it’s quick, easy, and can be done in a single line without looking messy.

Thank you for providing an example!

Yes, what you are looking for are inline functions.

You are currently limiting your code to be limited to external state which is unnecessary when you can have the inline function take in an argument.

class Test {
  static inline function formula(x :Int) {
		return x * 3.14;
  }
  static function main() {
    var x = 3;
    trace(formula(3));
    trace("Haxe is great!");
  }
}

If we compile the above to javascript we can see that the Haxe compiler will completely resolve the function and output:

console.log("Test.hx:7:",9.42); //Here is the result of our inline function
console.log("Test.hx:8:","Haxe is great!");

No runtime calculation required, pretty cool!

#define formula x * 3.14

As an aside, inline functions like this in C are dangerous since they are not hygienic. It requires the programmer to be aware of any unintentional redefinitions of x when formula is substituted. You can read up more on the hygiene problem here: Hygienic macro - Wikipedia

Thanks for the info and all the great resources you’ve given. I’ll make sure to use these tips where necessary to improve my code. Have a good day.

1 Like