Expanding the horizons of D purity
Timon Gehr
timon.gehr at gmx.ch
Fri Nov 1 07:24:54 PDT 2013
On 11/01/2013 03:21 PM, Kenji Hara wrote:
> 2013/11/1 Timon Gehr <timon.gehr at gmx.ch <mailto:timon.gehr at gmx.ch>>
>
> On 11/01/2013 02:36 PM, Kenji Hara wrote:
>
> I think this is a good discovery. Currently a pure function can have
> lazy parameters and it is treated as a weakly pure function.
>
> pure int foo(lazy int x) { return x; } // OK
>
> We can think the lazy parameter is a limited case of scope delegate
> parameter.
> ...
>
>
> They are. I think it would be quite strange to treat them differently.
>
>
> I've also felt same thing about it. The proposal will generalize the
> language rule.
>
> And more, I discovered that the purity may be stronger depends
> on the
> given delegate purity.
>
> void func(scope void delegate(int) dg) pure;
>
> void main() {
> int num;
>
> // the function call has weak purity
> func((x){ num = x;});
>
> // the function call has strong purity
> func((x){ ; });
> }
>
> Kenji Hara
>
>
> Yes. Furthermore, the first delegate should have inferred type 'void
> delegate(int x)pure nothrow @safe' and the second delegate should
> have inferred type 'void delegate(int x)pure immutable nothrow @safe'.
>
>
> The first delegate should not become pure.
Why not? It is weakly pure, like the following:
void main(){
struct S{
int num;
void member(int x)pure{ num = x; } // pure
}
S s;
func(&s.member); // weakly pure function call
}
> So it would become 'void
> delegate(int)nothrow @safe'. I agree with the second inference result.
>
> Kenji Hara
More information about the Digitalmars-d
mailing list