Search code examples
autocompletemacrosexpressionhaxeusing

Limit autocompletion of macro function when used as a static extension to multiple types


When using a static macro function, meant to be used as a static extension, how can I limit types of variables that will get this function on an autocompletion list? Caveat: I know I can use ExprOf<T> but I need this for multiple types to check inside my macro if expr unifies with a specific abstract.


Solution

  • Besides leveraging the type system to perform that unification by itself, if possible, you might be able to use a temporary abstract exclusively for this "filtering".

    // exclusively for static extension x autocomplete
    private abstract PseudoType(Dynamic)
      from ActualType1
      from ActualType2
      from ActualType3 {}
    
    [...]
    
    public static macro function myMacro(value:ExprOf<PseudoType>}
    {
      // ExprOf doesn't do anything other than help with autocomplete
      // do actual unification here
      // return the appropriate result
    }
    

    [EDIT] here's an example (live on Try Haxe/alt.):

    Macro.hx:

    import haxe.macro.Expr;
    
    private abstract PseudoType(Dynamic)
      from String
      from Int
      from { val:Float } {}
    
    class Macro {
      public static macro function magic(value:ExprOf<PseudoType>)
      {
         return macro Std.string($value);
      }
    }
    

    Test.hx:

    using Macro;
    
    class Test {
      static function main()
      {
        trace("Haxe is great!".magic());
        trace(42.magic());
        trace({ val : 3.14 }.magic());
      }
    }