Null Or Default Construct?

29 views
Skip to first unread message

Ashiq A.

unread,
Feb 9, 2016, 5:04:56 PM2/9/16
to haxe...@googlegroups.com
Hi,

In languages like C# and Javascript, I can write code like:

var c:SomeObject = ...
var x:name = c.name || "Unnamed"

If c.name is non-null, I get the value from c.name; if it is null, I get the value "Unnamed"

Is it possible to write a similar construct in Haxe? 

This is not the same as an optional function argument, which is all I could find in the docs.

--Ashiq

Philippe Elsass

unread,
Feb 9, 2016, 5:21:33 PM2/9/16
to Haxe
I think we would all like a `??` operator like in C#. Haxe type system however wouldn't allow `||`.

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



--
Philippe

Ashiq A.

unread,
Feb 9, 2016, 5:33:08 PM2/9/16
to haxe...@googlegroups.com
Either syntax is cool as far as I'm concerned.  Right now, I have to write:

var x:String = c.name != null ? c.name : "Unnamed";

If I have to do this several times, it gets tedious; much easier to write:

var x:String = c.name || "Unnamed";
var x:String = c.name ?? "Unnamed";

It sounds like I'm not the first person to ask for this, and that there is (sadly) no way to do this yet. Is there a macro I can use for this or a library, or something that'll give me shorter syntax than the ternary operator?




Marcelo de Moraes Serpa

unread,
Feb 9, 2016, 6:06:20 PM2/9/16
to haxe...@googlegroups.com
Open an issue here: https://github.com/HaxeFoundation/haxe/issues, and resume the discussion there. Higher chances of it being considered by the core team.

Not what you're looking for, but related: https://github.com/HaxeFoundation/haxe/issues/4427

Ashiq A.

unread,
Feb 9, 2016, 6:15:03 PM2/9/16
to haxe...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages