Evaluating Boolean Expressions
Boolean expressions come in many forms in Cocolang, such as
- A comparison expression with valid comparison operators like
<
,>
,==
,<=
or>=
. SeeConditionals.DoIfLarge()
- An inversion with the
!
operator. SeeBoolClass.Underage()
- A class that implements the
__bool__
special method can be used directly or converted withBool(x)
. SeeBoolClass.Overage()
If/Else Conditionals
Conditionals in Coco begin with the if
statement which can be used for optional execution of a block if the condition is met. The conditional expression must evaluate to a boolean as specified above. Alternate paths to the condition matching can be specified with the else
and else if
keywords
Ternary Operators
Syntax: result = (true_value
if
condition
else
false_value
)
Can have nested expression in true val and false val.
Switch Conditionals
switch statements are not currently supported yet in cocolang v0.5.2
coco Conditionals
endpoint invoke DoIf(confirm Bool) -> (out String):
if confirm:
return (out: {res} <- do())
endpoint invoke DoIfLarge(number U64) -> (out String):
if number > 5000:
return (out: {res}<- do())
else if number > 2500:
return (out: "almost")
else:
return (out: "not done")
func do() -> (res String):
yield res "done!"
coco BoolClass
class Person:
field name String
field age U64
method __bool__(self) -> (result Bool):
yield result self.age > 18
endpoint local Overage(person Person) -> (ok Bool):
yield ok Bool(person)
endpoint local Underage(person Person) -> (ok Bool):
yield ok !person
coco ternary
endpoint invoke Basic(v U64) -> (out U64):
out = (v if v < 10 else v * 10)