On Friday, 21 November 2014 at 03:36:30 UTC, Jonathan Marler
wrote:
Has the idea of function overloading via nogc been explored?
void func() @nogc
{
// logic that does not use GC
}
void func()
{
// logic that uses GC
}
void main(string[] args) // @nogc
{
// if main is @nogc, then the @nogc version of func
// will be called, otherwise, the GC version will be
func();
}
This could be useful for the standard library to expose
different implementations based on whether or not the
application is using the GC.
If you need temporary allocations on the heap, and already have
an implementation using nogc anyways, it's better to do that even
for the GC version.
The only difference I can see would be in returning values, and
having whether the returned memory is managed by the GC or by the
caller be based off the overload seems extremely dangerous.
Template functions in particular could be either nogc or not with
inference, and making the caller nogc shouldn't make it suddenly
leak memory.
Instead consider passing in an allocator. Then it's explicit,
more flexible, and more different.