The language / Calls and expressions
Start here01
The language02
- 01The basics
- 02Values and numbers
- 03Variables
- 04Calls and expressions
- 05Conditions and loops
- 06Strings
- 07Arrays and hashes
- 08Classes and records
- 09Traits and enums
- 10Blocks
- 11Methods
- 12Imports and exports
- 13Foreign files
- 14Failures
Programs and the world03
- 01Files, input and output
- 02JSON, time, URLs and random numbers
- 03Html and forms
- 04Templates: Ochre
- 05Built-in methods
Kiln04
02 The language / 04
Calls and expressions
How methods are called, why arguments have names, and why expressions stay small.
A call runs a method: one you wrote with def (see methods), or a built-in one like print. Every call is written the same way, with the method's name, parentheses, and arguments that say what each value is, so you can read any call without looking up the method first. The second half of the page is about keeping expressions readable: carrying one over several lines, and the limit on how deeply code may nest.
Calls
Call a method by writing its name directly followed by parentheses, with no space before the (. Separate the arguments with commas. A call gives back the method's value, which you can use like any other value:
def add(number: Int, to: Int): Int
return number + to
end
const price = 10
const tax = 2
print(add(1, to: 2)) # prints 3
const total = add(price, to: tax) * 2
print(total) # prints 24Named arguments
Only the first argument goes without a name, and only when the method's name says what it is. Every other argument is written with its parameter's name, so a call says what each value is. In transfer(50.0, from: "savings", to: "checking") there is no doubt which account is which:
def transfer(amount: Float, from: String, to: String): String
return "#{amount} from #{from} to #{to}"
end
print(transfer(50.0, from: "savings", to: "checking")) # prints 50.0 from savings to checking
print(transfer(50.0, "savings", to: "checking")) # error: name this argument after its parameter: 'from: ...'Not this
transfer(50.0, "savings", "checking"), which leaves the reader to remember the order
Write this
transfer(50.0, from: "savings", to: "checking")
Sometimes the method's name doesn't say what the first argument is: resize(100, height: 50) leaves the reader guessing what 100 is. Then mark the first parameter named, and the first argument gets a name too:
def resize(named width: Int, height: Int): Int
return width * height
end
print(resize(width: 100, height: 50)) # prints 5000
print(resize(100, height: 50)) # error: resize names its first argument: write resize(width: ...)Either way, each method has exactly one way to be called. The rules that keep it so:
- The first argument has no name, unless the method marks its first parameter
named. - Every other argument is named after its parameter.
- Named arguments come in the order the method declares its parameters.
- This holds for every call, built-in methods too:
ages.set("Bob", to: 40),ages.get("Bob", fallback: 0).
Each way of breaking them has its own error, which says what to write instead:
def transfer(amount: Float, from: String, to: String): String
return "#{amount} from #{from} to #{to}"
end
print(transfer(amount: 50.0, from: "a", to: "b")) # error: the first argument of transfer goes without a name: write transfer(...), not transfer(amount: ...)
print(transfer(50.0, to: "b", from: "a")) # error: name the arguments in the order transfer declares them: from, to
print(transfer(50.0, from: "a")) # error: transfer needs 'to: ...'
print(transfer(50.0, from: "a", too: "b")) # error: transfer has no parameter 'too'; its named parameters are from, toWhen you write a method, choose parameter names that make its calls read well: add(1, to: 2), repeat(text, times: 3), distance(home, to: work). Defining methods has more on parameters.
Inside the parentheses, arguments may span several lines, and a trailing comma after the last one is allowed. hem fmt breaks a call over lines like that when it is too long for one, and joins it back up when it fits, which is why this one sits on a single line:
def add(number: Int, to: Int): Int
return number + to
end
const total = add(10, to: 2)
print(total) # prints 12Parentheses
Every call has parentheses, even one without arguments. They're what says a method runs; a name without them is a value. So name.upcase() runs a method, while name.length reads a value:
const name = "Ada"
print(name.upcase()) # prints ADA
print(name.length) # prints 3Only length, keys, values and characters are values, so they go without parentheses, like the fields of an object. Everything else a value offers is a method (see built-in methods). Leaving the parentheses off a method, adding them to a value, or putting a space before the ( is an error:
const name = "Ada"
print name # error: expected end of line, found 'name'; to call 'print', use parentheses: print(...)
const x = name.upcase # error: 'upcase' is a method; write it with parentheses: name.upcase()
const n = name.length() # error: 'length' is a value; write it without parentheses
print (1) # error: expected end of line, found '('; to call 'print', write print(...) with no space before '('Long expressions
A line break ends a statement (see lines and whitespace), so an expression that carries on to the next line has to show that it isn't finished. End the line with an operator or =, or wrap the expression in parentheses:
const price = 10
const shipping = 3
const tax = 2
const total = price +
shipping +
tax
const subtotal = (price + shipping)
print(total) # prints 15
print(subtotal) # prints 13The operator goes at the end of the line, never at the start of the next. A line that starts with an operator is a new statement, not a continuation, and on its own it means nothing:
const shipping = 3
const total = 10
+ shipping # error: expected an expression, found '+'Complexity limit
Expressions are meant to stay small, so there is a limit on how deep code may nest: 256 levels. These each add a level:
- every pair of parentheses;
- every
-ornot; - every block (
if,while) inside another; - every operator in a chain like
1 + 2 + 3.
A long else if chain does not, since its branches sit side by side. Past the limit you get:
error: code is too complex (more than 256 levels deep); split it into smaller stepsReal code should never come close. If yours does, break it into steps with names of their own, held in variables; that is also easier to review.
While the program runs, the only limit is on how many method calls are in progress at once; see recursion.