implements overloadable enum values; WIP (#18470)
* implements overloadable enum values * simpler code
This commit is contained in:
parent
4c1202972a
commit
a273ea70e8
10 changed files with 125 additions and 11 deletions
48
tests/enum/toverloadable_enums.nim
Normal file
48
tests/enum/toverloadable_enums.nim
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
discard """
|
||||
output: '''B
|
||||
0'''
|
||||
joinable: false
|
||||
"""
|
||||
|
||||
{.experimental: "overloadableEnums".}
|
||||
|
||||
type
|
||||
E1 = enum
|
||||
value1,
|
||||
value2
|
||||
E2 = enum
|
||||
value1,
|
||||
value2 = 4
|
||||
|
||||
const
|
||||
Lookuptable = [
|
||||
E1.value1: "1",
|
||||
value2: "2"
|
||||
]
|
||||
|
||||
when false:
|
||||
const
|
||||
Lookuptable: array[E1, string] = [
|
||||
value1: "1",
|
||||
value2: "2"
|
||||
]
|
||||
|
||||
|
||||
proc p(e: E1): int =
|
||||
# test that the 'case' statement is smart enough:
|
||||
case e
|
||||
of value1: echo "A"
|
||||
of value2: echo "B"
|
||||
|
||||
|
||||
let v = p value2 # ERROR: ambiguous!
|
||||
# (value2|value2) nkClosedSymChoice -> nkSym
|
||||
|
||||
proc x(p: int) = discard
|
||||
proc x(p: string) = discard
|
||||
|
||||
proc takeCallback(param: proc(p: int)) = discard
|
||||
|
||||
takeCallback x
|
||||
|
||||
echo ord v
|
||||
|
|
@ -2,7 +2,6 @@ discard """
|
|||
action:reject
|
||||
cmd: "nim check $options $file"
|
||||
nimout: '''
|
||||
t10251.nim(15, 5) Error: redefinition of 'foo'; previous declaration here: t10251.nim(13, 5)
|
||||
t10251.nim(19, 23) Error: redefinition of 'goo1'; previous declaration here: t10251.nim(19, 11)
|
||||
'''
|
||||
"""
|
||||
|
|
@ -14,6 +13,7 @@ type
|
|||
Enum2 = enum
|
||||
foo, bar, baz
|
||||
|
||||
|
||||
type
|
||||
Enum3 {.pure.} = enum # fixed (by accident?) in https://github.com/nim-lang/Nim/pull/18263
|
||||
goo0, goo1, goo2, goo1
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue