Casting by assigning to the right ...

aliak something at something.com
Wed Apr 15 12:30:43 UTC 2020


On Wednesday, 15 April 2020 at 09:56:02 UTC, Manfred Nowak wrote:
> On Wednesday, 15 April 2020 at 07:44:23 UTC, aliak wrote:
>> [...] it feels weird.
>
> Yes. But `._=' is legal Dlang.

Yes, but I'm not sure what the legality of syntax has to do with 
readability/maintainability/etc?

>
> In C one would have to annotate the possibility of writing to a 
> parameter by using `&' to get the address of the variable.
>
> Would the legal Dlang phrase `._=&' make the intention more 
> clear?

Not to me.

Custom operators are generally very hard to get right. It's one 
of D's good decisions to limit them (not that they're perfect, 
there are still inconsistencies to fix but aside from that, they 
are a good decision)

Elm recently (well, last year or two was it?) did away with 
allowing cryptic operators as well and there's a writeup by Elm's 
author: 
https://gist.github.com/evancz/769bba8abb9ddc3bf81d69fa80cc76b1



>
> ------
>
> struct S{
>   string data;
>     import std.conv: to;
>   void _( ref int arg){
>     arg= to!int( data) *10;
>   }
>   void _( real* arg){
>     *arg= to!real( data) *10 +1;
>   }
> }
> void main(){
>   auto s= S( "42");
>
>   int i;
>   s  ._=  i;
>
>   real r;
>   s  ._=&  r;
>
>     import std.stdio;
>   writeln( i, ":", r); // 420:421
> }




More information about the Digitalmars-d mailing list