Discussion Thread: DIP 1029--Add throw as Function Attribute--Final Review

Jacob Carlborg doob at me.com
Sat Apr 18 12:03:46 UTC 2020


On 2020-04-17 13:28, Mike Parker wrote:
> This is the discussion thread for the Final Review of DIP 1029, "Add 
> throw as Function Attribute":
> 
> https://github.com/dlang/DIPs/blob/9db80ddadcf5a220958ddcfec14b9c71cdb43d1c/DIPs/DIP1029.md 

I would like to see the "throw" keyword being used as an attribute to 
implement something like "Zero-overhead deterministic exceptions: 
Throwing values" [1].

I imagine it looking something like this:

enum CopyError
{
     permissionDenied
}

void copy(string src, string dest) throw(CopyError)
{
     throw CopyError.permissionDenied;
}

void main()
{
     try
         copy("foo", "bar");
     catch (CopyError e)
         writeln(e);
}

Which would be lowered to something like the equivalent of the following 
code:

struct Result(Value, Error)
{
     bool isValue;
     union
     {
         Value value;
         Error error;
     }

     this(Value value)
     {
         this.value = value;
         isValue = true;
     }

     this(Error errro)
     {
         this.error = error;
         isValue = true;
     }
}

Result!(void, CopyError) copy(string src, string dest)
{
     return Result!(void, CopyError)(CopyError.permissionDenied);
}

void main()
{
     auto result = copy("foo", "bar");

     if (!result.isValue)
         goto L1;
     L1:
         writeln(result.error);
}


[1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0709r0.pdf

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list