Try/catch not working when building an expression - Any workaround ?

47 views
Skip to first unread message

Francis Bourre

unread,
Jun 27, 2016, 1:01:07 AM6/27/16
to Haxe
Hi guys,

is there a way to catch an error when building an expression ?

try
{
e = macro { new $typePath( $a { constructorArgs } ); };
}
catch( e : Dynamic )
{
trace( e );
}

In the example above, errors are never caught.

Thanks in advance for your feedback.

Juraj Kirchheim

unread,
Jun 27, 2016, 1:24:22 AM6/27/16
to haxe...@googlegroups.com
There is no way that code might result in an exception. The generated expression might cause a compiler error down the road. If that's what you want to check against, something like this would work:

  var expr = macro { new $typePath( $a { constructorArgs } ); };
  try Context.typeof(expr)
  catch (errod:Dynamic) trace(error);

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.

Francis Bourre

unread,
Jun 27, 2016, 1:52:09 AM6/27/16
to Haxe
Thanks for your quick feedback!

So, if I want to intercept some compiler errors (ie: Constructor argument missing, and/or wrong argument type...), it means that I should re-implement compiler's logic on my side before building the expression ?

Quick example. I'm building this little piece of code:
<receiver id="receiver" type="hex.ioc.parser.xml.mock.MockReceiverModule"/>
<proxy id="eventProxy" type="hex.event.EventProxy">
        <argument ref="receiver"/>
</proxy>

With:
85 e = macro { new $typePath( $a { constructorVO.constructorArgs } ); };
86 try Context.typeof( e )
87  catch (error:Dynamic) trace(error);

NB: EventProxy signature is new( scope : Dynamic, method : Dynamic ) 

So I got:
hexIoC/src/hex/compiler/factory/ClassInstanceFactory.hx:85: characters 16-69 : Not enough arguments, expected method:Dynamic
hexIoC/src/hex/compiler/factory/ClassInstanceFactory.hx:87: Unknown identifier : receiver

I'm trying to figure out a nice way to intercept these kind of errors before the compiler to mark/position them inside the DSL file.

Thanks again.

Juraj Kirchheim

unread,
Jun 27, 2016, 2:10:45 AM6/27/16
to haxe...@googlegroups.com
On Mon, Jun 27, 2016 at 7:52 AM, Francis Bourre <peter...@gmail.com> wrote:
Thanks for your quick feedback!

So, if I want to intercept some compiler errors (ie: Constructor argument missing, and/or wrong argument type...), it means that I should re-implement compiler's logic on my side before building the expression ?

Nope, that's not what I was saying. My point is that compiler errors are not thrown (because they are not exceptions). You can turn some compiler errors into an exceptions under some circumstances, e.g. calling Context.typeof on an "invalid" expression will result in an exception.
 
Quick example. I'm building this little piece of code:
<receiver id="receiver" type="hex.ioc.parser.xml.mock.MockReceiverModule"/>
<proxy id="eventProxy" type="hex.event.EventProxy">
        <argument ref="receiver"/>
</proxy>

With:
85 e = macro { new $typePath( $a { constructorVO.constructorArgs } ); };
86 try Context.typeof( e )
87  catch (error:Dynamic) trace(error);

NB: EventProxy signature is new( scope : Dynamic, method : Dynamic ) 

So I got:
hexIoC/src/hex/compiler/factory/ClassInstanceFactory.hx:85: characters 16-69 : Not enough arguments, expected method:Dynamic
hexIoC/src/hex/compiler/factory/ClassInstanceFactory.hx:87: Unknown identifier : receiver

I'm trying to figure out a nice way to intercept these kind of errors before the compiler to mark/position them inside the DSL file.

Oh, ok. Then that's what you should have said :)

In this case, the solution is to build a `Position` that points into the original XML file (using `Context.makePosition`) and then to use `@:pos` to apply it to the reified expression like so:

  var e = macro @:pos(POSITION_IN_XML) { new $typePath( $a { constructorVO.constructorArgs } ); };

Then the compiler will report the error at the right location (i.e. in the XML file).

Best,
Juraj

Francis Bourre

unread,
Jun 27, 2016, 2:14:40 AM6/27/16
to Haxe
Wow!!! That's magic. :) 
What a nice hidden gem! Thank you so much!!!

Francis Bourre

unread,
Jun 27, 2016, 2:30:20 AM6/27/16
to Haxe
And it works like a charm.

Francis Bourre

unread,
Jun 29, 2016, 12:59:33 AM6/29/16
to Haxe
Hi, I'm facing an unexpected behavior.

If I build this kind of expression:
var e = macro @:pos( filePosition ) $p { refs };

The position is set to:
#pos(C:\HaxeToolkit\haxe\std/haxe/macro/MacroStringTools.hx:73: characters 82-87)
And is not overriden by the passed position ( filePosition ).

Any idea/workaround for this case ?

Thanks in advance for your lights.

Francis Bourre

unread,
Jun 29, 2016, 1:02:38 AM6/29/16
to Haxe
Ok, found it, my bad. It was simple. :)
e.pos = filePosition;

Sorry!
Reply all
Reply to author
Forward
0 new messages