# Why Explore.myRow(::Int64) instead of UInt8

**URL:** <https://discourse.julialang.org/t/why-explore-myrow-int64-instead-of-uint8/2804>\
**Category:** New to Julia\
**Created:** [March 21, 2017, 9:10pm UTC](https://discourse.julialang.org/t/why-explore-myrow-int64-instead-of-uint8/2804 "2017-03-21T21:10:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ilanggear](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ilanggear/32/862_2.png) [@ilanggear](https://discourse.julialang.org/u/ilanggear)\
**Post date:** [March 21, 2017, 9:10pm UTC](https://discourse.julialang.org/t/why-explore-myrow-int64-instead-of-uint8/2804/1 "2017-03-21T21:10:00Z")

</div>

1. In the following snippet, why does the error message report "Explore.myRow(::Int64) " rather than UInt8? (Ignoring the inexactness, that was deliberate to provoke an error message.) I am trying to reduce memory usage, so I’d like to make sure myTable is not wasting memory when it will have a million rows. Is the ‘whos’ command sufficient for verifying this?

2. What difference does it make if I code myTable as a Vector instead of Array? Are they the same?

‘’’  
immutable myRow  
secId::UInt8  
end

myTable=Array{myRow}()

push!(myTable,myRow(-1))  
‘’’  
LoadError: InexactError()  
while loading /Users/mymachine/Projects/juliaNLP/explore.jl, in expression starting on line 35  
in Explore.myRow(::Int64) at explore.jl:30  
in include\_string(::String, ::String) at loading.jl:441  
in include\_string(::String, ::String) at sys.dylib:?  
in include\_string(::Module, ::String, ::String) at eval.jl:32  
in (::Atom.##59#62{String,String})() at eval.jl:81  
in withpath(::Atom.##59#62{String,String}, ::String) at utils.jl:30  
in withpath(::Function, ::String) at eval.jl:46  
in macro expansion at eval.jl:79 [inlined]  
in (::Atom.##58#61{Dict{String,Any}})() at task.jl:60

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 21, 2017, 10:09pm UTC](https://discourse.julialang.org/t/why-explore-myrow-int64-instead-of-uint8/2804/2 "2017-03-21T22:09:05Z")

</div>

I believe it’s because you’ve called `myRow()` with an input of type `Int64`. Julia doesn’t parse the literal `-1` differently just because you’re calling a function that will eventually expect a `UInt8`, so that `-1` becomes an `Int64` as usual.

> 1. What difference does it make if I code myTable as a Vector instead of Array? Are they the same?

Vector is an alias for a 1-dimensional array:

```julia
julia> immutable Foo
       end

julia> Vector{Foo} === Array{Foo, 1}
true

```

Note that your code declares `myTable` as an `Array{myRow}()`, which actually creates a zero-dimensional array (since you haven’t specified the number of dimensions):

```julia
julia> Array{Foo}()
0-dimensional Array{Foo,0}:
Foo()

```

You can explicitly put the number of dimensions, or just use the `Vector` shorthand:

```julia
julia> Vector{Foo}()
0-element Array{Foo,1}

julia> Array{Foo, 1}()
0-element Array{Foo,1}

```

As for memory consumption, I’m afraid I don’t know.

---

<div class="post-metadata">

**Author:** ![ilanggear](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ilanggear/32/862_2.png) [@ilanggear](https://discourse.julialang.org/u/ilanggear)\
**Post date:** [March 21, 2017, 11:30pm UTC](https://discourse.julialang.org/t/why-explore-myrow-int64-instead-of-uint8/2804/3 "2017-03-21T23:30:52Z")

</div>

Thanks for the info about vector being an alias of a 1-dim array.

Back to my first question: do I need to explicity call convert on the parse result to ensure that it becomes UInt8 when stored, or is that happening behind-the-scenes, or is myRow itself being converted to hold Int64s intead of UInt8s?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 21, 2017, 11:36pm UTC](https://discourse.julialang.org/t/why-explore-myrow-int64-instead-of-uint8/2804/4 "2017-03-21T23:36:14Z")

</div>

```julia
julia> immutable myRow
       secId::UInt8
       end

julia> myRow(-1)
ERROR: InexactError()

```

The problem is that the default constructor is trying to `convert``-1` into a `UInt8` in order to go into that field. However, `convert` throws an inexact error because it’s nonsensical:

```julia
julia> convert(UInt8,1)
0x01

julia> convert(UInt8,-1)
ERROR: InexactError()

```

> [@ilanggear](#):
>
> why does the error message report "Explore.myRow(::Int64) "

That’s the method you are invoking. You are invoking `myRow` on an `Int64` (so, you’re on a 64-bit computer). It’s saying that function is returning an error.

> [@ilanggear](#):
>
> I am trying to reduce memory usage, so I’d like to make sure myTable is not wasting memory when it will have a million rows.

It will only store `UInt8`’s, because that’s what you told it to do. Immutables are value types and, other than a small tag, just store your values. It’s not dynamic: it won’t keep extra room just in case you put in more. An immutable, as a value type, also just holds the values, not pointers. When you put them in a vector, the vector will specialize to the type if everything is concretely typed, making it efficient in memory. Many times a function can compile away the abstraction of the immutable too.

> [@ilanggear](#):
>
> Is the ‘whos’ command sufficient for verifying this?

Yes, whos will tell you the memory usage.
