digitalmars.D - const/immutable on delegates
- Tofu Ninja (50/50) Jul 02 2015 So this idea spawned from a thread I posted over in learn[1].
- deadalnix (2/6) Jul 02 2015 http://wiki.dlang.org/DIP30
- Timon Gehr (2/8) Jul 02 2015 That's overkill.
- Timon Gehr (2/7) Jul 02 2015 https://issues.dlang.org/show_bug.cgi?id=14745
- Tofu Ninja (8/19) Jul 03 2015 -_-
- deadalnix (3/24) Jul 03 2015 They can't, it is not implemented. It should e possible and pure
- Tofu Ninja (11/13) Jul 03 2015 void main(string[] args)
- Tofu Ninja (17/18) Jul 03 2015 It even gives nice errors when you try to access mutable data
- Timon Gehr (5/23) Jul 03 2015 I assume the related commits are listed here:
So this idea spawned from a thread I posted over in learn[1]. Similar to how you can mark a member function as const or immutable, there should be a way to mark a delegate as const or immutable. For a member function, marking it const/immutable means the this* will be const/immutable. For a delegate, marking it const/immutable should mean that the context* will be const/immutable. This already happens for delegates to member functions, but I think this would be valuable for all delegates in general. Const on a delegate would just implicitly convert everything in the context to const. ie: void foo() { int x = 4; auto bar = () const { x++;// error, x is const }; x++;// Ok } Immutable on a delegate would require every thing in the context to be immutable. Because you can't just implicitly convert to immutable this means any variable use in the context must be immutable. ie: void foo() { int x = 4; immutable int y = 4; auto bar = () immutable { return x;// error, x is not immutable }; auto bar2 = () immutable { return y;// Ok }; } One of the benefits of the immutable attribute would be to ensure strong purity on delegates. For instance currently a delegate marked pure could still change if either A) it modifies its own context, or B) the function that created the context still has not returned and it modifies the context. Being able to annotate it as const fixes A, being able to annotate it as immutable fixes both A and B. Thoughts? [1] http://forum.dlang.org/thread/dpyncfsfjinwjvkzeomk forum.dlang.org
Jul 02 2015
On Friday, 3 July 2015 at 00:26:40 UTC, Tofu Ninja wrote:So this idea spawned from a thread I posted over in learn[1]. Similar to how you can mark a member function as const or immutable, there should be a way to mark a delegate as const or immutable.http://wiki.dlang.org/DIP30
Jul 02 2015
On 07/03/2015 03:18 AM, deadalnix wrote:On Friday, 3 July 2015 at 00:26:40 UTC, Tofu Ninja wrote:That's overkill.So this idea spawned from a thread I posted over in learn[1]. Similar to how you can mark a member function as const or immutable, there should be a way to mark a delegate as const or immutable.http://wiki.dlang.org/DIP30
Jul 02 2015
On 07/03/2015 02:26 AM, Tofu Ninja wrote:So this idea spawned from a thread I posted over in learn[1]. Similar to how you can mark a member function as const or immutable, there should be a way to mark a delegate as const or immutable. ... Thoughts?https://issues.dlang.org/show_bug.cgi?id=14745
Jul 02 2015
On Friday, 3 July 2015 at 02:13:39 UTC, Timon Gehr wrote:On 07/03/2015 02:26 AM, Tofu Ninja wrote:-_- Is that documented anywhere? I can't find anything in the docs that says nested functions can have immutable, const, shared, inout attached to them. This seems to be a wholly unknown and undocumented feature, and the fact that it doesn't work on anonymous delegates seems like it's not even fully implemented, like someone started to implement it and stopped half way though.So this idea spawned from a thread I posted over in learn[1]. Similar to how you can mark a member function as const or immutable, there should be a way to mark a delegate as const or immutable. ... Thoughts?https://issues.dlang.org/show_bug.cgi?id=14745
Jul 03 2015
On Friday, 3 July 2015 at 08:35:53 UTC, Tofu Ninja wrote:On Friday, 3 July 2015 at 02:13:39 UTC, Timon Gehr wrote:They can't, it is not implemented. It should e possible and pure is broken for them.On 07/03/2015 02:26 AM, Tofu Ninja wrote:-_- Is that documented anywhere? I can't find anything in the docs that says nested functions can have immutable, const, shared, inout attached to them. This seems to be a wholly unknown and undocumented feature, and the fact that it doesn't work on anonymous delegates seems like it's not even fully implemented, like someone started to implement it and stopped half way though.So this idea spawned from a thread I posted over in learn[1]. Similar to how you can mark a member function as const or immutable, there should be a way to mark a delegate as const or immutable. ... Thoughts?https://issues.dlang.org/show_bug.cgi?id=14745
Jul 03 2015
On Friday, 3 July 2015 at 10:59:43 UTC, deadalnix wrote:They can't, it is not implemented. It should e possible and pure is broken for them.void main(string[] args) { void foo() immutable { } } That compiles and works as expected, it seems to be implemented at least for nested functions. I didn't know about it before because it wasn't documented anywhere and it does not work for anonymous delegates. >.>
Jul 03 2015
On Friday, 3 July 2015 at 11:08:46 UTC, Tofu Ninja wrote:...It even gives nice errors when you try to access mutable data void main(string[] args){ int x; auto foo() immutable{return x;} } Error: pure immutable nested function 'main.main.foo' cannot access mutable data 'x' Const does not seem to work properly though... void main(string[] args){ int x = 0; auto foo() const{ x++; return x; } writeln(foo()); // prints 1 }
Jul 03 2015
On 07/03/2015 10:35 AM, Tofu Ninja wrote:On Friday, 3 July 2015 at 02:13:39 UTC, Timon Gehr wrote:I assume the related commits are listed here: https://issues.dlang.org/show_bug.cgi?id=9148 The last of them are after the 2.067.1 release, so maybe it already works correctly in master. Otherwise the issue should be reopened.On 07/03/2015 02:26 AM, Tofu Ninja wrote:-_- Is that documented anywhere? I can't find anything in the docs that says nested functions can have immutable, const, shared, inout attached to them. This seems to be a wholly unknown and undocumented feature, and the fact that it doesn't work on anonymous delegates seems like it's not even fully implemented, like someone started to implement it and stopped half way though.So this idea spawned from a thread I posted over in learn[1]. Similar to how you can mark a member function as const or immutable, there should be a way to mark a delegate as const or immutable. ... Thoughts?https://issues.dlang.org/show_bug.cgi?id=14745
Jul 03 2015