Thread overview | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 30, 2018 __has_side_effects | ||||
---|---|---|---|---|
| ||||
https://www.reddit.com/r/programming/comments/88aqsf/the_joy_of_max/ Discussion aside, I notice with pleasant surprise gcc has an introspection primitive we didn't think of: __is_constant that (I assume) yields true if the given expression has no side effects. In D the primitive would be called e.g. __has_side_effects to avoid confusion with the "const" qualifier. I wonder how difficult it would be to define __has_side_effects (it would apply to an alias) and how it can be put to good use. Andrei |
March 30, 2018 Re: __has_side_effects | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Friday, 30 March 2018 at 20:28:27 UTC, Andrei Alexandrescu wrote:
> https://www.reddit.com/r/programming/comments/88aqsf/the_joy_of_max/
>
> Discussion aside, I notice with pleasant surprise gcc has an introspection primitive we didn't think of: __is_constant that (I assume) yields true if the given expression has no side effects.
>
> In D the primitive would be called e.g. __has_side_effects to avoid confusion with the "const" qualifier.
>
> I wonder how difficult it would be to define __has_side_effects (it would apply to an alias) and how it can be put to good use.
>
>
> Andrei
It's actually quite easy.
make a function literal annotated with pure and call your function, if that returns the function had no side effects.
if something is constant you can determine by using at in ctfe context, if that function call does not compile then it is not a constant.
|
March 31, 2018 Re: __has_side_effects | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan Koch | On 30/03/18 23:35, Stefan Koch wrote:
> On Friday, 30 March 2018 at 20:28:27 UTC, Andrei Alexandrescu wrote:
>> https://www.reddit.com/r/programming/comments/88aqsf/the_joy_of_max/
>>
>> Discussion aside, I notice with pleasant surprise gcc has an introspection primitive we didn't think of: __is_constant that (I assume) yields true if the given expression has no side effects.
>>
>> In D the primitive would be called e.g. __has_side_effects to avoid confusion with the "const" qualifier.
>>
>> I wonder how difficult it would be to define __has_side_effects (it would apply to an alias) and how it can be put to good use.
>>
>>
>> Andrei
>
> It's actually quite easy.
> make a function literal annotated with pure and call your function, if that returns the function had no side effects.
> if something is constant you can determine by using at in ctfe context, if that function call does not compile then it is not a constant.
That would work in C++. It does not work with D. D is willing to call functions that have side effects "pure".
struct S {
int a;
void func(int b) pure {
// For some strange reason, this is not considered a pure violation.
a+=b;
}
}
|
March 31, 2018 Re: __has_side_effects | ||||
---|---|---|---|---|
| ||||
Posted in reply to Shachar Shemesh | On Saturday, 31 March 2018 at 19:18:24 UTC, Shachar Shemesh wrote:
> struct S {
> int a;
>
> void func(int b) pure {
> // For some strange reason, this is not considered a pure violation.
> a+=b;
> }
> }
It's the exact equivalent of this code:
void func(ref S s, int b) pure {
S.a += b;
}
And that code is perfectly pure accordion to D rules - it does not modify any data not reachable through its arguments.
--
Simen
|
March 31, 2018 Re: __has_side_effects | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjærås | On 3/31/18 4:01 PM, Simen Kjærås wrote:
> On Saturday, 31 March 2018 at 19:18:24 UTC, Shachar Shemesh wrote:
>> struct S {
>> int a;
>>
>> void func(int b) pure {
>> // For some strange reason, this is not considered a pure violation.
>> a+=b;
>> }
>> }
>
> It's the exact equivalent of this code:
>
> void func(ref S s, int b) pure {
> S.a += b;
> }
>
> And that code is perfectly pure accordion to D rules - it does not modify any data not reachable through its arguments.
Yah, only strongly pure functions would qualify. Indeed that's easy for the compiler to figure - so I'm thinking pragma(isStronglyPure, expression) would be easy to define.
What would be some good uses of this?
Andrei
|
April 01, 2018 Re: __has_side_effects | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Saturday, 31 March 2018 at 20:44:13 UTC, Andrei Alexandrescu wrote:
> Yah, only strongly pure functions would qualify. Indeed that's easy for the compiler to figure - so I'm thinking pragma(isStronglyPure, expression) would be easy to define.
>
> What would be some good uses of this?
>
>
> Andrei
Surely you mean __traits(isStronglyPure, expression)?
|
March 31, 2018 Re: __has_side_effects | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nicholas Wilson | On 3/31/18 8:35 PM, Nicholas Wilson wrote:
> On Saturday, 31 March 2018 at 20:44:13 UTC, Andrei Alexandrescu wrote:
>> Yah, only strongly pure functions would qualify. Indeed that's easy for the compiler to figure - so I'm thinking pragma(isStronglyPure, expression) would be easy to define.
>>
>> What would be some good uses of this?
>>
>>
>> Andrei
>
> Surely you mean __traits(isStronglyPure, expression)?
Affirmative, thx for the correction.
|
April 01, 2018 Re: __has_side_effects | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Friday, 30 March 2018 at 20:28:27 UTC, Andrei Alexandrescu wrote: > https://www.reddit.com/r/programming/comments/88aqsf/the_joy_of_max/ > > Discussion aside, I notice with pleasant surprise gcc has an introspection primitive we didn't think of: __is_constant that (I assume) yields true if the given expression has no side effects. > > In D the primitive would be called e.g. __has_side_effects to avoid confusion with the "const" qualifier. > > I wonder how difficult it would be to define __has_side_effects (it would apply to an alias) and how it can be put to good use. > > > Andrei Just for the fun of it, I implemented this as a template: https://run.dlang.io/is/pXLndG |
April 01, 2018 Re: __has_side_effects | ||||
---|---|---|---|---|
| ||||
Posted in reply to Uknown | On Sunday, 1 April 2018 at 04:11:37 UTC, Uknown wrote:
> On Friday, 30 March 2018 at 20:28:27 UTC, Andrei Alexandrescu wrote:
>> https://www.reddit.com/r/programming/comments/88aqsf/the_joy_of_max/
>>
>> Discussion aside, I notice with pleasant surprise gcc has an introspection primitive we didn't think of: __is_constant that (I assume) yields true if the given expression has no side effects.
>>
>> In D the primitive would be called e.g. __has_side_effects to avoid confusion with the "const" qualifier.
>>
>> I wonder how difficult it would be to define __has_side_effects (it would apply to an alias) and how it can be put to good use.
>>
>>
>> Andrei
>
> Just for the fun of it, I implemented this as a template:
>
> https://run.dlang.io/is/pXLndG
You're missing classes, AAs, and pointers (possibly function pointers and delegates as well).
|
April 01, 2018 Re: __has_side_effects | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nicholas Wilson | On Sunday, 1 April 2018 at 04:30:03 UTC, Nicholas Wilson wrote: > On Sunday, 1 April 2018 at 04:11:37 UTC, Uknown wrote: >> On Friday, 30 March 2018 at 20:28:27 UTC, Andrei Alexandrescu wrote: >>> [...] >> >> Just for the fun of it, I implemented this as a template: >> >> https://run.dlang.io/is/pXLndG > > You're missing classes, AAs, and pointers (possibly function pointers and delegates as well). I knew I was missing something. Fixed it, thanks https://run.dlang.io/is/tZeZrP |
Copyright © 1999-2021 by the D Language Foundation