Why we can't use '$' as method name?

67 views
Skip to first unread message

Freewind

unread,
Jan 24, 2013, 1:41:36 AM1/24/13
to haxe...@googlegroups.com
Hi, all:

I know `$` is used in macro methods and have may have special meaning in strings. But that will it convenient to work with javascript(especially with angularjs, which has a lot of methods and fields starts with "$").

Is it possible to make it easier? In my code, there is a lot of:

    Reflect.field(scope, "$eval")(...);
    Reflect.field(scope, "$watch")(...);

Which really make the code hard to read.

Nicolas Cannasse

unread,
Jan 24, 2013, 4:30:39 AM1/24/13
to haxe...@googlegroups.com
Le 24/01/2013 07:41, Freewind a �crit :
> Hi, all:
>
> I know `$` is used in macro methods and have may have special meaning in
> strings. But that will it convenient to work with javascript(especially
> with angularjs, which has a lot of methods and fields starts with "$").

Most of the platforms does not support $ as an identifier, so that would
require to rename them which would lead to issues as soon as you use
Dynamic/Reflection.

Your angularjs wrapper could solve that by having inline not-dollar
methods that translate into JS dollar calls :

inline function doSomething() {
return untyped this["$doSomething"]();
}

Best,
Nicolas

Freewind

unread,
Jan 25, 2013, 4:33:22 AM1/25/13
to haxe...@googlegroups.com
Is it possible to provide an simple syntax for "Reflect.field(...)". 

E.g.

    scope["$on"]

which means:

    Reflect.field(scope, "$on")

I really don't like my code code full of:

    Reflect.field(scope, "$on")('routeChangeStart', function(next, current) {}

Or:

    on(scope)('routeChangeStart', function(next, current) {}

    private inline static function on(scope:Dynamic) {
        return untyped scope["$on"];
    }


On Thursday, January 24, 2013 5:30:39 PM UTC+8, Nicolas Cannasse wrote:
Le 24/01/2013 07:41, Freewind a �crit :

Postite

unread,
Jan 25, 2013, 4:44:34 AM1/25/13
to haxe...@googlegroups.com
> Is it possible to provide an simple syntax for "Reflect.field(...)".


using Reflect;

var pop={pif:2,paf:4};
pop.setField("pif",2);
Trace( pop.field("paf");
//is the same as
Reflect.setField(pop."pif",2);
Trace( pop.field("paf");

Is this the kind of shorcut your asking for ?



Postite

unread,
Jan 25, 2013, 4:46:19 AM1/25/13
to haxe...@googlegroups.com
Oups I did it wrong in last line of my code but you surely corrected by yourself.

Not easy to write code from an Ipad

Heinz Hölzer

unread,
Jan 25, 2013, 4:53:28 AM1/25/13
to haxe...@googlegroups.com
You can easily type your scope Object with an extern interface or a typedef:

extern interface Scope {
     @:native("$on") public function on (name:String, f: String -> String);
}

and then:

class XY {
  function myFunction (scope:Scope) {
    scope.on(routeChangeStart', function(next, current) {}); 
    ...

clemos

unread,
Jan 25, 2013, 4:58:01 AM1/25/13
to haxe...@googlegroups.com
I've recently tried that, and as far as I can tell, @:native works only for class names, not for field names (or is it a bug ?).
I'd also like to see it working...

If you only target javascript, inline methods + bracket access are fine :
public inline function _on( name : String ) return untyped this['$on']( name )

Regards,
Clément


Jason O'Neil

unread,
Jan 25, 2013, 6:04:02 AM1/25/13
to haxe...@googlegroups.com
I've found this trick doesn't work if your object is typed as Dynamic:

The compiler will assume "setField" is a method on the dynamic "pop" object, which the compiler doesn't know the type of, and so it will not substitute it for `Reflect.setField(pop, "pif", 2)` but will instead leave it as `pop.setField("pif", 20)`, causing a runtime error...

It will work however if your object is not typed as "Dynamic"


Simon Krajewski

unread,
Jan 25, 2013, 6:14:34 AM1/25/13
to haxe...@googlegroups.com
Am 25.01.2013 12:04, schrieb Jason O'Neil:
> I've found this trick doesn't work if your object is typed as Dynamic:
>
> The compiler will assume "setField" is a method on the dynamic "pop"
> object, which the compiler doesn't know the type of, and so it will
> not substitute it for `Reflect.setField(pop, "pif", 2)` but will
> instead leave it as `pop.setField("pif", 20)`, causing a runtime error...
>
> It will work however if your object is not typed as "Dynamic"

Works for me on both 2.10 and trunk. Can you post an issue with a
reproducible example?

Simon

Jason O'Neil

unread,
Jan 25, 2013, 6:34:36 AM1/25/13
to haxe...@googlegroups.com

Ah when I ran into it I assumed it was the expected behaviour. I've got an example in my code somewhere... When I find it I'll double check if it was my fault and submit an issue if needed

Jason

Reply all
Reply to author
Forward
0 new messages