# Parsing Strings as Strings

**URL:** <https://discourse.julialang.org/t/parsing-strings-as-strings/13833>\
**Category:** New to Julia\
**Tags:** strings, parsing\
**Created:** [August 21, 2018, 1:45pm UTC](https://discourse.julialang.org/t/parsing-strings-as-strings/13833 "2018-08-21T13:45:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nphair](https://avatars.discourse-cdn.com/v4/letter/n/d2c977/32.png) [@nphair](https://discourse.julialang.org/u/nphair)\
**Post date:** [August 21, 2018, 1:45pm UTC](https://discourse.julialang.org/t/parsing-strings-as-strings/13833/1 "2018-08-21T13:45:24Z")

</div>

I was playing around with parse the other day and was a bit surprised by,

```julia
julia> str = "foo"
julia> parse(String, str)
ERROR: MethodError: no method matching parse(::Type{String}, ::String)

```

and similarly,

```julia
julia> str = "foo"
julia> sstr = SubString(str, 1, 2)
julia> parse(String, sstr)
ERROR: MethodError: no method matching parse(::Type{String}, ::SubString{String})

```

IMHO, having a parse function that can handle these two cases seems like a reasonable behavior and can be easily implemented with,

```julia
parse(::Type{String}, str::String) = str
parse(::Type{String}, str::SubString{String}) = String(str)

```

With that said, I trust there is a reason this is not implemented. My question is, what is that reason?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 21, 2018, 1:53pm UTC](https://discourse.julialang.org/t/parsing-strings-as-strings/13833/2 "2018-08-21T13:53:35Z")

</div>

Parse is not conversion, it is not supposed to be no-op.

And what does parsing to string even mean? The closest meaning will be unquoting a string syntax, which isn’t a no-op. It is not very universal and very syntax dependent though (different language have very different quoting and escape rules).

---

<div class="post-metadata">

**Author:** ![owiecc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/owiecc/32/8894_2.png) [@owiecc](https://discourse.julialang.org/u/owiecc)\
**Post date:** [February 18, 2021, 9:46am UTC](https://discourse.julialang.org/t/parsing-strings-as-strings/13833/3 "2021-02-18T09:46:54Z")

</div>

How to implement this kind of operation, without resorting to the above mentioned extension of `parse`?

```julia
map(x -> parse(x...), zip((Int64,Float64,Float64),("123","234","234.7")))
map(x -> parse(x...), zip((Int64,Float64,String),("123","234","234.7")))

```

The first line is executed correctly but the second one has this corner case of parsing one `String` into a `String`.

I stumbled upon this when using a regex. Can I somehow type annotate regex capture groups to automatically parse certain groups as integers or floats?

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [February 18, 2021, 9:53am UTC](https://discourse.julialang.org/t/parsing-strings-as-strings/13833/4 "2021-02-18T09:53:59Z")

</div>

A clean solution would be to implement a new function:

```julia
my_parse(T, x) = ((T <: AbstractString) && (x isa AbstractString)) ? x : parse(T, x)
map(x -> my_parse(x...), zip((Int64,Float64,String),("123","234","234.7")))

```

---

<div class="post-metadata">

**Author:** ![owiecc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/owiecc/32/8894_2.png) [@owiecc](https://discourse.julialang.org/u/owiecc)\
**Post date:** [February 18, 2021, 12:37pm UTC](https://discourse.julialang.org/t/parsing-strings-as-strings/13833/5 "2021-02-18T12:37:18Z")

</div>

Would it be ok to `import Base.parse` in my own module and then extend it with

```julia
parse(String, str::AbstractString) = str

```

or would that cause some issues? I am not exactly sure how the namespaces work in this situations.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 18, 2021, 12:43pm UTC](https://discourse.julialang.org/t/parsing-strings-as-strings/13833/6 "2021-02-18T12:43:04Z")

</div>

> [@owiecc](#):
>
> Would it be ok to `import Base.parse` in my own module and then extend it…

That would be a bad idea, see [Style Guide · The Julia Language](https://docs.julialang.org/en/v1/manual/style-guide/#Avoid-type-piracy). Just use the little wrapper function or define you own:

```julia
custom_parse(T, x::AbstractString) = parse(T, x)
custom_parse(::Type{T}, x::AbstractString) where {T <: AbstractString} = T(x)

```

---

<div class="post-metadata">

**Author:** ![owiecc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/owiecc/32/8894_2.png) [@owiecc](https://discourse.julialang.org/u/owiecc)\
**Post date:** [February 18, 2021, 1:01pm UTC](https://discourse.julialang.org/t/parsing-strings-as-strings/13833/7 "2021-02-18T13:01:34Z")

</div>

Ah, I wasn’t sure if my extended function “leaks” outside of my module. Thank you both.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 18, 2021, 1:31pm UTC](https://discourse.julialang.org/t/parsing-strings-as-strings/13833/8 "2021-02-18T13:31:15Z")

</div>

Method extensions are “global”.
