implement generic default values for object fields (#24384)

fixes #21941, fixes #23594
This commit is contained in:
metagn
2024-10-30 10:58:04 +03:00
committed by GitHub
parent d61897459d
commit 4091576ab7
5 changed files with 65 additions and 23 deletions

View File

@@ -0,0 +1,26 @@
block: # issue #23594
type
Gen[T] = object
a: T = 1.0
Spec32 = Gen[float32]
Spec64 = Gen[float64]
var
a: Spec32
b: Spec64
doAssert sizeof(a) == 4
doAssert sizeof(b) == 8
doAssert a.a is float32
doAssert b.a is float64
block: # issue #21941
func what[T](): T =
123
type MyObject[T] = object
f: T = what[T]()
var m: MyObject[float] = MyObject[float]()
doAssert m.f is float
doAssert m.f == 123.0