Julia: parse(Bool,"0") doesn't work

Created on 9 Nov 2018  路  1Comment  路  Source: JuliaLang/julia

A common usage of parse (at least for me) is for reading configuration files.
For boolean flags we can encounter either a "true" string or a "1" (or "false" / "0").
But parsing of AbstractString to Bool with numeric text doesn't work (it works with Chars '0' and '1').

julia> a = split("test: 0")
2-element Array{SubString{String},1}:
 "test:"
 "0"

julia> parse(Bool,a[2])
ERROR: ArgumentError: invalid Bool representation: "0"
Stacktrace:
 [1] macro expansion at ./gcutils.jl:88 [inlined]
 [2] tryparse_internal(::Type{Bool}, ::SubString{String}, ::Int64, ::Int64, ::Int64, ::Bool) at ./parse.jl:188
 [3] #parse#333(::Nothing, ::Function, ::Type{Bool}, ::SubString{String}) at ./parse.jl:225
 [4] parse(::Type{Bool}, ::SubString{String}) at ./parse.jl:225
 [5] top-level scope at none:0

julia> versioninfo()
Julia Version 1.0.1
Commit 0d713926f8 (2018-09-29 19:05 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin14.5.0)
  CPU: Intel(R) Core(TM) i5-5287U CPU @ 2.90GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.0 (ORCJIT, broadwell)
Environment:
  JULIA_NUM_THREADS = 2

It is not difficult to work around this, but wouldn't it make sense to allow parsing of "0" and "1" (of any AbstractString type) to Bool?

Most helpful comment

Seems reasonable to me given that we have Bool <: Integer that one should be able to parse integer syntax for booleans.

>All comments

Seems reasonable to me given that we have Bool <: Integer that one should be able to parse integer syntax for booleans.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iamed2 picture iamed2  路  3Comments

arshpreetsingh picture arshpreetsingh  路  3Comments

omus picture omus  路  3Comments

sbromberger picture sbromberger  路  3Comments

manor picture manor  路  3Comments