I think I have a better design for `converter`, should write an RFC... _sigh_
I really don't like converters if they are not in a "lenient_ops" kind of
module.
They introduce hard to debug bugs where the original input is converted and
then not matched to something or ambiguous call because you have 2 automatic
conversion possible (for example uint16 to int or uint16 to
To combine what @def and @Udiknedormin have suggested:
import options
converter toOption[T](x:T):Option[T] = some(x)
proc xxx(param1: string, param2, param3 = none(int)) =
if param2.isSome:
echo "param2: ", param2.some
if param3.isSome:
But that's not how overloading in this case would work, as proposed by evacchi.
If the function performs a different action depending on whether the value is
nil or not, it might require totally different function to handle that case.
Otherwise, putting the logic to adquire this value in a
Overloading has one major disadvantage:
proc hasTwoIntArgs(x = 3, y = 4) = ...
hasTwoIntArgs(x = -1) # args: x = -1, y = 4
hasTwoIntArgs(y = -1) # args: x = 3, y = -1
proc hasTwoIntArgsInvalid(x, y) = ...
proc hasTwoIntArgsInvalid(x) =
overloading may be an option as well:
proc a(x:int) =
discard
proc a() =
discard
a()
a(1)
For sake of completeness:
import options
proc xxx(param1: string, param2, param3 = none(int)) =
if param2.isSome:
echo "param2: ", param2.some
if param3.isSome:
echo "param3: ", param3.some
xxx("a", param2 = some(1))
xxx("b", param3 =
Actually there's no need for external libs or rolling your own solution. The
standard library has an `Option[T]` and is exactly suited for this.
proc p(x: int, xIsSet: bool) = ...
or smth like
type IntOrNil = object
x: int
isSet: bool
proc p(x: IntOrNil) = (if x.isSet: echo x.x else: #[ something else ]#
discard)
# additional procs may be added for such a type
proc `$`(x:
**Issue**:
Need `int` type parameters to be optional, and to test whether the
parameter has been passed to the `proc` or not, BUT the `int` type does not
allow a `nil` value
So, how do you do the equivalent of
proc xxx(param1: string, param2: int = nil, param3: int = nil)
10 matches
Mail list logo