Skip to content

parse(Bool,"0") doesn't work #29980

@favba

Description

@favba

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions