If you could make any changes to D, what would they look like?

SealabJaster sealabjaster at gmail.com
Wed Oct 20 09:47:54 UTC 2021


Just for giggles, without pesky things like breaking changes; 
rational thinking, logical reasoning behind the changes, etc.

What interesting changes would you make to the language, and what 
could they possibly look like?

Here's a small example of some things I'd like.

```d
import std;

interface Animal
{
     void speak(string language);
}

struct Dog
{
     @nogc @nothrow @pure @safe
     static void speak(string l)
     {
         // Pattern matching of some kind
         // With strings this is just a fancy switch statement, 
but this is the gist of it
         match l with
         {
             "english" => writeln("woof"),
             "french" => writeln("le woof"),
             _ => writeln("foow")
         }
     }
}

struct Cat
{
     // Remove historical baggage. Make old attributes into `@` 
attributes
     @nogc @nothrow @pure @safe
     static void speak()
     {
         writeln("meow")
     }
}

// ? for "explicitly nullable"
void doSpeak(alias T)(string? language)
if(is(T == struct) && match(T : Animal)) // Match structs against 
an interface.
{
     // immutable by default. ?? is the same as in C#
     auto lang = language ?? "UNKNOWN";

     // So of course we'd need a mutable keyword of some sort
     mutable output = $"{__traits(identifier, T)} speaking in 
{lang}"; // String interpolation
     writeln(output);
     T.speak(lang);
}

void main()
{
     doSpeak!Dog;
     doSpeak!Cat; // Should be a compiler error since it fails the 
`match` statement
}
```


More information about the Digitalmars-d mailing list