-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add experimental inferGenericTypes switch #22317
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
008ffa4
Infer generic bindings
bc08ff2
Simple test
257fd33
Add t
0c6d8bb
Allow it to work for templates too
d5cbc7e
Fix some builds by putting bindings in a template
f2ed402
Fix builtins
cf36fa8
Slightly more exotic seq test
a235f14
Test value-based generics using array
e331214
Pass expectedType into buildBindings
851fbcf
Put buildBindings into a proc
04f7e08
Manual entry
d607d26
Remove leftover `
f915036
Improve language used in the manual
165673b
Experimental flag and fix basic constructors
9e42bbc
Tiny commend cleanup
e66af09
Move to experimental manual
444c169
Use 'kind' so tuples continue to fail like before
b554162
Explicitly disallow tuples
1cad79f
Table test and document tuples
189c9e1
Test type reduction
6d85aeb
Disable inferGenericTypes check for CI tests
b006c4e
Remove tuple info in manual
347eb20
Always reduce types. Testing CI
2a8353c
Fixes
dccee38
Ignore tyGenericInst
be98387
Prevent binding already bound generic params
fe2b42e
tyUncheckedArray
524a289
Few more types
d926772
Update manual and check for flag again
fbde75b
Update tests/generics/treturn_inference.nim
Araq 970fafc
var candidate, remove flag check again for CI
18465fa
Enable check once more
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,78 @@ | ||
|
|
||
| block: | ||
| type | ||
| MyOption[T, Z] = object | ||
| x: T | ||
| y: Z | ||
|
|
||
| proc none[T, Z](): MyOption[T, Z] = | ||
| when T is int: | ||
| result.x = 22 | ||
| when Z is float: | ||
| result.y = 12.0 | ||
|
|
||
| proc myGenericProc[T, Z](): MyOption[T, Z] = | ||
| none() # implied by return type | ||
|
|
||
| let a = myGenericProc[int, float]() | ||
| doAssert a.x == 22 | ||
| doAssert a.y == 12.0 | ||
|
|
||
| let b: MyOption[int, float] = none() # implied by type of b | ||
| doAssert b.x == 22 | ||
| doAssert b.y == 12.0 | ||
|
|
||
| # Simple template based result with inferred type for errors | ||
| block: | ||
| type | ||
| ResultKind {.pure.} = enum | ||
| Ok | ||
| Err | ||
|
|
||
| Result[T] = object | ||
| case kind: ResultKind | ||
| of Ok: | ||
| data: T | ||
| of Err: | ||
| errmsg: cstring | ||
|
|
||
| template err[T](msg: static cstring): Result[T] = | ||
| Result[T](kind : ResultKind.Err, errmsg : msg) | ||
|
|
||
| proc testproc(): Result[int] = | ||
| err("Inferred error!") # implied by proc return | ||
| let r = testproc() | ||
| doAssert r.kind == ResultKind.Err | ||
| doAssert r.errmsg == "Inferred error!" | ||
|
|
||
| # Builtin seq | ||
| block: | ||
| let x: seq[int] = newSeq(1) | ||
| doAssert x is seq[int] | ||
| doAssert x.len() == 1 | ||
|
|
||
| type | ||
| MyType[T, Z] = object | ||
| x: T | ||
| y: Z | ||
|
|
||
| let y: seq[MyType[int, float]] = newSeq(2) | ||
| doAssert y is seq[MyType[int, float]] | ||
| doAssert y.len() == 2 | ||
|
|
||
| let z = MyType[seq[float], string]( | ||
| x : newSeq(3), | ||
| y : "test" | ||
| ) | ||
| doAssert z.x is seq[float] | ||
| doAssert z.x.len() == 3 | ||
| doAssert z.y is string | ||
| doAssert z.y == "test" | ||
|
|
||
| # array | ||
| block: | ||
| proc giveArray[N, T](): array[N, T] = | ||
| for i in 0 .. N.high: | ||
| result[i] = i | ||
| var x: array[2, int] = giveArray() | ||
| doAssert x == [0, 1] |
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.
Uh oh!
There was an error while loading. Please reload this page.