Thread overview
Rosettacode: program termination
Apr 14, 2014
bearophile
Apr 14, 2014
John Colvin
Apr 14, 2014
bearophile
Apr 15, 2014
Andrea Fontana
Apr 15, 2014
bearophile
Apr 15, 2014
Andrea Fontana
Apr 15, 2014
bearophile
Apr 15, 2014
Andrea Fontana
Apr 15, 2014
bearophile
April 14, 2014
I have added a D entry regarding how to terminate D programs. What's missing?

http://rosettacode.org/wiki/Program_termination#D

Bye,
bearophile
April 14, 2014
On Monday, 14 April 2014 at 09:58:09 UTC, bearophile wrote:
> I have added a D entry regarding how to terminate D programs. What's missing?
>
> http://rosettacode.org/wiki/Program_termination#D
>
> Bye,
> bearophile

I think notInifnite is too contrived, you would always just use `else` there. This is more realistic:

int notInfinite(in int b) pure nothrow {
    if (b < 0)
        return 10;
    if (b > 10)
        return 20;

    // In release mode this becomes a halt, and it's sometimes
    // necessary. If you remove this the compiler gives:
    // Error: function test.notInfinite no return exp;
    //    or assert(0); at end of function
    assert(false);
}
April 14, 2014
John Colvin:

> I think notInifnite is too contrived, you would always just use `else` there. This is more realistic:
>
> int notInfinite(in int b) pure nothrow {
>     if (b < 0)
>         return 10;
>     if (b > 10)
>         return 20;
>
>     // In release mode this becomes a halt, and it's sometimes
>     // necessary. If you remove this the compiler gives:
>     // Error: function test.notInfinite no return exp;
>     //    or assert(0); at end of function
>     assert(false);
> }

OK, I have improved the code:
http://rosettacode.org/wiki/Program_termination#D

Bye,
bearophile
April 15, 2014
On Monday, 14 April 2014 at 11:16:41 UTC, bearophile wrote:
> John Colvin:
>
>> I think notInifnite is too contrived, you would always just use `else` there. This is more realistic:
>>
>> int notInfinite(in int b) pure nothrow {
>>    if (b < 0)
>>        return 10;
>>    if (b > 10)
>>        return 20;
>>
>>    // In release mode this becomes a halt, and it's sometimes
>>    // necessary. If you remove this the compiler gives:
>>    // Error: function test.notInfinite no return exp;
>>    //    or assert(0); at end of function
>>    assert(false);
>> }
>
> OK, I have improved the code:
> http://rosettacode.org/wiki/Program_termination#D
>
> Bye,
> bearophile

What about:

static ~this() { "asdasdasd".writeln; }

?
April 15, 2014
Andrea Fontana:

> What about:
>
> static ~this() { "asdasdasd".writeln; }

I have added it.

Bye,
bearophile
April 15, 2014
On Tuesday, 15 April 2014 at 09:30:21 UTC, bearophile wrote:
> Andrea Fontana:
>
>> What about:
>>
>> static ~this() { "asdasdasd".writeln; }
>
> I have added it.
>
> Bye,
> bearophile

http://dpaste.dzfl.pl/33cb0a05f0ff

Static destructor is called!

Application output:
spam at exit
bar at exit
foo at exit
Never called
April 15, 2014
Andrea Fontana:

> http://dpaste.dzfl.pl/33cb0a05f0ff
>
> Static destructor is called!
>
> Application output:
> spam at exit
> bar at exit
> foo at exit
> Never called

Well, we have a little mystery here :-) Using both the latest beta of ldc2 and the latest alpha of dmd, on Windows32 I don't see "Never called" called :-) Perhaps this incongruence is another little compiler bug? :-)

Bye,
bearophile
April 15, 2014
On Tuesday, 15 April 2014 at 09:48:57 UTC, bearophile wrote:
> Andrea Fontana:
>
>> http://dpaste.dzfl.pl/33cb0a05f0ff
>>
>> Static destructor is called!
>>
>> Application output:
>> spam at exit
>> bar at exit
>> foo at exit
>> Never called
>
> Well, we have a little mystery here :-) Using both the latest beta of ldc2 and the latest alpha of dmd, on Windows32 I don't see "Never called" called :-) Perhaps this incongruence is another little compiler bug? :-)
>
> Bye,
> bearophile

I think so. On dpaste using 2.063.2 or git version that function isn't called. Only on 2.065..

More:
import std.stdio;
"Never called".writeln;

This doens't work on git version (only in 2.065). I think it is a known behaviour about "local" import + ufcs. Why does it work on 2.065?




April 15, 2014
Andrea Fontana:

> I think so. On dpaste using 2.063.2 or git version that function isn't called. Only on 2.065..

The C exit() function is not required to honour D module destructors.


> More:
> import std.stdio;
> "Never called".writeln;
>
> This doens't work on git version (only in 2.065). I think it is a known behaviour about "local" import + ufcs. Why does it work on 2.065?

This code should work (because the imported names are not locally defined), and it correctly works with the latest dmd alpha :-)

Bye,
bearophile