static/dynamic null primitives from a library

32 views
Skip to first unread message

Raoul Duke

unread,
Jul 16, 2015, 4:26:29 AM7/16/15
to haxe...@googlegroups.com
hi,

I'm using a library. I want to run it across all platforms. It has a
variable of type Int I want to check. Looking at the code, the
variable is not explicitly given a default value.

I think that means on a 'dynamic' platform I can check vs. null.

But on a 'static' platform I guess I have to check vs. 0 and the
compiler will explicitly error if I have a null check.

Do I have to #if any code that uses the variable? What else might I
do? Hopefully I'm just befuddling myself and there's some simple
approach.

thanks.

Juraj Kirchheim

unread,
Jul 16, 2015, 4:33:45 AM7/16/15
to haxe...@googlegroups.com
Always initialize the variables, then you'll know what value they have: http://try.haxe.org/#D2a6D (you can just use 0 of course)

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.

Raoul Duke

unread,
Jul 16, 2015, 4:36:16 AM7/16/15
to haxe...@googlegroups.com
> Always initialize the variables

but I am using a library. Unless I want to fork it and fix that one...
oh, and also find every other bleedin' place there was an
un-explicitly-initialized value... which I really do not want to do...

Juraj Kirchheim

unread,
Jul 16, 2015, 4:50:25 AM7/16/15
to haxe...@googlegroups.com
On Thu, Jul 16, 2015 at 10:36 AM, Raoul Duke <rao...@gmail.com> wrote:
> Always initialize the variables

but I am using a library. Unless I want to fork it and fix that one...

Well, either the library is actively maintained, then surely you can make a pull request. Or it is not, then indeed it is time for you to fork. The whole point of 3rd party libraries is to make life simpler, not to give you something to work around ;)
 
oh, and also find every other bleedin' place there was an
un-explicitly-initialized value... which I really do not want to do...

You can probably use Context.onGenerate to determine that. 

Alternatively you can use Compiler.addGlobalMetaData to add a @:build directive to each class in said library, that makes sure ints are initialized to 0, floats to 1/0 (since NaN is not a constant) and bools to false. That does avoid the necessity of forking, but unless you are using the compilation server, you're sacrificing too much compile time.

Best,
Juraj

Raoul Duke

unread,
Jul 16, 2015, 4:57:43 AM7/16/15
to haxe...@googlegroups.com
Hm. Oh well. That's kind of overkill for me. How about the compiler do
this for us, instead of each and every person using Haxe having to do
this... To every file... All the time...

Basically it seems to me the whole "cross platformness" quickly
becomes a rather leaky abstraction. Yes, I undersatand that's the
nature of "cross platformness" since you end up between a rock and a
hard place...

Hugh

unread,
Jul 17, 2015, 1:16:18 AM7/17/15
to haxe...@googlegroups.com
The "Null<Int>" construct is designed to allow cross-platform abstraction to work.  If your library used this, everything would work the same.
Alternatively, if the library actually wanted things to start at zero, it could do this in a cross-platform way.

But actually, I think neko could initialize the floats to 0 (or Nan I guess) and this would be pretty beneficial for everyone.
Alternatively, you could write a macro to inject initilizers on dynamic platforms for all variables along the lines of what Juraj suggests.  If this proved popular, it could be lifted from a macro to a compiler function for additional speed.

Practically, you could use:
   var x = Default.get( somelibraryclass.funkyVariable, 22.0 )
which might be nicer than "#if static" everywhere.

Hugh

Raoul Duke

unread,
Jul 17, 2015, 3:24:28 AM7/17/15
to haxe...@googlegroups.com
> But actually, I think neko could initialize the floats to 0 (or Nan I guess)
> and this would be pretty beneficial for everyone.

Cross-platform just seems like a proverbial path paved with good
intentions leading to wailing and gnashing of teeth. The fact that
e.g. Flash-static Floats default to NaN whereas on the other statics
it is 0 is an example of the oy-veh-i-give-up-ness of it all.

> Alternatively, you could write a macro to inject initilizers on dynamic
> platforms for all variables along the lines of what Juraj suggests. If this
> proved popular, it could be lifted from a macro to a compiler function for
> additional speed.

> Practically, you could use:
> var x = Default.get( somelibraryclass.funkyVariable, 22.0 )
> which might be nicer than "#if static" everywhere.

hm. hmmmm...
Reply all
Reply to author
Forward
0 new messages