D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3833 - pure/nothrow functions/delegates are a subtype of the nonpure/throw ones
Summary: pure/nothrow functions/delegates are a subtype of the nonpure/throw ones
Status: RESOLVED DUPLICATE of issue 3180
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Windows
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-02-18 12:58 UTC by bearophile_hugs
Modified: 2015-06-09 01:27 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2010-02-18 12:58:00 UTC
This compiles, but it's an error because the array is mixed, &sqr is not pure:

void main() {
    double sqr(double x) { return x * x; }
    pure double psqr(double x) { return x * x; }
    pure double delegate(double)[] funcs = [&sqr, &psqr];
}


This doesn't compile, but pure functions are a subsets of impure functions:

pure real sqr1(real x) { return x * x; }
real sqr2(real x) { return x * x; }
void main() {
    real function(real x)[] funcs = [&sqr1, &sqr2];
}



While this compiles:

void main() {
    double sqr(double x) { return x * x; }
    pure double psqr(double x) { return x * x; }
    double delegate(double)[] funcs = [&sqr, &psqr];
}


The following doesn't compile, but nothrow functions are a subset of throwing functions:

nothrow real sqr1(real x) { return x * x; }
real sqr2(real x) {
    if (x == 0) throw new Error("");
    return x * x;
}
void main() {
    real function(real x)[] funcs = [&sqr1, &sqr2];
}


(Similar things happen with "pure nothrow" functions/delegates.)
Comment 1 bearophile_hugs 2010-12-16 00:50:55 UTC
With dmd 2.051beta all four examples don't compile, so the DMD is more strict.
Comment 2 bearophile_hugs 2011-02-02 10:15:39 UTC
This D2 program shows a cost of a strong type system: creating collections of functions becomes a problem because there are so many attributes that change a type. This specific problem is solved managing correctly variance/covariance of function pointers and delegates, typing the array 'a' as an array of impure nothrow @trusted functions:


import std.math;
void main() {
    auto a = [&asin, &cbrt];
}
Comment 3 yebblies 2011-06-08 22:48:20 UTC
I'm going to mark this as a dupe of 3180, along with 4000, as both cases are covered by covariance.

*** This issue has been marked as a duplicate of issue 3180 ***