Silicon Valley D Meetup January 28, 2016
Adam D. Ruppe via Digitalmars-d-announce
digitalmars-d-announce at puremagic.com
Thu Jan 28 20:37:37 PST 2016
I've been listening in on this and the talk about @nogc on
constructors highlights the need to me of having more attribute
inference. Yes, it works on templates, but not all methods are
templates.
int add(int a, int b) { return a+b; }
OK, cool, but what if you want to call that in a @nogc context?
Or a nothrow context? Or a @safe context? Won't work!
@safe nothrow @nogc
int add(int a, int b) { return a+b; }
It just gets silly listing it like that. What abotu
import std.conv;
class Foo {
this(int a) { this.a = a; }
int a;
}
@nogc void foo() {
ubyte[__traits(classInstanceSize, Foo)] buffer;
Foo foo = emplace!Foo(buffer[], 2);
}
That won't work because the ctor is not explicitly marked @nogc!
l.d(9): Error: @nogc function 'l.foo' cannot call non- at nogc
function 'std.conv.emplace!(Foo, int).emplace'
Since emplace itself is a template, it isn't the problem... it is
Foo's constructor. It isn't a template and isn't marked @nogc, so
it won't propagate.
You can fix it by marking `@nogc this(int a) {....}`
But isn't this getting silly? Everything needs half a dozen
attributes!
It'd be nice if we could do some kind of implicit cast in the
template if the function body is available for inspection. Or
something.
In the real world, I rarely see people defensively putting @nogc
on functions like this, but that limits down the line :(
More information about the Digitalmars-d-announce
mailing list