Casting by assigning to the right ...
Manfred Nowak
svv1999 at hotmail.com
Wed Apr 15 16:56:35 UTC 2020
On Wednesday, 15 April 2020 at 14:42:11 UTC, Paul Backus wrote:
> If that's unacceptable, the next clearest way is to use words:
> lhs.castAssign(rhs);
> If you insist on inventing a new symbol, [...]
I don't.
As well as it is legal Dlang to write your
lhs.castAssign(rhs)
it is legal Dlang to write
lhs._(rhs);
or
lhs._ = rhs;
or
rhs.castAssignToParameterByUsingLongWord(lhs);
As long as the used identifiers have no predefined and
unchangeable meaning every reader will anyway have to lookup the
meaning of a particular identifier. Usually software engineers
have the duty to build the dictionary for that lookup and it is
the duty of the language to shield that dictionary against
changes.
But in Dlang even unchanged operators have no fixed meaning. For
this please look for restrictions in the specs for the
overloading of the assignment operator
(https://dlang.org/spec/operatoroverloading.html#assignment).
One might recognize, that there are no restrictions for structs.
Therefore it is legal for an instance `lhs' of a struct to write
lhs = rhs;
and thereby de facto assigning to the variable rhs.
Please note that the second requirement for
(https://dlang.org/spec/expression.html#assignment_operator_expressions)
is fulfilled too.
---
struct S{
int data;
void opAssign( ref int arg){
arg= data;
}
}
void main(){
auto s= S( 42);
int i= 101;
s = i;
import std.stdio;
writeln( i); // 42
}
More information about the Digitalmars-d
mailing list