GIVETH
Specifies the return type of a function. Placed after GIVEN clauses and before the function definition.
Syntax
GIVETH A Type
GIVETH Type
GIVES A Type
Purpose
GIVETH declares what type a function returns. While L4 has type inference, explicit return types improve code clarity and catch errors early.
Examples
Example file:
-- GIVETH keyword examples
-- Basic Return Types
GIVEN n IS A NUMBER
GIVETH A NUMBER
square n MEANS n TIMES n
GIVEN a IS A NUMBER
b IS A NUMBER
GIVETH A NUMBER
maximum a b MEANS IF a > b THEN a ELSE b
#EVAL square 7
#EVAL maximum 10 25
-- Boolean Return Types
GIVEN age IS A NUMBER
GIVETH A BOOLEAN
isAdult age MEANS age >= 18
GIVEN n IS A NUMBER
GIVETH A BOOLEAN
isEven n MEANS (n MODULO 2) EQUALS 0
#EVAL isAdult 21
#EVAL isEven 4
-- String Return Types
GIVEN name IS A STRING
GIVETH A STRING
greet name MEANS CONCAT "Hello, ", name
GIVEN num IS A NUMBER
GIVETH A STRING
describe num MEANS
IF num > 0 THEN "positive"
ELSE IF num < 0 THEN "negative"
ELSE "zero"
#EVAL greet "Alice"
#EVAL describe (-5)
-- Complex Return Types
-- Returning MAYBE
GIVEN xs IS A LIST OF NUMBER
GIVETH A MAYBE NUMBER
safeHead xs MEANS
CONSIDER xs
WHEN EMPTY THEN NOTHING
WHEN h FOLLOWED BY rest THEN JUST h
#EVAL safeHead (LIST 1, 2, 3)
#EVAL safeHead EMPTY
-- Returning LIST
GIVEN n IS A NUMBER
GIVETH A LIST OF NUMBER
countdown n MEANS
IF n <= 0 THEN EMPTY
ELSE n FOLLOWED BY countdown (n MINUS 1)
#EVAL countdown 5
-- GIVES as Synonym
GIVEN x IS A NUMBER
GIVES A NUMBER
triple x MEANS x TIMES 3
#EVAL triple 7
-- Parameterized Return Types
GIVEN a IS A TYPE, x IS AN a
GIVETH A LIST OF a
singleton x MEANS x FOLLOWED BY EMPTY
#EVAL singleton 42
#EVAL singleton "hello"
-- Record Return Types
DECLARE Person HAS name IS A STRING, age IS A NUMBER
GIVEN personName IS A STRING
personAge IS A NUMBER
GIVETH A Person
makePerson personName personAge MEANS Person WITH name IS personName, age IS personAge
#EVAL makePerson "Bob" 30
Basic Return Types
GIVEN n IS A NUMBER
GIVETH A NUMBER
square n MEANS n TIMES n
GIVEN s IS A STRING
GIVETH A NUMBER
stringLength s MEANS length s
Boolean Return
GIVEN age IS A NUMBER
GIVETH A BOOLEAN
isAdult age MEANS age >= 18
Complex Return Types
GIVEN xs IS A LIST OF NUMBER
GIVETH A MAYBE NUMBER
safeHead xs MEANS
CONSIDER xs
WHEN EMPTY THEN NOTHING
WHEN h FOLLOWED BY _ THEN JUST h
Parameterized Return Types
GIVEN a IS A TYPE
GIVEN x IS AN a
GIVEN y IS AN a
GIVETH A PAIR OF a, a
makePair x y MEANS Pair WITH first IS x, second IS y
GIVES vs GIVETH
GIVES is a synonym for GIVETH:
GIVEN x IS A NUMBER
GIVES A NUMBER
triple x MEANS x TIMES 3
Without GIVETH
L4 can infer return types, so GIVETH is optional:
-- Type inferred as NUMBER
GIVEN x IS A NUMBER
double x MEANS x TIMES 2
Related Keywords
Note: GIVES is a synonym for GIVETH.
See Also
- Types Reference - Available types