wren
wren copied to clipboard
Add optional argument to specify base in Num.fromString
Depends on #984.
Allows Num.fromString
to take an optional argument to specify radix.
Currently, I set it to throw errors if the radix is invalid (greater than 36 or less than 2 and unequal to 0) however some may prefer Num.fromString
to return null instead (JavaScript appears to do this, though they also don't appear to check that the radix is an integer or that the first argument is even a string!).
For now, I'll mark this PR as draft until #984 is accepted or declined.
This is something I've been meaning to suggest myself so you've beaten me to it :)
Regardless of the fate of #984, I still think it's worth doing.
I agree that it's better to throw an error if the base is invalid rather than return null
.
Ideally, Num.toString
should also take an optional radix argument though an awkward point there is that the toString
method is used in System.print
and friends and in string interpolation as the default string representation of an object. Although this wouldn't necessarily rule out an overloaded version, the latter would always have to be specified in full.
Thinking some more about my last point, perhaps an entirely new method Num.toBaseString(radix)
would be better.
I prefer an overloaded Num.toString(radix)
.
I prefer an overloaded Num.toString(radix)
Well, it would be more symmetric with what we already have.
Another alternative would be String.fromNum(num, radix)
though that would need to be static.