@nogc with assoc array

FG via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Feb 16 11:12:40 PST 2015


On 2015-02-16 at 18:58, Benjamin Thaut wrote:
> Am 16.02.2015 um 18:55 schrieb Jonathan Marler:
>> Why is the 'in' operator nogc but the index operator is not?
>>
>> void main() @nogc
>> {
>>      int[int] a;
>>      auto v = 0 in a; // OK
>>      auto w = a[0];   // Error: indexing an associative
>>                       // array in @nogc function main may
>>                       // cause GC allocation
>> }
>
> Because the index operator throws a OutOfRange exception and throwing exceptions allocates, maybe?

Range violation is an Error, but never mind that. The real question is: given all the work related to @nogc, wouldn't it be better for such common Errors to be preallocated and only have file and line updated when they are thrown?

@nogc already, because they simply cast typeid(OutOfMemoryError).init or typeid(InvalidMemoryOperationError).init:
extern (C) void onOutOfMemoryError(void* pretend_sideffect = null) @trusted pure nothrow @nogc
extern (C) void onInvalidMemoryOperationError(void* pretend_sideffect = null) @trusted pure nothrow @nogc

Could be made @nogc with one object of each kind preallocated:
extern (C) void onAssertError( string file = __FILE__, size_t line = __LINE__ ) nothrow
extern (C) void onRangeError( string file = __FILE__, size_t line = __LINE__ ) @safe pure nothrow
extern (C) void onSwitchError( string file = __FILE__, size_t line = __LINE__ ) @safe pure nothrow



More information about the Digitalmars-d-learn mailing list