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

Refactoring how optional params work #505

Merged
merged 1 commit into from
Nov 20, 2024
Merged

Conversation

kengorab
Copy link
Owner

Previously, functions with optional parameters would have an additional "mask" parameter at the end which would indicate to if-statements contained within the body of the function whether or not to reassign parameters to their default values if they were omitted. This resulted in a lot of bloated logic within the compiler, and also a performance cost since each time a function with optional parameters was called, those if-statements would run even if all values were passed.

The new approach is to compile a separate function instance for each combination of optional parameters, which ultimately calls the base function and passes in the default values for the missing parameters. This allows all decisions to happen at compile-time rather than at runtime, and it's also a LOT simpler to reason about within the compiler itself.

The change was also made for types with optional fields, and I also updated enum variants to be able to have optional fields as well. This also has the added benefit of being able to remove the Pointer.null() default field value for String#_buf (which is a relic of a previous version of the prelude).

Previously, functions with optional parameters would have an additional
"mask" parameter at the end which would indicate to if-statements
contained within the body of the function whether or not to reassign
parameters to their default values if they were omitted. This resulted
in a lot of bloated logic within the compiler, and also a performance
cost since each time a function with optional parameters was called,
those if-statements would run even if all values were passed.

The new approach is to compile a separate function instance for each
combination of optional parameters, which ultimately calls the base
function and passes in the default values for the missing parameters.
This allows all decisions to happen at compile-time rather than at
runtime, and it's also a LOT simpler to reason about within the compiler
itself.

The change was also made for types with optional fields, and I also
updated enum variants to be able to have optional fields as well. This
also has the added benefit of being able to remove the `Pointer.null()`
default field value for `String#_buf` (which is a relic of a previous
version of the prelude).
@kengorab
Copy link
Owner Author

This also closes #495

@kengorab kengorab merged commit cbeb86b into master Nov 20, 2024
1 check passed
@kengorab kengorab deleted the refactoring-optional-params branch November 20, 2024 02:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant