wren icon indicating copy to clipboard operation
wren copied to clipboard

Add optional argument to specify base in Num.fromString

Open CrazyInfin8 opened this issue 3 years ago • 4 comments

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.

CrazyInfin8 avatar May 02 '21 04:05 CrazyInfin8

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.

PureFox48 avatar May 02 '21 11:05 PureFox48

Thinking some more about my last point, perhaps an entirely new method Num.toBaseString(radix) would be better.

PureFox48 avatar May 02 '21 11:05 PureFox48

I prefer an overloaded Num.toString(radix).

ChayimFriedman2 avatar May 02 '21 12:05 ChayimFriedman2

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.

PureFox48 avatar May 02 '21 12:05 PureFox48