I'm making a type registration helper template (or macro) and want to check to make sure the custom type fulfills certain criteria, like a concept.
How can I check if the following property exists at compile-time when I only know the type?
type AClass = ref object
prop1*:bool
REGISTER[AClass]() # template version
REGISTER(AClass) # macro version
Some notes of things I tried.
when declared(AClass.prop1): echo "is defined" # does not print defined
type important = concept x
x.prop1
var a:AClass # make a temporary instance
if a of important: echo "is defined" # error 'of' expects object types
Regarding your important concept: boolean values are treated special, compared to other values, in concepts. Boolean false is treated the same way, as not compiling lines, that is make concept check fail, and false is default value for bool. Other expressions/statements are implicitly wrapped with compiles:, so return true disregarding of the value of the expression. These will match:
type important2 = concept x
x.prop1 is bool # i.e. `false is bool`, which returns `true`
type important3 = concept x
x.prop1 = false # treated as `compiles: x.prop1 = false`, which returns true
type important3 = concept x
compiles: x.prop1 # `true`
type AnotherClass = ref object
prop1*: int
type important4 = concept x
x.prop1 # matches `AnotherClass`, because again is treated as `compiles: x.prop1`
echo AnotherClass is important4 # `true`
var o: AnotherClass
echo o is important4 # `true`
And you meant is, using of.
And yet a hint: you really don't need that temporal variable, is works both with types and values.
And without defining a dedicated concept:
echo compiles AClass.prop1