-
-
Notifications
You must be signed in to change notification settings - Fork 661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Null coalescing operator #10428
Null coalescing operator #10428
Conversation
Check this out: var count = 0;
function call() {
count++;
return "1";
}
function main() {
var a = call() ?? "default";
trace(count); // oh oh
} |
I've made a fix. Didn't really mean to push that to your remote but oh well. |
I'm ok with any changes in my branches, thank you |
I think let iftype = WithType.WithType(e2.etype,None) in
make_if_then_else ctx e e1 e2 if_type p But there is null-safety error, if i pass @:nullSafety(StrictThreaded)
class Main {
static function main() {
final nullInt:Null<Int> = null;
final notNull = nullInt ?? 2;
final notNull2:Int = nullInt ?? 2;
$type(notNull); // should be Int
$type(notNull2); // should be Int
final isNull = nullInt == null ? 2 : nullInt;
final isNull2:Int = nullInt == null ? 2 : nullInt;
$type(isNull); // should be Null<Int>
$type(isNull2); // should be Int (with null-safety error?)
eq(nullInt == null ? 2 : nullInt, 2); // should be null-safety error?
eq(nullInt ?? 2, 2); // but it is there with if_type based on e2.etype
}
static function eq<T>(a:T, b:T):Void {
trace(a == b);
}
} If @:mergeBlock {
var tmp = {
nullInt;
};
if (tmp != null) tmp else {
2;
};
} and then counts as nullable because of |
Bug cases for current Haxe: @:nullSafety(StrictThreaded)
class Main {
static function main() {
final arr:Array<Int> = [ // Cannot use nullable value of Int as an item in Array<Int>
{
var tmp = (1 : Null<Int>);
if (tmp != null) tmp else 2;
}
];
arr.push(
{ // Cannot pass nullable value to not-nullable argument "x" of function "push"
var tmp = (1 : Null<Int>);
if (tmp != null) tmp else 2;
}
);
}
} Only for |
I've pushed a fix for that sample to development. Try reverting your changes of |
Currently i see these problems: final a = 1;
final b = 2;
trace(a ?? b);
~ Will return And another problem is disabled operator overloading. Not sure about reason, is this possible with current basic operator implementation? Cannot find where this thing is handled, because there is no errors for Anyway, maybe current state of PR is okay to merge, if feature will be approved and i don't skip some broken behavior. |
I think on static platform |
If you forgot to write |
I don't get what you mean. Could you please provide a sample? |
var a = cast null;
// or
var a = ({} : Dynamic).x;
var b = a ?? 2; // Error: On static platforms, null can't be used as basic type Int
|
Then var tmp = a;
if(tmp == null) b else a; just change the type of |
Is this would allocate new |
class Main {
static function main() {
final a = Std.random(0);
final b = {
final tmp:Null<Int> = a;
if (tmp != null) tmp else 2;
}
logInt(b);
final c = {
final tmp:Int = a;
// silly unspecified hack
if (tmp != cast null) tmp else 2;
}
logInt(c);
}
static function logInt(a:Int):Void trace(a);
} hxcpp: void Main_obj::main(){
HX_STACKFRAME(&_hx_pos_e47a9afac0942eb9_2_main)
HXLINE( 3) int a = ::Std_obj::random(0);
HXLINE( 6) ::Dynamic tmp = a;
HXLINE( 5) ::Dynamic b;
HXLINE( 7) if (::hx::IsNotNull( tmp )) {
HXLINE( 5) b = tmp;
}
else {
HXLINE( 5) b = 2;
}
HXLINE( 9) ::Main_obj::logInt(( (int)(b) ));
HXLINE( 11) int c;
HXLINE( 13) if (::hx::IsNotNull( a )) {
HXLINE( 11) c = a;
}
else {
HXLINE( 11) c = 2;
}
HXLINE( 15) ::Main_obj::logInt(c);
} |
If |
imagine rtl multiplication op with this, i'm not sure...
This reverts commit 48320b6.
Are there any TODOs here or can we head towards merging this? |
I would like to add |
@RealyUniqueName The changes look good to me. Since you're Mr. Null, could you double-check that all cases are covered in the tests? If so, we can merge this. |
I'll merge this now. If there's some fallout, we can deal with that separately. Thank you for this contribution! |
Need to know if current implementation is fine.
final a = null ?? 2;
shows null safety error, same as witha = null == null ? 2 : null
expression.Closes #10478