With my current code I keep on getting typeDesc and type instead of both of them being types. How do I fix this specific issue or is there already a better implementation?
macro typecheck*(arg: typed, typ: typed) =
## Type check a proc name with args to types
let argType = arg.getType.repr # Syms won't be the same for some reason
let typType = typ.repr # Syms won't be the same for some reason
if not (argType == typType and arg.getType.typeKind == typ.getType.typeKind):
let message = "type mismatch\nExpected: '" & typType & "' got '" & argType & "'"
return quote do:
block:
{.error: `message`.}
return arg
Calls:
echo typecheck(a, int)
Note: There is also an issue with the return arg saying that arg is unused?
You do not have a return type it should be : untyped.
Is there a reason for you to use a macro here, a template works just fine.
template typeCheck(arg: typed, typ: typedesc): untyped =
when arg isnot typ:
{.error: "Type Mismatch\nExpected: '" & astToStr(typ) & "', but got '" & $type(arg) & "'".}
arg
You do not have a return type it should be : untyped.
I had : typed there but the compiler bugged me to remove it as it was the default?