Contract error string generation is not implicitly @nogc

Paul Backus snarwin at gmail.com
Sun Sep 4 14:54:52 UTC 2022


On Sunday, 4 September 2022 at 14:30:51 UTC, IGotD- wrote:
> On Sunday, 4 September 2022 at 14:06:44 UTC, Ali Çehreli wrote:
>> void foo(string s) @safe pure nothrow @nogc
>> in (s != "hello", "Invalid " ~ s) {
>> }
>>
>> void main() {}
>>
>> Error: cannot use operator `~` in `@nogc` function `deneme.foo`
>>
>> But that operator is used only when we are about to throw an 
>> Error, which is supposed to imply we shouldn't continue with 
>> the program anyway. So, just one little GC allocation wouldn't 
>> hurt, right? :)
>>
>
> ...but what if the GC is totally disabled or not even 
> initialized? This might be the environment you'd want to use 
> @nogc.

If the GC is totally disabled (like in BetterC), the expression 
`"Invalid " ~ s` will not even compile in the first place. So you 
would need to create your error message some other way (perhaps 
with malloc).


More information about the Digitalmars-d mailing list