to Int() - > Option<int>- Module
- Strings →
- Receiver
- string
- Minimum arity
- 0
- Maximum arity
- 0
Declaration and parameters
Availability
Included by default · yes
These types are provided by the language. No module import is required.
The declarations below describe the standard operations on built-in values.
Receiver
string
Parameter constraints
string. returns Option<int>. It accepts an optional leading - followed by one or more ASCII digits 0 through 9, and no other characters.
Leading zeros are accepted. - produces 0.
Leading or trailing whitespace, a leading +, digit separators, radix prefixes, exponents, fractions and non-ASCII digits return None(). Empty text and a lone - also return None().
A spelled value below -9223372036854775808 or above 9223372036854775807 returns None(). The call never faults for its text.
Call trim() before to when the input format allows surrounding whitespace.
Result
The declared result type is Option<int>.
Effects
Failures
Result and effects
Return None() for any other spelling and for a value outside the int range.
Related entries
Parse decimal integers with one spelling
Parse decimal integers with one spelling
string. returns Option<int>. It accepts an optional leading - followed by one or more ASCII digits 0 through 9, and no other characters.
Leading zeros are accepted. - produces 0.
Leading or trailing whitespace, a leading +, digit separators, radix prefixes, exponents, fractions and non-ASCII digits return None(). Empty text and a lone - also return None().
A spelled value below -9223372036854775808 or above 9223372036854775807 returns None(). The call never faults for its text.
Call trim() before to when the input format allows surrounding whitespace.
Why
One number has one accepted spelling, as in BigInt and Decimal text parsing. Rejecting whitespace and a plus sign keeps malformed input visible to the program, so a reader of wire formats does not accept a value that its writer never produces.
Parse a minus sign and decimal digits
import std.iofunction show(text: string) -> string {match (text.toInt()) {case Some(value) => "{value}"case None() => "none"}}for text in (["42", "-7", "007", "-0", "9223372036854775807", "-9223372036854775808"]) {io.writeLine("[{text}] {show(text)}")}
[42] 42
[-7] -7
[007] 7
[-0] 0
[9223372036854775807] 9223372036854775807
[-9223372036854775808] -9223372036854775808
Expected result
Expected result
Execution completes. Exit code 0.
[42] 42[-7] -7[007] 7[-0] 0[9223372036854775807] 9223372036854775807[-9223372036854775808] -9223372036854775808
Return None() for other spellings and out-of-range values
import std.iofunction show(text: string) -> string {match (text.toInt()) {case Some(value) => "{value}"case None() => "none"}}for text in (["", "-", "12a", "1_000", "0x10", "1e3", "١٢", "9223372036854775808", "-9223372036854775809"]) {io.writeLine("[{text}] {show(text)}")}
[] none
[-] none
[12a] none
[1_000] none
[0x10] none
[1e3] none
[١٢] none
[9223372036854775808] none
[-9223372036854775809] none
Expected result
Expected result
Execution completes. Exit code 0.
[] none[-] none[12a] none[1_000] none[0x10] none[1e3] none[١٢] none[9223372036854775808] none[-9223372036854775809] none
Return None() for surrounding whitespace and a plus sign
import std.iofunction show(text: string) -> string {match (text.toInt()) {case Some(value) => "{value}"case None() => "none"}}for text in ([" 12", "12 ", "+5"]) {io.writeLine("[{text}] {show(text)}")}
[ 12] none
[12 ] none
[+5] none
Expected result
Expected result
Execution completes. Exit code 0.
[ 12] none[12 ] none[+5] none