In my function to make wFont for https://khchen.github.io/wNim/, the following 2 can work
import wNim
proc makeFont1(fontMessage: int): wFont {.discardable.}=
var
fontTmpMessage: wFont = Font(12, weight=wFontWeightBold)
echo fontMessage is float
echo fontMessage is int
echo fontMessage is wFont
if (fontMessage is float) or (fontMessage is int) :
echo "in if"
fontTmpMessage = Font(float fontMessage, weight=wFontWeightBold)
return fontTmpMessage
proc makeFont2(fontMessage:wFont): wFont {.discardable.}=
var
fontTmpMessage: wFont = Font(12, weight=wFontWeightBold)
echo fontMessage is float
echo fontMessage is int
echo fontMessage is wFont
if (fontMessage is wFont):
echo "in if"
fontTmpMessage = fontMessage
return fontTmpMessage
echo makeFont1(20).getPointSize()
echo "\n"
echo makeFont2(Font(32, weight=wFontWeightBold)).getPointSize()
echo "\n"
But when I try to combine the above into one in which the argument can be a number of the font size, or wFont.
import wNim
proc makeFont3(fontMessage: int|wFont): wFont {.discardable.}=
var
fontTmpMessage: wFont = Font(12, weight=wFontWeightBold)
echo fontMessage is float
echo fontMessage is int
echo fontMessage is wFont
if (fontMessage is float) or (fontMessage is int) :
fontTmpMessage = Font(float fontMessage, weight=wFontWeightBold)
else:
fontTmpMessage = fontMessage
echo makeFont3(40).getPointSize()
echo "\n"
echo makeFont3(Font(45, weight=wFontWeightBold)).getPointSize()
echo "\n"
says
b2.nim(16, 15) template/generic instantiation of `makeFont3` from here
b2.nim(14, 24) Error: type mismatch: got <int> but expected 'wFont = ref wFont:ObjectType'
Use when not if (the type must know by compiler at compile time, not run time):
proc makeFont3(fontMessage: int|wFont): wFont {.discardable.}=
var
fontTmpMessage: wFont = Font(12, weight=wFontWeightBold)
when (fontMessage is float) or (fontMessage is int) :
fontTmpMessage = Font(float fontMessage, weight=wFontWeightBold)
else:
fontTmpMessage = fontMessage
return fontTmpMessage
blush this is the 2nd time that I met and asked the same question these days. There is a long way for me to get familiar with Nim from a Python user view-of-point.
thanks again