# Tuple vs tuple

**URL:** <https://discourse.julialang.org/t/tuple-vs-tuple/12976>\
**Category:** Internals & Design\
**Created:** [August 7, 2018, 8:52am UTC](https://discourse.julialang.org/t/tuple-vs-tuple/12976 "2018-08-07T08:52:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [August 7, 2018, 8:52am UTC](https://discourse.julialang.org/t/tuple-vs-tuple/12976/1 "2018-08-07T08:52:18Z")

</div>

What is the rationale for Tuple() not working as a constructor as a struct type would?

It’s fine to use `tuple()` instead for general purposes, but it messes with the pattern of using types as general constructors when working in the AST. You have to specifically handle `Tuple` to use `tuple()` instead of just constructing X(args…).

Obviously not going to be changing right now, but it would be good to understand the rationale for it.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [August 7, 2018, 10:30am UTC](https://discourse.julialang.org/t/tuple-vs-tuple/12976/2 "2018-08-07T10:30:48Z")

</div>

I regularly have similar issues with `String` vs `string`. I’m sure there’s a reason, but I can never remember when I should use one vs the other.

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [August 8, 2018, 12:52pm UTC](https://discourse.julialang.org/t/tuple-vs-tuple/12976/3 "2018-08-08T12:52:03Z")

</div>

```julia
julia> Tuple([1, 2, 3])
(1, 2, 3)

julia> tuple([1, 2, 3])
([1, 2, 3],)

```

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [August 8, 2018, 12:58pm UTC](https://discourse.julialang.org/t/tuple-vs-tuple/12976/4 "2018-08-08T12:58:23Z")

</div>

But why is it so 😉

It could easily be the other way around, and this would be more intuitive:

```julia
struct Example
    a
    b
    c
end

julia> typeof(Example(1, 2, 3)).name.wrapper(1.0, 2.0, 3.0)                                            
Example(1.0, 2.0, 3.0)                                                                                  

julia> typeof((1, 2, 3)).name.wrapper(1.0, 2.0, 3.0)                                                    
ERROR: MethodError: no method matching Tuple(::Float64, ::Float64, ::Float64)  

```

There might be counterexample of the opposite kind of unintuitive if it were switched, so maybe it was a coin flip

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [August 8, 2018, 1:14pm UTC](https://discourse.julialang.org/t/tuple-vs-tuple/12976/5 "2018-08-08T13:14:45Z")

</div>

`string` does not guarantee you to return `String`. In general you can get other subtype of `AbstractString`. As an input you can pass any objects.

`String` guarantees that you will get `String` but has a limited number of methods - it will only accept `String`, `SubString{String}`, `Symbol` or several variants of `UInt8` or `AbstractChar` arrays.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [August 8, 2018, 3:21pm UTC](https://discourse.julialang.org/t/tuple-vs-tuple/12976/6 "2018-08-08T15:21:21Z")

</div>

> [@bkamins](#):
>
> `string` does not guarantee you to return `String` . In general you can get other subtype of `AbstractString` . As an input you can pass any objects.

Oh, interesting. I think I’ve only ever used `string()` to get string representations of numbers (`Int`s and `Float`s) and `Regex`s, both of which do return `String`. What are examples where it returns something else?

Looking at this a little more, I think `string()` actually works for _all_ my uses, including those where I currently use `String()`. I wonder if it would be a good idea to just use that function as a general case rather than trying to use the converting version.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [August 8, 2018, 3:51pm UTC](https://discourse.julialang.org/t/tuple-vs-tuple/12976/7 "2018-08-08T15:51:53Z")

</div>

The simplest example from Julia Base is:

```julia
julia> string(s"12")
s"12"

```

So `SubstitutionString{String}` remains a `SubstitutionString{String}`. But it is pretty obscure case. However, when you start using e.g. [https://github.com/JuliaString/Strs.jl](https://github.com/JuliaString/Strs.jl) this would become a serious issue.

Actually there was a recent PR regarding this [Specialize string and repeat for SubString{String} by bkamins · Pull Request #28149 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/28149) (and there you can see why a discussion why `string` does not always return `String` although this is what I have initially proposed 🙂). In Julia 0.6.4 when you run `string` on `SubString{String}` it remains a `SubString{String}`. In Julia 0.7 it will be a `String`.

---

<div class="post-metadata">

**Author:** ![jeff.bezanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff.bezanson/32/48_2.png) [@jeff.bezanson](https://discourse.julialang.org/u/jeff.bezanson)\
**Post date:** [August 11, 2018, 1:17am UTC](https://discourse.julialang.org/t/tuple-vs-tuple/12976/8 "2018-08-11T01:17:11Z")

</div>

There is a convention that when `T` is a collection type, you can construct it from another similar collection using `T(other)`. Examples:

```julia
julia> Array(1:3)
3-element Array{Int64,1}:
 1
 2
 3

julia> Tuple(1:3)
(1, 2, 3)

julia> Dict(i=>i for i in 1:2)
Dict{Int64,Int64} with 2 entries:
  2 => 2
  1 => 1

```

I agree this is somewhat at odds with the default constructors for struct types, which accept field values as separate arguments. However, structs aren’t collections of their fields! A struct might represent something totally different, e.g. a single number.
