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

Named Arguments #625

Merged
merged 32 commits into from
Sep 17, 2023
Merged

Named Arguments #625

merged 32 commits into from
Sep 17, 2023

Conversation

gdotdesign
Copy link
Member

This PR implements the ability to call functions with an alternative syntax using the names of the arguments.

component Main {
  fun test (argument1 : String, argument2: Number) : Html {
    <div/>
  }

  fun render : Html {
    test(argument2: 0, argument1: "")
  }
}

The only rule as of now is that there can't be normal arguments and labelled arguments in a call.

@gdotdesign gdotdesign requested a review from Sija August 8, 2023 12:35
@gdotdesign gdotdesign self-assigned this Aug 8, 2023
@gdotdesign gdotdesign added this to the 0.19.0 milestone Aug 8, 2023
src/compilers/call.cr Outdated Show resolved Hide resolved
.parameters
.index { |param| param.label == argument.name.try(&.value) }

raise CallNotFoundArgument, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise CallNotFoundArgument, {
raise ArgumentNameMismatch, {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't agree with change, it's not a mismatch (although it could be) but a not found parameter.

If we really need to change this, then It could be CallArgumentNameMismatch.

src/type_checkers/call.cr Outdated Show resolved Hide resolved
Comment on lines -21 to +31
parameters.map(&.to_pretty.as(String))
parameters.map do |param|
pretty =
param.to_pretty.as(String)

if param.label
"#{param.label}: #{pretty}"
else
pretty
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd refactor this into Record#to_pretty(named_arguments: true).

src/type_checkers/argument.cr Outdated Show resolved Hide resolved
src/type_checkers/call.cr Outdated Show resolved Hide resolved
src/type_checkers/call.cr Outdated Show resolved Hide resolved
@Sija Sija added enhancement New feature or request language Language feature labels Aug 9, 2023
@gdotdesign gdotdesign requested a review from Sija September 7, 2023 08:32
@gdotdesign gdotdesign marked this pull request as ready for review September 7, 2023 08:32
Copy link
Member

@Sija Sija left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 🤩

@Sija
Copy link
Member

Sija commented Sep 9, 2023

As a side note: I'd use Calls with named arguments or Named arguments in calls wording rather than Labeled calls, which IMO is a bit abstract.

Base automatically changed from locales to master September 17, 2023 19:10
@gdotdesign gdotdesign changed the title Labelled Calls. Named Arguments Sep 17, 2023
@gdotdesign gdotdesign merged commit 5acd5b8 into master Sep 17, 2023
3 checks passed
@gdotdesign gdotdesign deleted the labelled-calls branch September 17, 2023 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request language Language feature
Development

Successfully merging this pull request may close these issues.

2 participants