fork download
  1. import typetraits
  2.  
  3. type MyObj = object
  4. type MyObj2 = object
  5.  
  6. proc test( x:int ) = discard
  7. proc test( x:MyObj ) = discard
  8.  
  9. template hasTestProc(x:typed) =
  10. when( compiles( test(x) ) ):
  11. echo typetraits.name( type(x) ), " has Test Proc"
  12. else:
  13. echo typetraits.name( type(x) ), " doesn't have Test Proc"
  14.  
  15. hasTestProc( 0 )
  16. hasTestProc( "abc" )
  17. hasTestProc( MyObj() )
  18. hasTestProc( MyObj2() )
Success #stdin #stdout 0s 4284KB
stdin
Standard input is empty
stdout
int has Test Proc
string doesn't have Test Proc
MyObj has Test Proc
MyObj2 doesn't have Test Proc