Hi Nim lovers,
I am trying to find a way to implement something like the following:
proc type_map(t: typedesc): typedesc =
result = t # should check some conditions and generate a new t if needed
type Builder[T] = object
obj: type_map(T) # complexity is here
type MyObj = object
date: int
let b = Builder[MyObj]
The idea is Builder needs to hold modified type T, in this example I need to detect presence of case elements in T object and define new linearized type. So if t is defined
type MyTyp = object
case a: bool
of true: b:int
of false: c:float
translate it into
type MyTypComputed = object
a: bool
b:int
c:float
I have tried a number of ways but can't find a way that compiles. Feels like something static[T] should be able to do.
Please help
I'm pretty sure you can use macro and getTypeImpl.
Check how the distinctBase macro to "undistinct" distinct types is implemented: https://github.com/nim-lang/Nim/commit/43f634db8dfac4fb13c8454958d35e776410dac1
Hi mratsim I should have explained better where the problem is. I don't a problem with calling getImpl and analyzing the type. I can't make template instantiation mechanism to call my type_map proc/template that is supposed to do the analysis.
I am getting errors like
builder.nim(6, 16) Error: expression 'type_map(T)' has no type (or is ambiguous)
when I am trying to instantiate the Builder type Builder[MyObj]Here you go
import macros
macro type_map(t: typedesc): untyped =
result = getTypeInst(t) # should check some conditions and generate a new t if needed
type Builder[T] = object
obj: type_map(T) # complexity is here
type MyObj = object
date: int
let b = Builder[MyObj]()