Some questions about Haxe (Compiler warnings, non-nullable default params, map key equality)

106 views
Skip to first unread message

Aleph Null

unread,
May 18, 2015, 2:42:06 AM5/18/15
to haxe...@googlegroups.com
Hi everyone, I have a few questions:


1) Is the compiler able to warn about potential mistakes like variable1 == variable2; ? Here the value of the boolean expression isn't used, which suggests this may not be what the programmer intended to do. (In my case, I intended variable1 = variable2, spent 2 hours last night looking for my goof.)


2) Regarding non-nullable default parameters. Consider the following code:

class Main {
    static function main()     {      
        defaultParams(1);
    }
      
    static function defaultParams(b = true, i = 1) { }
}

This compiles fine on Neko and C++, but on Flash the compiler gives the error "Cannot skip non-nullable argument b". The error goes away if the compiler is able to infer b as having type Null<Bool> instead of Bool, through ?b = true or b:Null<Bool> = true. But what's strange is that:

- like Flash, C++ is also a static target, yet compiling to C++ doesn't produce an error
- omitting all optional params until the end is fine, e.g. defaultParams(); defaultParams(false); will compile. But skipping some non-nullable params and providing some later on is not.

So what exactly is the compiler doing?


3) I'd like a Map whose keys are general objects, and to return the same value when two objects having the same data (but not necessarily with the same reference) are queried. But Map only does this for Strings.

What I'm currently doing is

typedef MyObjData = {
    var data1:Data1;
    var data2:Data2;
}


@:forward(data1, data2)
abstract MyObj(MyObjData) from MyObjData to MyObjData {
    ...
    @:to
    inline public function toID():String {
        ... //return string encoding of the data
    }
}

which allows me to use a Map<String, MyValue> via array access on MyObj indices. But is there a way to use a Map<MyObj, MyValue> instead and still have keys be compared by value instead of by reference?

Haxe's == operator compares Bools, Ints, Floats and Strings by value, and all other objects by reference, does it have something to do with this? I've tried overloading == to compare MyObjs as well, but Map still compares them by reference.

Benjamin Dubois

unread,
May 18, 2015, 7:25:51 AM5/18/15
to haxe...@googlegroups.com
Just a part of the answer

--
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.

Simon Krajewski

unread,
May 18, 2015, 7:37:53 AM5/18/15
to haxe...@googlegroups.com
Am 18.05.2015 um 08:42 schrieb Aleph Null:
> Hi everyone, I have a few questions:
>
>
> 1) Is the compiler able to warn about potential mistakes like
> variable1 == variable2; ? Here the value of the boolean expression
> isn't used, which suggests this may not be what the programmer
> intended to do. (In my case, I intended variable1 = variable2, spent 2
> hours last night looking for my goof.)

The static analyzer can detect these cases, but it's currently not
enabled by default because there are too many false positives. Try
compiling with -D analyzer -D analyzer-check-has-effect and see if it
warns you about something like that.

Simon

Aleph Null

unread,
May 19, 2015, 5:44:46 AM5/19/15
to haxe...@googlegroups.com
Thanks for the replies.


The static analyzer can detect these cases, but it's currently not
enabled by default because there are too many false positives. Try
compiling with -D analyzer -D analyzer-check-has-effect and see if it
warns you about something like that.

Cool, I got a warning about variable1 == variable2; . Unfortunately together with lots of others from the libs I'm using.

One way to manage the warning spam could be a filter that restricts to only warnings arising from e.g. files in a particular directory (and subdirectories). (Maybe that's a feature for the IDEs? Currently FlashDevelop lets you search for and highlight keywords in the compiler's console output, but that's not convenient for this.)

Reply all
Reply to author
Forward
0 new messages