nimpylib
nimpylib copied to clipboard
None?
It seems to me there are two possible solution:
- provide a wrapper for non-nilable types and translate None for them for this wrapper constructor
- provide a wrapper for any type specialized for nilable types and translate any None call to it
It seems to me the second one is easier. Unluckily there is no return-type generic overloading in implicit conversions so a type None[T]
type rather than const None: NoneType
should be used. It would works like this:
var w = "wrap".pyWrap()
w = "value" # ok, returns PyWrapper("value")
let s: string = w # ok, returns "value"
var y = @[2,7,1]
y = @[8, 2, 8].pyWrap() # ok, returns @[8, 2, 8]
y = none(y) # ok, returns nil
y = none(type(y))
y = None[type(y)]()
y = none(y).pyWrap() # ok, returns nil
var x = 3
x = 1.pyWrap() # ok, returns 3
x = none(x).pyWrap() # runtime-error: "None dereference"
x = pyWrapNone(x)
x = none(x) # compile-time error: "`int` type is not nilable!"
So in fact, all lhs = None
assignments should be translated to lhs = pyWrap(none(lhs))
. Of course they can form a cycle with generic arguments and then some additional type annotation would be needed.
Actually, I have already implemented PyWrapper which works for these examples but I'm not sure if it's the best approach available, especially considering no generic implicit conversions...