まとめ
typedesc[SomeObject]
を第一引数にとろう。型に対して関数が定義できる。
enumにエイリアス
enumDef.nim
type SomeEnum {.pure.} = enum
ValA
ValB
ValC
type OtherEnum {.pure.} = enum
ValA
ValB
ValC
template AliasA(someEnum: typedesc[SomeEnum]): SomeEnum =
SomeEnum.ValA
assert SomeEnum.ValA == SomeEnum.AliasA
# error!
# assert SomeEnum.ValA == AliasA
# assert SomeEnum.ValA == OtherEnum.AliasA
# echo OtherEnum.AliasA
# echo AliasA
template defineAlias*(TEnum: typedesc; value, alias: untyped): untyped =
template alias*(tEnum: typedesc[TEnum]): TEnum =
TEnum.value
SomeEnum.defineAlias(ValB, AliasB)
assert SomeEnum.ValB == SomeEnum.AliasB
class method
type Object = object
value: int
var `Object.staticmember`: int = 10
proc init(TObject: typedesc[Object]; value: int): Object =
Object(value: value)
template staticmember(_: typedesc[Object]): int = `Object.staticmember`
echo repr Object.init(10)
# stdout: 10
echo Object.staticmember
# stdout: 10