Ok, I understand now, *both *variables have to be of type Ref:

using Base.RefValue

type A
  x::RefValue{Float64}
end
type B
  x::RefValue{Float64}
end

a=A(Ref(1.))
b=B(a.x)
a.x[]=2
assert(b.x[] == 2)

That was a little tricky, but in the end doesn't look bad. Thanks for your
help!


Il giorno dom 4 ott 2015 alle ore 02:37 Carlo Lucibello <
[email protected]> ha scritto:

> Sorry, maybe I'm missing something,  but this is what I need and I cannot
> see how to obtain it using Ref:
>
>
> class A{
> public:
>     double x;
> }
>
> class B{
> public:
>     double& x;
> }
> A a(1);
> B b(a.x);
> a.x=2;
> assert(b.x == 2);
>
> In other words, I need two objects to have access to the same memory
> location. That's where Ref/Ptr should come to rescue, but I cannot find the
> correct sintax to do that, and i doubt it exists at all. Can you translate
> the above example to Julia?
>
> Il giorno dom 4 ott 2015 alle ore 02:01 Yichao Yu <[email protected]> ha
> scritto:
>
>> On Sat, Oct 3, 2015 at 6:53 PM, Carlo Lucibello
>> <[email protected]> wrote:
>> >
>> >
>> > Il giorno sab 3 ott 2015 alle ore 19:56 Yichao Yu <[email protected]>
>> ha
>> > scritto:
>> >>
>> >> On Sat, Oct 3, 2015 at 1:36 PM, Carlo Lucibello
>> >> <[email protected]> wrote:
>> >> > Ok, thanks, we are getting closer to what I need, but still...
>> >> >
>> >> > julia> a=1
>> >> > 1
>> >> > julia> b=Ref(a)
>> >> > Base.RefValue{Int64}(1)
>> >> > julia> b[]+=3
>> >> > 4
>> >> > julia> a
>> >> > 1
>> >> >
>> >> > while I was expecting a==4
>> >>
>> >> This is not allowed. All the assignments has to be explicit and you
>> >> should either just use `b[]` to replace `a` or update `a` with `a =
>> >> b[]`.
>> >
>> > The pattern I showed in the first post it's not uncommon, and easily
>> > achieved in C/C++ through pointers/references.  Achieving a similar
>> > functionality in julia, by any way I can think of, would be tedious,
>> > inelegant and slower, so I think this is a serious lack of the
>> language.I
>>
>> I don't see why it's "tedious, inelegant and slower"
>>
>> tedious:
>>     There are way more difference between julia and c++
>> inelegant:
>>     The Ref type is pretty much like a c pointer or a c++ Ref. I don't
>> see why `b[]` is so much different from `*b` when you have a pointer
>> in c
>> slower:
>>     Quite the opposite. If we change the simple meaning of assignment,
>> it will immediately kill the performance of any code with any kinds of
>> type instability
>>
>> > hope there will be some way in the future to accomadate this use case
>>
>> Just use a Ref type anywhere you want a c pointer or a variable that
>> you want to take reference of.
>>
>> >>
>> >> >
>> >> > Il giorno sab 3 ott 2015 alle ore 19:16 Yichao Yu <[email protected]>
>> ha
>> >> > scritto:
>> >> >>
>> >> >> On Sat, Oct 3, 2015 at 12:48 PM, Carlo Lucibello
>> >> >> <[email protected]> wrote:
>> >> >> > Those don't seem to be viable solutions. In fact
>> >> >> >
>> >> >> > julia> a=2
>> >> >> > 2
>> >> >> >
>> >> >> > julia> b=Ref(a)
>> >> >> > Base.RefValue{Int64}(2)
>> >> >> >
>> >> >> > julia> b += 3
>> >> >> > ERROR: MethodError: `+` has no method matching
>> >> >> > +(::Base.RefValue{Int64},
>> >> >> > ::Int64)
>> >> >>
>> >> >> b[] += 3
>> >> >>
>> >> >> >
>> >> >> > and also how do you define a 0-dimensional array?
>> >> >> >
>> >> >> > a=Array{Float64,0}()
>> >> >> > ERROR: MethodError: `convert` has no method matching
>> >> >> > convert(::Type{Array{Float64,0}})
>> >> >> > This may have arisen from a call to the constructor
>> >> >> > Array{Float64,0}(...),
>> >> >> > since type constructors fall back to convert methods.
>> >> >> >
>> >> >> >
>> >> >> >
>> >> >> > Il giorno sabato 3 ottobre 2015 18:07:51 UTC+2, Jameson ha
>> scritto:
>> >> >> >>
>> >> >> >> the `Ref` type or the single-valued `Array{T,0}` provides this
>> >> >> >> ability
>> >> >> >> for
>> >> >> >> Julia.
>> >> >> >>
>> >> >> >> On Fri, Oct 2, 2015 at 5:10 PM Carlo Lucibello
>> >> >> >> <[email protected]>
>> >> >> >> wrote:
>> >> >> >>>
>> >> >> >>> Hi julians,
>> >> >> >>> I'd like to emulate the following behaviour from C++
>> >> >> >>>
>> >> >> >>> class A{
>> >> >> >>> public:
>> >> >> >>>     double x;
>> >> >> >>> }
>> >> >> >>>
>> >> >> >>> class B{
>> >> >> >>> public:
>> >> >> >>>     double& x;
>> >> >> >>> }
>> >> >> >>> A a(1);
>> >> >> >>> B b(a.x);
>> >> >> >>> a.x=2;
>> >> >> >>> assert(b.x == 2);
>> >> >> >>>
>> >> >> >>> Whilie it would be easy to obtain this behaviour with
>> `composite`
>> >> >> >>> types,
>> >> >> >>> since they are passed by referece, for elementary types, such as
>> >> >> >>> Float64,
>> >> >> >>> thi is not possible since they have a sort of by vlue semantic
>> >> >> >>> (yes, I
>> >> >> >>> know
>> >> >> >>> it's not exactly like that, but still...)
>> >> >> >>>
>> >> >> >>> Greetings,
>> >> >> >>> Carlo
>>
>

Reply via email to