Option types and pattern matching.

Kagamin via Digitalmars-d digitalmars-d at puremagic.com
Tue Oct 27 00:55:43 PDT 2015


On Monday, 26 October 2015 at 16:42:27 UTC, TheFlyingFiddle wrote:
> If you instead use pattern matching as in your example you have 
> much better context information that can actually help you do 
> something in the case a value is not there.

Probably possible:

Some!T get(T)(Option!T item) {
     Some!T r;
     //Static guarantee of handling value not present
     item match {
         None() => {
             throw new Exception("empty!");
         }
         Some(t) => {
             r=t;
         }
     }

     return r;
}

Then:
Option!File file;
Some!File s = file.get();


More information about the Digitalmars-d mailing list