[ ] Nim |
proc plusOne(arg: int): int =
return arg + 1
echo plusOne(5) # 6
proc tryLen[T](something: T) =
when compiles(something.len): # something.len , len(something)
echo something.len
else:
echo " `len`"
# , `len` ( )
type MyObject = object
#
let myObj = MyObject()
tryLen([1, 2, 3]) # 3
tryLen("Hello world!") # 12
tryLen(myObj) # " `len`"
type MyObject = object
let myObj = MyObject()
# , return
# -
converter toInt(b: MyObject): int = 1
# ( )
echo myObj.toInt + 1 # 2
# :
echo myObj + 1 # 2, toInt myObj 1
converter toBool[T](arg: T): bool =
# len
when compiles(arg.len):
arg.len > 0
# T
# T(0) ,
# (. 0 )
elif compiles(arg + T(0)):
arg > 0
if [1, 2, 3]: #
echo "True!"
if @[1, 2, 3]: #
echo "True too!"
if "": #
echo "No :("
if 5: # Integer
echo "Nice number!"
if 0.0001: # Float
echo "Floats are nice too!"