forcing "@nogc" on class destructors

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Tue Jan 20 19:02:53 PST 2015


On 1/20/15 9:04 PM, ketmar via Digitalmars-d wrote:
> On Tue, 20 Jan 2015 20:51:34 -0500
> Steven Schveighoffer via Digitalmars-d <digitalmars-d at puremagic.com>
> wrote:
>
>>>> You can always put @nogc on the dtor if you want.
>>> seems that you completely missing my point. (sigh)
>>
>> Nope, not missing it. The mechanics are there. You just have to annotate.
> that is where you missing it. your answer is like "hey, C has all
> mechanics for doing OOP with virtual methods and type checking, you
> just have to write the code!"

No, actually it's not. Adding @nogc to a function is as hard as writing 
"class" when you want to do OOP.

>
> the whole point of my talk was "free programmer from writing the
> obvious and setup some red tapes for beginners".

If he does it wrong, it gives him a stack trace on where to look. What 
is different here than any other programming error?

>> What's the first thing you do if you aren't sure your destructors are
>> running?
>>
>> ~this() { writeln("in dtor"); }
>>
>> oops, sorry, can't do that, it's @nogc! I don't think this is a tenable
>> situation.
> but it is! first: we can loosen that restriction somehow for `debug`
> parts. your sample should be read like this then:
>
>    ~this () { debug writeln("in dtor"); }
>
> second: it's `writeln` who is bad. one of the reasons that motivated me
> to write my `iv.writer` was that `std.stdio.write` is not `@nogc`, and
> so it was completely unusable in any of my `@nogc` functions. even
> something that simple as `writeln("hi!")` was a disaster (both `@gc`
> and `@canthrow`).

writeln("hi!") may need to extend a buffer, but probably not at this 
point. This is what I mean by "sometimes" allocates.

> this is a sign that Phobos needs some simple output API that can be
> used in `@nogc` and `nothrow` functions without hackery.

You may be able to, but I don't see the point. writeln can work 
perfectly fine for the most part inside dtors. But it can't be marked nogc.

-Steve


More information about the Digitalmars-d mailing list