The right way to think about it is that an immutable is like a number: you're 
used to being able to have an array of Float64 and replace those, right? 
You're not redefining the meaning of "5.2", you're overwriting the value stored 
in that slot. Arrays of immutables work exactly the same way: the container is 
not immutable, but the individual values stored in it are. That doesn't 
prevent you from replacing them.

Best,
--Tim

On Tuesday, May 10, 2016 08:43:04 PM Boylan, Ross wrote:
> But if I make an array of immutables I won't be able to change them
> afterwards.  Though I can replace them.
> 
> With the old code except for
> 
> immutable Stuff
>     a::Int
>     b::Int
> end
> 
> I now have
>  julia> v=Array(TT.Stuff, 2)
>  2-element Array{TT.Stuff,1}:
>   TT.Stuff(140124021836848,140124000460928)
>   TT.Stuff(140124048730192,0)
> 
>  julia> v[1].a=44  # what I'd like to do
>  ERROR: type Stuff is immutable
> 
>  julia> v[1]=TT.Stuff(3, 4)  # work-around
>  TT.Stuff(3,4)
> 
> !julia> v
>  2-element Array{TT.Stuff,1}:
>   TT.Stuff(3,4)
>   TT.Stuff(140124048730192,0)
> 
> With immutable, is everything in the array laid out contiguous in memory? 
> It seems sort of odd that the type is immutable yet I can overwrite data of
> that type.  I suppose that would only be prohibited if the array were
> immutable.
> 
> Thanks for the tip about using Type to get the zero to work.
> Ross
> 
> ________________________________
> From: julia-users@googlegroups.com [julia-users@googlegroups.com] on behalf
> of Cedric St-Jean [cedric.stj...@gmail.com] Sent: Tuesday, May 10, 2016
> 1:20 PM
> To: julia-users
> Subject: [julia-users] Re: newbie questions (was undefined reference error)
> 
> "normal" types are by definition heap-allocated, and are always manipulated
> them through pointers. What you want is
> immutables<http://docs.julialang.org/en/release-0.4/manual/types/#immutable
> -composite-types>
> 
> immutable Stuff
>     a::Int
>     b::Int
> end
> 
> # Also, for zeros to work,
> 
> function zero(x::Type{Stuff})
>     Stuff(0, 0)
> end
> 
> 
> On Tuesday, May 10, 2016 at 4:04:37 PM UTC-4, Boylan, Ross wrote:
> I'm puzzled that a type  consisting  only of 2 integers doesn't qualify as
> "bitstype".  Further experiment shows that the array seems to be an array
> of references, I don't know how to implement zero, and generally that I'm a
> bit lost :)  My goal is to get a densely packed array of data.  I assume
> that will use less memory and generate faster code; if not, maybe I should
> change my goal.
> 
> BTW, my real use has a type more heterogeneous than 2 Int's, so a solution
> that uses a 2D array doesn't really generalize appropriately for me.
> 
> module TT
> import Base.zero
> 
> type Stuff
>     a::Int
>     b::Int
> end
> 
> function zero(x::Stuff)
>     Stuff(0, 0)
> end
> 
> end
> 
> julia> v=Array(TT.Stuff, 3)  #as before
>  3-element Array{TT.Stuff,1}:
>   #undef
>   #undef
>   #undef
> 
>  julia> s=TT.Stuff(3, 5)
>  TT.Stuff(3,5)
> 
>  julia> v=fill(s, 2)
>  2-element Array{TT.Stuff,1}:
>   TT.Stuff(3,5)
>   TT.Stuff(3,5)
> 
>  julia> s.a=900
>  900
> 
>  julia> v  ###OOPS: every array element points to the same instance
>  2-element Array{TT.Stuff,1}:
>   TT.Stuff(900,5)
>   TT.Stuff(900,5)
> 
>  julia> zero(TT.Stuff)  # This is probably what needs to work for zeros() to
> work ERROR: MethodError: `zero` has no method matching
> zero(::Type{TT.Stuff})
> 
> !julia> zero(TT.Stuff(1, 1))  # this at least calls the right c'tor
>  TT.Stuff(0,0)
> 
> Ross
> ________________________________________
> From: julia...@googlegroups.com<UrlBlockedError.aspx>
> [julia...@googlegroups.com<UrlBlockedError.aspx>] on behalf of Lutfullah
> Tomak [tomak...@gmail.com<UrlBlockedError.aspx>] Sent: Tuesday, May 10,
> 2016 12:04 AM
> To: julia-users
> Subject: [julia-users] undefined reference error
> 
> You need to initialize array entries if you don't have eltype as bitstype.
> Here, undefined reference means you had not initialize the entry. And, full
> type assignment works because it initializes the entry.

Reply via email to