Discussion:
[julia-users] overloading assignment operator
Thorsten Dahmen
2015-04-27 19:25:03 UTC
Permalink
Hi,
is it possible to overload the assignment operator in Julia like in C++. I
cannot really find information whether this is possible or not and if not
why not and if there is an alternative.
Thanks, Thorsten
Toivo Henningsson
2015-04-27 19:52:50 UTC
Permalink
The answer is basically no. Assignment to an identifier always creates a new binding for that identifier, possibly after converting the right hand side to the type of the variable in question.
For an object x,

x[inds...] = a

can be overloaded (this is the setindex! function). At some point it might become possible to overload

x.y = a

As for alternatives: What is your use case?
Thorsten Dahmen
2015-05-03 14:12:12 UTC
Permalink
Hi Toive,
thanks for the quick reply. Concerning my use case, I found that there is
an even easier solution without using an assignment overload at all in the
meantime. But it is good to know that it does not exist. Thus, as far as I
can see, the best alternative is to introduce a function assign(f,x) and
evoke f = x within together with other custom code. That's not too much of
a problem in case I need it in future.
Best,
Thorsten
Post by Toivo Henningsson
The answer is basically no. Assignment to an identifier always creates a
new binding for that identifier, possibly after converting the right hand
side to the type of the variable in question.
For an object x,
x[inds...] = a
can be overloaded (this is the setindex! function). At some point it might
become possible to overload
x.y = a
As for alternatives: What is your use case?
Loading...