-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compiler): variadic arguments (#3643)
This PR adds the possibility of using variadic arguments Unfortunately, I had to add the `Clone` attribute to many `enum` and `struct` to be able to create a list of arguments compatible with the requirements for verifying the typing of the variadic argument. I needed to implement `PartialEq` for `Type`, `TypeRef`, `Class`, `Interface`, `Struct`, and `Enum`. I'm not sure if it was done in the best way, but it fulfills this specific case. - [x] Validates if the last argument (variadic) of the function accepts multiple elements. - [x] Validates if the only argument (variadic) of the function accepts multiple elements. - [x] Validates if all types of the variadic argument are the same. - [ ] Accept range as a variadic argument. Closes #125 Closes #397 ## Checklist - [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted) - [x] Description explains motivation and solution - [x] Tests added (always) - [ ] Docs updated (only required for features) - [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing *By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
- Loading branch information
1 parent
50669c5
commit d5e8bd8
Showing
14 changed files
with
438 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
let f1 = (...args: Array<num>, x:num) => {}; | ||
// ^^^^ Variadic parameters must always be the last parameter in a function. | ||
|
||
let f2 = (...nums: Array<num>, ...strs: Array<str>) => {}; | ||
// ^^^^ Variadic parameters must always be the last parameter in a function. | ||
|
||
let f3 = (...args: Array<num>) => {}; | ||
f3(1, true, 2); | ||
// ^^^^ Expected type to be num, but got bool instead. | ||
|
||
let f4 = (...args: Set<num>) => {}; | ||
// ^^^^ Variadic parameters must be type Array or MutArray. | ||
|
||
let f5 = (...args: Array<num>) => {}; | ||
f5(args: 1, 2); | ||
// ^^^^^^^^^^^^^^ No named arguments expected | ||
|
||
bring cloud; | ||
let bucket1 = new cloud.Bucket() as "bucket1"; | ||
let bucket2 = new cloud.Bucket() as "bucket2"; | ||
let funcBucket = (...buckets: Array<cloud.Bucket>) => { | ||
assert(buckets.length == 2); | ||
}; | ||
funcBucket(bucket1, true, bucket2); | ||
// ^^^^ Expected type to be Bucket, but got bool instead. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
bring cloud; | ||
let bucket1 = new cloud.Bucket() as "bucket1"; | ||
let bucket2 = new cloud.Bucket() as "bucket2"; | ||
let funcBucket = (...buckets: Array<cloud.Bucket>) => { | ||
assert(buckets.length == 2); | ||
}; | ||
funcBucket(bucket1, bucket2); | ||
|
||
let func1 = (var x: num, y: str?, var ...args: MutArray<num>) => { | ||
assert(x == 1); | ||
assert(y == "something" || y == nil); | ||
assert(args.length == 4); | ||
for i in args { | ||
assert(i > 0 && i < 5); | ||
} | ||
args.push(10); | ||
assert(args.at(4) == 10); | ||
}; | ||
func1(1, "something", 1, 2, 3, 4); | ||
func1(1, nil, 1, 2, 3, 4); | ||
|
||
let addNums = (...nums: MutArray<num>):num => { | ||
let var total = 0; | ||
for n in nums { | ||
total = total + n; | ||
} | ||
return total; | ||
}; | ||
assert(addNums(1, 2, 3) == 6); | ||
assert(addNums() == 0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.