Added support for the null coalescing operator ?? #809

Merged
merged 1 commit into from Aug 21, 2014

Projects

None yet

2 participants

@shamanas
Collaborator

No description provided.

@shamanas shamanas merged commit c10f431 into ooc-lang:master Aug 21, 2014

1 check passed

continuous-integration/travis-ci The Travis CI build passed
Details
@davidhesselbom
Contributor
@davidhesselbom
Contributor

Wait, it was added in... um. I don't know what I was thinking, this is over a year old and so is the commit in NagaQueen. Forget I said anything :P

@shamanas
Collaborator

Haha no worries, I missed the Nagaqueen.c diff too for a bit and was wondering if it was pushed before it got implemented in rock ^^

@shamanas
Collaborator

@davidhesselbom I think there is a double evaluation bug with it still, I'll take a look at it right now

(I think I naively translated a ?? b to a != null ? a : b without taking the side effects of a into account)

@davidhesselbom
Contributor

You did...

honk: func -> Bool { println("honk"); true }
toot := honk() ?? false

prints

honk
honk

but it should only print the word once.

@davidhesselbom
Contributor

Also, your test includes this line:

if ((1 ?? 2 ?? 3) != 3)

which gives me the following warnings:

test.ooc: In function ‘test_test_load’:
test.ooc:7:19: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
 if ((1 ?? 2 ?? 3) != 3)
                   ^
test.ooc:7:55: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
 if ((1 ?? 2 ?? 3) != 3)
                                                       ^

@shamanas
Collaborator

Huh, interesting how the operator works with bool too, probably because I actually translate to a ? a : b, I'm not sure if even that is intended.

Translating to

a_temp : typeof(a)

... (a_temp = a, a_temp ? a_temp : b) ...

Should do the trick, grabbing a coffee and going to town :)

Edit: actually, the != null comparison is there, your honk example compares a Bool to a Pointer, which apparently passes right through rock, and true == 1 != null, so it seems to work with bools too :P

@shamanas
Collaborator

I don't get warnings on my test but I could see how you would if you ignore the overload

operator ?? (l, r: Int) -> Int { r }

In which case you are comparing integers to null, thus "cast from pointer to integer of different size"

@davidhesselbom
Contributor

Ah, yes, I did, I just took that one line from the test file. That explains it.

@shamanas
Collaborator

Should be somewhat better to use now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment