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 arguments in function definitions #17

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

msakuta
Copy link
Owner

@msakuta msakuta commented Jul 9, 2024

This feature exists in C++, Python and many other languages (but interestingly not in Rust).

You can define a function like this:

fn add(a: i32 = 1, b: i32 = 2) -> i32 {
    a + b;
}

and you can call this function without explicit arguments:

print(add());

and it should print 3.

You can also partially provide an argument:

print(add(1000));

which should print 1002.

See the wiki for more details.

msakuta added 14 commits July 7, 2024 14:47
Bytecode compiler fails to compile fn_default_expr.dragon, but
it's intended.
It's actually a test for the whole pipeline, i.e.
parse, compile and interpret.
Since it is more like integration test.
In order to match the behavior with the bytecode.

We may extend the language to allow accessing outer variables,
but for now we only allow constant expression, which makes
the implementation of bytecode much simpler.
If a function default argument accesses an outer variable, it should
be an error.
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

Successfully merging this pull request may close these issues.

None yet

1 participant