constructor icon indicating copy to clipboard operation
constructor copied to clipboard

Make defaults work on generic types

Open Patitotective opened this issue 2 years ago • 0 comments

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'

Patitotective avatar Nov 23 '23 14:11 Patitotective