digitalmars.D - bitfields VS pure nothrow
- Guillaume Chatelet (47/47) May 13 2012 Sure enough bitfields is a strange beast and I ran into some subtleties
- David Nadlinger (6/12) May 13 2012 Yes, the getters and setters should be both pure and nothrow.
- =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= (5/52) May 13 2012 I've sent a pull request that fixes this:
- Guillaume Chatelet (4/13) May 13 2012 Looking forward to reading it.
Sure enough bitfields is a strange beast and I ran into some subtleties with purity and nothrow. Consider the following code : ------------------------------ struct POD { int a; } int getA(POD o) pure nothrow { return o.a; } POD setA(POD o, int value) pure nothrow { o.a = value; return o; } ------------------------------ It compiles fine. But now with a bitfield : struct POD { mixin(std.bitmanip.bitfields!( int, "a",10, int, "", 22)); } The compiler complains about missing pure and nothrow attributes Error: pure function 'getA' cannot call impure function 'a' Error: o.a is not nothrow Error: function test.getA 'getA' is nothrow yet may throw Error: pure function 'setA' cannot call impure function 'a' Error: o.a is not nothrow Error: function test.setA 'setA' is nothrow yet may throw Which makes sense as the mixin outputs : ------------------------------ property uint a() const { auto result = (_a_ & 1023U) >>0U; return cast(uint) result; } property void a(uint v){ assert(v >= a_min); assert(v <= a_max); _a_ = cast(typeof(_a_)) ((_a_ & ~1023U) | ((cast(typeof(_a_)) v << 0U) & 1023U)); } enum uint a_min = cast(uint)0U; enum uint a_max = cast(uint)1023U; private uint _a_; ------------------------------ IMHO getters and setters could be nothrow but what about purity ? Looks like it's a bit far-fetched to qualify member methods as pure right ? Yet it makes sense regarding the semantic. What's your take on that ?
May 13 2012
On Sunday, 13 May 2012 at 20:49:13 UTC, Guillaume Chatelet wrote:IMHO getters and setters could be nothrow but what about purity? Looks like it's a bit far-fetched to qualify member methods as pure right? Yet it makes sense regarding the semantic. What's your take on that?Yes, the getters and setters should be both pure and nothrow. Your question about purity actually roots in a quite common misunderstanding – I have a pretty comprehensive article on the topic in the pipe, hope to be able to finish it later this week. David
May 13 2012
On 13-05-2012 21:51, Guillaume Chatelet wrote:Sure enough bitfields is a strange beast and I ran into some subtleties with purity and nothrow. Consider the following code : ------------------------------ struct POD { int a; } int getA(POD o) pure nothrow { return o.a; } POD setA(POD o, int value) pure nothrow { o.a = value; return o; } ------------------------------ It compiles fine. But now with a bitfield : struct POD { mixin(std.bitmanip.bitfields!( int, "a",10, int, "", 22)); } The compiler complains about missing pure and nothrow attributes Error: pure function 'getA' cannot call impure function 'a' Error: o.a is not nothrow Error: function test.getA 'getA' is nothrow yet may throw Error: pure function 'setA' cannot call impure function 'a' Error: o.a is not nothrow Error: function test.setA 'setA' is nothrow yet may throw Which makes sense as the mixin outputs : ------------------------------ property uint a() const { auto result = (_a_& 1023U)>>0U; return cast(uint) result; } property void a(uint v){ assert(v>= a_min); assert(v<= a_max); _a_ = cast(typeof(_a_)) ((_a_& ~1023U) | ((cast(typeof(_a_)) v<< 0U)& 1023U)); } enum uint a_min = cast(uint)0U; enum uint a_max = cast(uint)1023U; private uint _a_; ------------------------------ IMHO getters and setters could be nothrow but what about purity ? Looks like it's a bit far-fetched to qualify member methods as pure right ? Yet it makes sense regarding the semantic. What's your take on that ?I've sent a pull request that fixes this: https://github.com/D-Programming-Language/phobos/pull/583 -- - Alex
May 13 2012
On 05/13/12 23:01, David Nadlinger wrote:Yes, the getters and setters should be both pure and nothrow. Your question about purity actually roots in a quite common misunderstanding – I have a pretty comprehensive article on the topic in the pipe, hope to be able to finish it later this week. DavidLooking forward to reading it. On 05/13/12 23:19, Alex Rønne Petersen wrote:I've sent a pull request that fixes this: https://github.com/D-Programming-Language/phobos/pull/583Awesome. I was going to do so :)
May 13 2012