Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support default values for function arguments #34

Open
harlanhaskins opened this issue Mar 5, 2017 · 4 comments
Open

Support default values for function arguments #34

harlanhaskins opened this issue Mar 5, 2017 · 4 comments

Comments

@harlanhaskins
Copy link
Collaborator

harlanhaskins commented Mar 5, 2017

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)
@harlanhaskins harlanhaskins changed the title Support default arguments. Support default values for function arguments Mar 5, 2017
@segiddins
Copy link
Contributor

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
Copy link
Collaborator Author

No, they should be restricted to global constants

@harlanhaskins
Copy link
Collaborator Author

Or literals

@segiddins
Copy link
Contributor

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) {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants