@nogc, exceptions, generic containers... Issues.

yazd via Digitalmars-d digitalmars-d at puremagic.com
Mon Sep 8 09:46:58 PDT 2014


An incomplete idea regarding exceptions and @nogc that I have is 
to encapsulate the exception within the returned value. And 
whenever the original value is attempted to be read, a check for 
the exception is done. In other words, the exception is not 
thrown from the place where it is constructed, but from the place 
where the return value is accessed.

This provides two advantages. Firstly, a choice can be made by 
the user of the function on whether to throw or not. If the user 
decided to check for the existence of an exception prior to 
accessing the value, the exception (with all of its data, msg, 
file/line info) can be accessed without (the costly) throwing. 
And if the user did not check for the exception, whenever the 
returned value is attempted to be accessed, throwing will occur 
(this gets us ease of use and flexibility).

Secondly, the returned type can contain the exception. This will 
remove the need for the GC and the need for allocating the 
exception on the heap.

Some preliminary code demonstrating the idea:
http://dpaste.dzfl.pl/3d37d524e4c6

The code currently is not @nogc, because emplace is not.

A disadvantage to this is that it cannot be used with functions 
that have purposes other than returning a value. For example, 
writing to a file.


More information about the Digitalmars-d mailing list