Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
June 28, 2015 Map Purity | ||||
---|---|---|---|---|
| ||||
My understanding of pure is that a function labeled pure can only include pure functions. I've been confused by the fact that a function calling map (like below) can be labeled pure without any problems. The only way I can rationalize it is that map really isn't a function, it's (if I'm understanding it correctly) a template that creates a template function that calls a struct template. auto test_map(T)(T x) pure { return x.map!(a => a + a); } This is related to http://forum.dlang.org/thread/ppmokalxdgtszzllzorb@forum.dlang.org?page=1 but maybe my question is more basic. |
June 28, 2015 Re: Map Purity | ||||
---|---|---|---|---|
| ||||
Posted in reply to jmh530 | On Sunday, 28 June 2015 at 15:55:51 UTC, jmh530 wrote:
> My understanding of pure is that a function labeled pure can only include pure functions. I've been confused by the fact that a function calling map (like below) can be labeled pure without any problems. The only way I can rationalize it is that map really isn't a function, it's (if I'm understanding it correctly) a template that creates a template function that calls a struct template.
>
> auto test_map(T)(T x) pure
> {
> return x.map!(a => a + a);
> }
>
> This is related to
> http://forum.dlang.org/thread/ppmokalxdgtszzllzorb@forum.dlang.org?page=1
> but maybe my question is more basic.
Map isn't explicitly marked as pure. D can infer purity for templated functions, so as long as you give it a pure predicate, map can be inferred as pure. This only works for templated functions; non-templated functions must be explicitly marked as pure.
|
June 28, 2015 Re: Map Purity | ||||
---|---|---|---|---|
| ||||
Posted in reply to jmh530 | On Sunday, 28 June 2015 at 15:55:51 UTC, jmh530 wrote: > My understanding of pure is that a function labeled pure can only include pure functions. I've been confused by the fact that a function calling map (like below) can be labeled pure without any problems. The only way I can rationalize it is that map really isn't a function, it's (if I'm understanding it correctly) a template that creates a template function that calls a struct template. > > auto test_map(T)(T x) pure > { > return x.map!(a => a + a); > } > > This is related to > http://forum.dlang.org/thread/ppmokalxdgtszzllzorb@forum.dlang.org?page=1 > but maybe my question is more basic. There are two aspects to that. First, purity in D is less strict than the concept in functional programming languages. It allows access (even mutation) to anything that is reachable through the parameters. David Nadlinger has written a nice article about the concept and its implications: http://klickverbot.at/blog/2012/05/purity-in-d/ Secondly, `map` is indeed a template function, as you write. For templates functions, the compiler infers many properties, including purity. Neither the `map` function nor the constructor of the struct it returns do anything impure, therefore they are treated as pure. And indeed, the return value of `test_map` will only depend on its arguments, which is the first requirement for purity, and of course it doesn't change global state either, which fulfills the second requirement. |
June 28, 2015 Re: Map Purity | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | On Sunday, 28 June 2015 at 16:15:31 UTC, Marc Schütz wrote:
>
> Secondly, `map` is indeed a template function, as you write. For templates functions, the compiler infers many properties, including purity.
Thanks for the reply. Two follow ups: 1) Does labeling a template as pure matter if the compiler infers it anyway? 2) Does the compiler also infer anything for @safe/nothrow in templates?
|
June 28, 2015 Re: Map Purity | ||||
---|---|---|---|---|
| ||||
Posted in reply to jmh530 | On Sunday, 28 June 2015 at 16:28:20 UTC, jmh530 wrote:
> Thanks for the reply. Two follow ups: 1) Does labeling a template as pure matter if the compiler infers it anyway? 2) Does the compiler also infer anything for @safe/nothrow in templates?
1) It means you can't instantiate the template function with arguments that would make it non-pure.
2) Yes, pure, nothrow, @safe, @nogc are all inferred.
|
Copyright © 1999-2021 by the D Language Foundation