On Sat, Nov 7, 2015 at 9:06 AM, andrew cooke <[email protected]> wrote:
>
> This is just blowing my mind. Can anyone explain it?
>
> To be clear - adding the {N} means that the "copy constructor" no longer
> copies. See how mutating Foo alters both "instanced", and they both have
> the same object reference.
>
You only defined inner constructor for Foo and the "copy" method you
defined is actually a method to call `Foo{N}` (more specifically
`call{N}(::Type{Foo{N}}, x::Foo)`)
When you call `Foo(f)`, you are calling `Foo` and not `Foo{2}` (i.e.
`call(::Type{Foo}, x::Foo{2})`) and it falls back to the no-op type
conversion. Use `which` or `@which` to figure out what you are
calling.
>
> andrew@laptop:~/.julia/v0.4/GoCL> julia-0.4
> _
> _ _ _(_)_ | A fresh approach to technical computing
> (_) | (_) (_) | Documentation: http://docs.julialang.org
> _ _ _| |_ __ _ | Type "?help" for help.
> | | | | | | |/ _` | |
> | | |_| | | | (_| | | Version 0.4.1-pre+22 (2015-11-01 00:06 UTC)
> _/ |\__'_|_|_|\__'_| | Commit 669222e (6 days old release-0.4)
> |__/ | x86_64-suse-linux
>
> julia> immutable Foo{N}
> a
> Foo() = new(zeros(UInt8, N))
> Foo(x::Foo) = new(copy(x.a))
> end
>
> julia> f = Foo{2}()
> Foo{2}(UInt8[0x00,0x00])
>
> julia> f2 = Foo(f)
> Foo{2}(UInt8[0x00,0x00])
>
> julia> immutable Bar
> a
> Bar() = new(zeros(UInt8, 2))
> Bar(x::Bar) = new(copy(x.a))
> end
>
> julia> b = Bar()
> Bar(UInt8[0x00,0x00])
>
> julia> b2 = Bar(b)
> Bar(UInt8[0x00,0x00])
>
> julia> for x in (f, f2, b, b2)
> println(x)
> println(pointer_from_objref(x))
> end
> Foo{2}(UInt8[0x00,0x00])
> Ptr{Void} @0x00007f5f10d6eb20
> Foo{2}(UInt8[0x00,0x00])
> Ptr{Void} @0x00007f5f10d6eb20
> Bar(UInt8[0x00,0x00])
> Ptr{Void} @0x00007f5f1031e530
> Bar(UInt8[0x00,0x00])
> Ptr{Void} @0x00007f5f10e85f80
>
> julia> f.a[1] = 7
> 7
>
> julia> f2
> Foo{2}(UInt8[0x07,0x00])
>
> julia> b.a[1] = 7
> 7
>
> julia> b2
> Bar(UInt8[0x00,0x00])
>
>
>