constructor
constructor copied to clipboard
Make defaults work on generic types
Trying to solve #13. Here's an example proving that it should work:
import std/[macros, genasts]
macro doIt(impl: untyped): untyped =
result = impl
result[^1] = genast():
type Bleh[T] = object
x: T
proc initBleh[T](): Bleh[T] = Bleh[T](x: 100)
Bleh
type MyType {.doIt.} = object
echo initBleh[int]()
Though right now, the code below fails:
import constructor/defaults
type
Thingy[T] {.defaults: {}.} = object
c: T = 1
echo initThingy[int]()
With:
$ nim c --expandMacro:defaults trial.nim
..................................................................................................
.../trial.nim(4, 15) Hint: expanded macro: Thingy[T] {..} =
type
Inner_536871044[T] = object
proc initThingy[T](): Inner_536871044[T] =
Inner_536871044[T](c: 1)
Inner_536871044 [ExpandMacro]
..../src/constructor/defaults.nim(63, 22) Hint: 'initThingy' is declared but not used [XDeclaredButNotUsed]
.../trial.nim(7, 6) Error: undeclared identifier: 'initThingy'
candidates (edit distance, scope distance); see '--spellSuggest':
(5, 1): 'Thingy'