Coalesce operator does not transfer ownership transitively
[Note: see MR !96 (merged)]
The coalesce expression uses the target type to detect that it needs to transfer ownership from the temporary local variable it creates under the hood.
But currently, it does not propagate its target type to the left or right expressions (unlike the conditional expression).
So in the following code test_direct()
compiles and works but test_transitive()
does not compile (error: duplicating Foo instance
):
[Compact]
class Foo {
public int n;
public Foo (int n) {
this.n = n;
}
}
Foo? get_some_foo (int? n) {
return n != null ? new Foo (n) : null;
}
void test_direct () {
Foo f = get_some_foo (null) ?? get_some_foo (42);
assert (f.n == 42);
}
void test_transitive () {
Foo f = get_some_foo (null) ?? (get_some_foo (null) ?? get_some_foo (42));
assert (f.n == 42);
}
Edited by philippejer