Closed vladpi closed 1 year ago
Because name and strength are possibly null, we have to handle that condition.
The easiest way is using ??
function fight(one: Person, two: Person) -> str
using (
(one.name ?? '') ++ ' wins!' IF (one.strength ?? 0) > (two.strength ?? 0) ELSE (two.name ?? '') ++ ' wins!'
)
Or you can use WITH
for more easy-to-read if statement
function fight(first_person: Person, second_person: Person) -> str
using (
with
first_person_name := first_person.name ?? 'First person',
first_person_strength := first_person.strength ?? 0,
second_person_name := second_person.name ?? 'Second person',
second_person_strength := second_person.strength ?? 0,
select (
first_person_name ++ ' wins!'
if first_person_strength > second_person_strength
else 'Draw!'
if first_person_strength = second_person_strength
else second_person_name ++ ' wins!'
)
)
The overloaded fight function in Chapter 12 throws the same error.
I see the fight functions have been fixed, added them to the chapter schemas as well so should be good to close now.
Chapter 11 has a function
fight
:But when I try to create it, I get this error:
As far as I understand, this happens because name and strength are optional fields and the function may return an empty set. The problem is solved by coalescing, but this is discussed in the next chapter (12)