trill-lang / trill

A type safe, compiled language inspired by (and written in) Swift
MIT License
275 stars 15 forks source link

Support default values for function arguments #34

Open harlanhaskins opened 7 years ago

harlanhaskins commented 7 years ago

Default arguments should just be AST nodes we can copy at call time if they're not provided. They should be scoped to the caller of the function, so special expressions like #file are populated with the caller's values, unlike the Python behavior where defaults are globally scoped.

For example:

func fatalError(_ message: String, file: String = #file, function: String = #function, line: Int = #line) {}

// This call here...
fatalError("Index out of bounds")

// ...desugars in Sema as:
fatalError("Index out of bounds", file: #file, function: #function, line: #line)
segiddins commented 7 years ago

Default arguments should just be AST nodes we can copy at call time if they're not provided.

would this mean that func f(_ s: Any = self) would by default return the caller?

harlanhaskins commented 7 years ago

No, they should be restricted to global constants

harlanhaskins commented 7 years ago

Or literals

segiddins commented 7 years ago

One thing I'd kinda like is for default values of args to be able to depend upon other arguments:

func f(_ x: Foo, _ length: Int = x.length) {}