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 hope there will be some way in the future to accomadate this use case > > > > 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 >
