Macro that expands to a block expression

Hi

I am trying to make eaiser to use optional arguments with default values non constant, I am trying my luck with macros however i couldnt find in the documentation how to generate a block expr from a string code this is my (almost) pseudo code

class Macros {
    static public macro function default_val(name:Expr,value:Expr) {
        var out:Expr
        names=if (names==null){
            ["Monsey", "Kiamesha", "Monroe", "Square", "Spring valley", "Woodridge"];
        } else {
            names;
        }
    }
}
class Macros {
    static public macro function default_val(name:Expr,value:Expr) {
        var out:Expr;
        return macro $name=if ($name==null){
            $value;
        } else {
            $name;
        }
    }
}

This is how I am trying to use it, what could be wrong?

public static function Tsp(?names: Array<String>, ?metrics: Array<Array<Float>>, ?after: Map<String,Int>): Situation {
    var cnt = 0;
    Macros.default_val(names,["Monsey", "Kiamesha", "Monroe", "Square", "Spring valley", "Woodridge"]);

I just managed to compile my code, and it did worked, and the compiler even optimized it.

What could I say, macros are not in the dark

maybe you could add this example to the cookbook

Thanks

2 Likes