# How do I create a new Array-like type with only ints and strings?

**URL:** <https://discourse.julialang.org/t/how-do-i-create-a-new-array-like-type-with-only-ints-and-strings/37711>\
**Category:** New to Julia\
**Created:** [April 16, 2020, 6:28pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-new-array-like-type-with-only-ints-and-strings/37711 "2020-04-16T18:28:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Tantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex_tantos/32/10636_2.png) [@Alex\_Tantos](https://discourse.julialang.org/u/Alex_Tantos)\
**Post date:** [April 16, 2020, 6:28pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-new-array-like-type-with-only-ints-and-strings/37711/1 "2020-04-16T18:28:08Z")

</div>

Hi there!

As am heading towards understanding the type system of Julia, I seem to not be able to do such a thing as to create-define a container type that would include only strings and integers.

Could anyone drop me the relevant code line? It does not seem there is somewhere in the docu that makes this easy…(although I have to admit that Julia’s documentation is one of the most straightforward I have seen and congrats for that!)

Any response with even a short explanation would be greatly appreciated!

Best!

Alex

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [April 16, 2020, 6:32pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-new-array-like-type-with-only-ints-and-strings/37711/2 "2020-04-16T18:32:05Z")

</div>

What about `Array{Union{Int,String},2}` (where 2 makes it a 2d array)

---

<div class="post-metadata">

**Author:** ![Alex\_Tantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex_tantos/32/10636_2.png) [@Alex\_Tantos](https://discourse.julialang.org/u/Alex_Tantos)\
**Post date:** [April 16, 2020, 6:46pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-new-array-like-type-with-only-ints-and-strings/37711/3 "2020-04-16T18:46:08Z")

</div>

Thanks a lot for the quick response!

So, following the union type section of the docu, the following 2 lines should work as expected, right?

```julia
IntString = Array{Union{Int,String},2}
[23 "dd"; "er" 24] :: IntString

```

Why do I receive the message below?

```julia
ERROR: TypeError: in typeassert, expected Array{Union{Int64, String},2}, got Array{Any,2}
Stacktrace:
 [1] top-level scope at none:0

```

There is something missing in my knowledge base related to the scoping and I still cannot figure that out…sorry, for the elementary level of my question…

Thanks again!

Alex

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [April 16, 2020, 6:50pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-new-array-like-type-with-only-ints-and-strings/37711/4 "2020-04-16T18:50:29Z")

</div>

In practice, an `Array{Union{Int,String},2}` often won’t be faster than a `Array{Any,2}` so it doesn’t really matter. The reason `[23 "dd"; "er" 24]` has eltype `Any` is that it is the only direct super-type of `Int` and `String`. Also, just wondering, why do you want to do this?

---

<div class="post-metadata">

**Author:** ![Alex\_Tantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex_tantos/32/10636_2.png) [@Alex\_Tantos](https://discourse.julialang.org/u/Alex_Tantos)\
**Post date:** [April 16, 2020, 6:56pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-new-array-like-type-with-only-ints-and-strings/37711/5 "2020-04-16T18:56:58Z")

</div>

Hmm…Thanks!  
What if I wanted to create a container type that is populated only with elements of these specific two types (and exclude booleans, for instance)?  
Basically, am experimenting these days, since I am thinking of various (research) domain-specific scenarios related to a research project I got funded recently. In general am charmed from the experience of learning Julia and trying to dig in a bit more and explore the possibility of using it for the project’s data analysis stage.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [April 16, 2020, 6:59pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-new-array-like-type-with-only-ints-and-strings/37711/6 "2020-04-16T18:59:43Z")

</div>

> [@Oscar\_Smith](#):
>
> `Array{Union{Int,String},2}`

`x::Array{Union{Int,String},2}= [23 "dd"; "er" 24]` will work in a function (typed globals aren’t supported in Julia yet). That said, doing something like this is almost always a bad idea. Fast code requires type stability.

---

<div class="post-metadata">

**Author:** ![Alex\_Tantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex_tantos/32/10636_2.png) [@Alex\_Tantos](https://discourse.julialang.org/u/Alex_Tantos)\
**Post date:** [April 16, 2020, 7:02pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-new-array-like-type-with-only-ints-and-strings/37711/7 "2020-04-16T19:02:10Z")

</div>

Am sure as am learning more about Julia I will be able to grasp more of the ideas related to type stability and performance!

Thanks again!

Alex

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [April 16, 2020, 7:06pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-new-array-like-type-with-only-ints-and-strings/37711/8 "2020-04-16T19:06:27Z")

</div>

You can also take this approach:

```julia
julia> IntString = Union{Int,String}
Union{Int64, String}

julia> IntString[23 "dd"; "er" 24]
2×2 Array{Union{Int64, String},2}:
 23 "dd"
   "er" 24

```

See [this section](https://docs.julialang.org/en/v1/manual/arrays/#Typed-array-literals-1) of the docs.

---

<div class="post-metadata">

**Author:** ![Alex\_Tantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex_tantos/32/10636_2.png) [@Alex\_Tantos](https://discourse.julialang.org/u/Alex_Tantos)\
**Post date:** [April 16, 2020, 7:13pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-new-array-like-type-with-only-ints-and-strings/37711/9 "2020-04-16T19:13:32Z")

</div>

Thanks a lot @CameronBieganek! That is exactly what I was hoping for! Still on my way of digging into the documentation…

Best!

Alex
