Proper way to handle "alias this" deprecation for classes

Salih Dincer salihdb at hotmail.com
Wed May 17 14:56:53 UTC 2023


On Wednesday, 17 May 2023 at 08:00:17 UTC, Dom DiSc wrote:
> If you want auto-conversion, you should be more explicit:
> ```d
> float opCast() { }
> ```
> because if you return "auto" it is not the highest-prio fit and 
> therefore not chosen.
> If you have multiple choices, you still don't need to use 
> "auto". Instead you can return T:
> ```d
> T opCast(T)() if(isFloatingPoint!T)
> {
>    return cast(T)num / cast(T)den; // float, double or real
> }
> ```
> Kind of ironic, but especially "auto" does NOT fit 
> automatically :-)

Sorry for not expressing myself better.  Let me try to explain 
with another example:

```d
struct RightShift
{
   int num, shr;

   T opCast(T : char)()
    => cast(T)(num >> shr);
}
import std.stdio;
void main()
{
   auto f = RightShift(128, 2);
   char chr = cast(char)f | '\1';
   assert(chr == '!');
   
   //char test = f; // Error: cannot implicitly convert expression
   //assert(test == ' '); // `f` of type `RightShift` to `char`
}
```

This snippet works fine.  But hidden the code gives an error.  
The reason is that the compiler ignores the explicitly specified 
lines without using auto.

SDB at 79


More information about the Digitalmars-d-learn mailing list