forked from nim-lang/Nim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add switch, warning, and
bind
support for new generic injection beh…
…avior refs nim-lang#23091, especially post merge comments
- Loading branch information
Showing
6 changed files
with
101 additions
and
6 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
type Xxx = enum | ||
error | ||
value | ||
|
||
type | ||
Result[T, E] = object | ||
when T is void: | ||
when E is void: | ||
oResultPrivate*: bool | ||
else: | ||
case oResultPrivate*: bool | ||
of false: | ||
eResultPrivate*: E | ||
of true: | ||
discard | ||
else: | ||
when E is void: | ||
case oResultPrivate*: bool | ||
of false: | ||
discard | ||
of true: | ||
vResultPrivate*: T | ||
else: | ||
case oResultPrivate*: bool | ||
of false: | ||
eResultPrivate*: E | ||
of true: | ||
vResultPrivate*: T | ||
|
||
template valueOr[T: not void, E](self: Result[T, E], def: untyped): untyped = | ||
let s = (self) # TODO avoid copy | ||
case s.oResultPrivate | ||
of true: | ||
s.vResultPrivate | ||
of false: | ||
when E isnot void: | ||
template error: untyped {.used, inject.} = s.eResultPrivate | ||
def | ||
|
||
proc f(): Result[int, cstring] = | ||
Result[int, cstring](oResultPrivate: false, eResultPrivate: "f") | ||
|
||
proc g(T: type): string = | ||
let x = f().valueOr: | ||
return $error #[tt.Warning | ||
^ a new symbol 'error' has been injected during instantiation of g, however 'error' [enumField declared in tmacroinjectedsymwarning.nim(2, 3)] captured at the proc declaration will be used instead; either enable --experimental:genericsOpenSym to use the injected symbol or `bind` this captured symbol explicitly [GenericsIgnoredInjection]]# | ||
|
||
"ok" | ||
|
||
discard g(int) |