I am not fully sure if I understood the question correctly, but I guess what
you want to achieve is something like this:
import macros
macro returnTypeOf(name:typed) :untyped =
if name.typeKind == ntyTypeDesc: # when a proc type is passed
getType(name)[1][1]
elif name.typeKind == ntyProc: # when a proc itself is passed
getType(getTypeInst(name))[1]
else: # when any other identifier is passed, returning its own type
getType(getTypeInst(name))
Run
This macro will return the _return
[type](https://forum.nim-lang.org/postActivity.xml#type) of the passed
identifier if it is a proc or a proc type name and the type of the identifier
itself for any other identifiers.
Using your example;
type Proc = proc: int
echo "Return type of Proc is: ", returnTypeOf(Proc)
#will print: Return type of Proc is: int
Run
You can use the returned type to define other types or variables and the same
macro also returns the type of any other identifier. (If I missed some other
type, it is just a matter of adding another elif branch.
Some more examples of use:
type ReturnOfProc = returnTypeOf(Proc)
var fancyInteger :ReturnOfProc = 1
var anotherInt :returnTypeOf(Proc) = 2
proc isThisRight(s:string) :bool =
return s == "right"
var someBool :returnTypeOf(isThisRight)
# exactly same as `var someBool :bool`
assert ( typeOf(someBool) is bool )
#even works with other identifiers:
var x :float
var y :returnTypeOf(x)
assert ( typeOf(y) is float )
assert ( typeOf(x) is returnTypeOf(x) )
assert ( typeOf(x) is returnTypeOf(y) )
assert ( typeOf(y) is returnTypeOf(x) )
Run
I hope this helps.