How can we make it easier to experiment with the compiler?

Walter Bright newshound2 at digitalmars.com
Thu May 27 08:41:49 UTC 2021


On 5/27/2021 12:14 AM, Mathias LANG wrote:
> Now to talk about what can be done to improve the DMD codebase, it's fairly 
> obvious: ELIMINATE ALL CASTS. But not by replacing `cast(XXX)e` with 
> `e.isXXX()`, but by actually using proper encapsulation.
> 
> What I mean is that instead of switching on types, like this:
> ```D
> if (auto tf = t.isTypeFunction())
>      (cast(FunctionDeclaration)t.sym).something();
> else if (auto td = t.isTypeDelegate())
> (cast(FunctionDeclaration)(cast(TypeFunction)t).sym).something();
> else
>      // Something else
> ```

The isXXX() functions also make for safe casting. Your example would be:

  if (auto tf = t.isTypeFunction())
       tf.sym.something();
  else if (auto td = t.isTypeDelegate())
       t.isTypeFunction().sym.isFunctionDeclaration().something();


> There are simple areas where one can start, for example making all aggregate 
> have the most specialized type possible: `TypeDelegate.nextOf()` should return a 
> `TypeFunction`, not a `Type`. `FunctionDeclaration.type()` should be a property 
> that gives you a `TypeFunction`, not a `Type`, etc...

FunctionDeclaration.type() can also give you a TypeError.



More information about the Digitalmars-d mailing list