Refactoring how optional params work #505
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 forString#_buf
(which is a relic of a previous version of the prelude).