Skip to content

Implement some syntax sugars, Optional type and Pattern Matching

New import idiom with improved methodology using compile-time opDispatch() method:

Old code:

import std.stdio.writeln;
writeln("atum");

New code using from struct:

from.std.stdio.writeln("atum");

New code using _from template: Sometimes _from is preferable if you want to pass a string with the desired module on compile-time using some traits or meta programming.

_from!"std.stdio".writeln("atum");

Object type pattern matching using typeid matching:

Imagine this simple hierarchy;

class Animal {}
class Dog : Animal {}
class Cat : Animal {}

Animal obj = new Dog();

Old code:

// ugly code
auto foo = (typeid(obj) == typeid(Dog))
   ? "ouwfh ouwfh"
   : ((typeid(obj) == typeid(Cat))
       ? "I want tuna"
       : "Your dog so but.... ohhh theres no dog.. OhoOhOhOhh"
   );

New code:

// using the beautiful and simple pattern matching code
auto bar = obj.match!(
    (Dog dog) => "ouwfh ouwfh",
    (Cat cat) => "I want tuna",
    () => "Your dog so but.... ohhh theres no dog.. OhoOhOhOhh"
);

Optional type for safety access

This type is inspired on Option from Scala language. This implements a type with no value (None) or something inside of it (Some()). This type is very useful in the sense of safety access. Unlike Nullable type, you don't need to verify the content if it's null. This type acts something like the null-conditional operator from C# foo?.func().

Some code examples:

Optional!int a = some(3);
int b = a.getOr(7);
Optional!int c = a.getOr(some(4));

Safe function call using oc:

class ClassTuna { void func() {} }
auto obj = none!ClassTuna; // equivalent to Optional!ClassTuna obj = ClassTuna.init;
oc(obj).func; // doesn't throw null exception or segmentation fault due to executing .func(), it returns `None`

Other stuff implemented

Inspirations

Edited by Luís Ferreira

Merge request reports