Detect used argument type at compile time

76 views
Skip to first unread message

Marcin Kotz

unread,
Nov 28, 2016, 4:17:17 AM11/28/16
to Haxe
I'm looking for a way to solve a specific problem

With a classic evet handler like addEventListener("click", onClick) I would like to check at compile time, if the event type was provided by an inline string or by a more safe static inline var, so:
addEventListener("click", onClick) // would provide a warning at compile time "please use static inline var for safe event attachement"
and
addEventListener(UIEvent.CLICK, onClick) // would be ok :)

Is there any way to do this at compile time? I can solve it with an abstract, but at runtime :/

Juraj Kirchheim

unread,
Nov 28, 2016, 4:25:03 AM11/28/16
to haxe...@googlegroups.com
Here's a solution: https://gist.github.com/back2dos/84ff9caf2c2c7bdd4789

If you wish to prevent direct use of strings, you will just need to remove the `from String` here: https://gist.github.com/back2dos/84ff9caf2c2c7bdd4789#file-eventtype-hx-L3

Best,
Juraj

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Marcin Kotz

unread,
Nov 28, 2016, 5:49:20 AM11/28/16
to Haxe
Thanks! That is something I also had in mind, but:
- this solution will not allow me only to warn at compile time
- this solution will not convert the strictly typed event to string

I'm rather looking for a general macro based solution, I think ;)

Juraj Kirchheim

unread,
Nov 28, 2016, 6:06:41 AM11/28/16
to haxe...@googlegroups.com
On Mon, Nov 28, 2016 at 11:49 AM, Marcin Kotz <er4...@gmail.com> wrote:
Thanks! That is something I also had in mind, but:
- this solution will not allow me only to warn at compile time

True, although I really question the value of warnings. People have two ways of dealing with them:

1. Fix all warnings - in which case it could simply be a compiler error
2. Ignore all warnings - in which case the warning has no value

Whatever solution you have, you should just have a compiler flag to either let compilation fail or let it succeed silently. Anything in between is just noise ;)
 
- this solution will not convert the strictly typed event to string

I'm not sure what you mean by that. The solution I proposed will still use ordinary strings at runtime. It also adds safety in that it checks whether the event type and the event handler are compatible. If you just check whether the first element is a static inline var you get some protection from typos, but you can still wind up having ProgressEvent dispatched to IOErrorEvent handlers.

I'm rather looking for a general macro based solution, I think ;)

Sure, you can do that, but I think a macro is a bit over the top here.

Best,
Juraj

Marcin Kotz

unread,
Dec 16, 2016, 6:59:09 AM12/16/16
to Haxe
I've managed to get an example working:

package;

import haxe.macro.Expr;
import haxe.macro.Context;
import haxe.PosInfos;

class Tset {
   
static private inline var DERP:String = "DERP";

   macro static public function print(string:ExprOf<String>):Expr{
     
var expr = macro trace("from code " + $string);

      switch(string.expr){
         
case EConst(c): switch (c){
           
case CString(str): //url is a constant String
               Context.warning('Please do not use inline strings, define them as inline instead', Context.currentPos());
            default:
         
}
         
default:
     
}

     
return expr;
   }

   
public static function main() {
      trace
("hello");

      print("lolz");
      print(DERP);
   }
}
Reply all
Reply to author
Forward
0 new messages