Skip to content

Commit

Permalink
fix StaticParam
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Apr 14, 2020
1 parent 10090c2 commit e475d74
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/pure/typetraits.nim
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ since (1, 1):
# Note: `[]` currently gives: `Error: no generic parameters allowed for ...`
type(default(T)[i])

type StaticParam*[value] = object
type StaticParam*[value: static type] = object
## used to wrap a static value in `genericParams`

# NOTE: See https://github.com/nim-lang/Nim/issues/13758 - `import std/macros` does not work on OpenBSD
Expand All @@ -118,13 +118,12 @@ macro genericParamsImpl(T: typedesc): untyped =
let ai = impl[i]
var ret: NimNode
case ai.typeKind
of ntyStatic:
since (1, 1):
ret = newTree(nnkBracketExpr, @[bindSym"StaticParam", ai])
of ntyTypeDesc:
ret = ai
of ntyStatic: doAssert false
else:
assert false, $(ai.typeKind, ai.kind)
since (1, 1):
ret = newTree(nnkBracketExpr, @[bindSym"StaticParam", ai])
result.add ret
break
else:
Expand Down
1 change: 1 addition & 0 deletions tests/metatype/ttypetraits.nim
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ block genericParams:
doAssert genericParams(Bar3).get(0) is StaticParam
doAssert genericParams(Bar3).get(0).value == 3
doAssert genericParams(Bar[3, float]).get(0).value == 3
static: doAssert genericParams(Bar[3, float]).get(0).value == 3

type
VectorElementType = SomeNumber | bool
Expand Down
32 changes: 32 additions & 0 deletions tests/statictypes/tstatictypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,35 @@ var
s: StringValue16

echo s

block: #13529
block:
type Foo[T: static type] = object
var foo: Foo["test"]
doAssert $foo == "()"
doAssert foo.T is string
static: doAssert foo.T == "test"
doAssert not compiles(
block:
type Foo2[T: static type] = object
x: T)

block:
type Foo[T: static[float]] = object
var foo: Foo[1.2]
doAssert $foo == "()"
doAssert foo.T == 1.2

block: # routines also work
proc fun(a: static) = (const a2 = a)
fun(1)
fun(1.2)
block: # routines also work
proc fun(a: static type) = (const a2 = a)
fun(1)
fun(1.2)

block: # this also works
proc fun[T](a: static[T]) = (const a2 = a)
fun(1)
fun(1.2)

0 comments on commit e475d74

Please sign in to comment.