www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - isMutable!(T) - intended/overlooked behaviour?

reply Liam McSherry <mcsherry.liam gmail.com> writes:
Are the results produced by the following program intended 
behaviour for isMutable!(T)?

---
void main()
{
     import std.stdio  : writeln;
     import std.traits : isMutable;

     struct S
     {
         enum size_t constant_0        = 0;
         enum const(size_t) constant_1 = 0;
     }

     __traits(compiles, { S s; s.constant_0 = 1; }).writeln; // 
false

     isMutable!(typeof(S.constant_0)).writeln; // true
     isMutable!(typeof(S.constant_1)).writeln; // false
}
---

I know the documentation for isMutable!(T) says "Returns true if 
T is not const or immutable," but it would make sense to me if it 
returned false when given an enum. Is this maybe something to be 
corrected?
Jan 11
next sibling parent Meta <jared771 gmail.com> writes:
On Monday, 11 January 2016 at 14:39:33 UTC, Liam McSherry wrote:
 Are the results produced by the following program intended 
 behaviour for isMutable!(T)?

 ---
 void main()
 {
     import std.stdio  : writeln;
     import std.traits : isMutable;

     struct S
     {
         enum size_t constant_0        = 0;
         enum const(size_t) constant_1 = 0;
     }

     __traits(compiles, { S s; s.constant_0 = 1; }).writeln; // 
 false

     isMutable!(typeof(S.constant_0)).writeln; // true
     isMutable!(typeof(S.constant_1)).writeln; // false
 }
 ---

 I know the documentation for isMutable!(T) says "Returns true 
 if T is not const or immutable," but it would make sense to me 
 if it returned false when given an enum. Is this maybe 
 something to be corrected?
As far as I know it's impossible to tell whether a value is actually a value or was declared as an enum. I thought that `enum size_t s = 0` was supposed to be sugar for: enum <hidden symbol>: size_t { s = 0 } alias s = <hidden symbol>.s; But they don't behave the same way. Observe: void main() { enum size_t s = 0; pragma(msg, is(typeof(s) == enum)); //Prints 'false' enum _: size_t { t = 0 } alias t = _.t; pragma(msg, is(typeof(t) == enum)); //Prints 'true' } So those shorthand enum definitions are actually different language entities from 'actual' enums. This seems like a bug to me. Of course this is somewhat moot as isMutable returns true for both s and t as defined in the above code. However, we could add code to isMutable to return false for all enums, if the problem I mentioned was fixed.
Jan 11
prev sibling next sibling parent reply Basile B. <b2.temp gmx.com> writes:
On Monday, 11 January 2016 at 14:39:33 UTC, Liam McSherry wrote:
 Are the results produced by the following program intended 
 behaviour for isMutable!(T)?

 ---
 void main()
 {
     import std.stdio  : writeln;
     import std.traits : isMutable;

     struct S
     {
         enum size_t constant_0        = 0;
         enum const(size_t) constant_1 = 0;
     }

     __traits(compiles, { S s; s.constant_0 = 1; }).writeln; // 
 false

     isMutable!(typeof(S.constant_0)).writeln; // true
     isMutable!(typeof(S.constant_1)).writeln; // false
 }
 ---

 I know the documentation for isMutable!(T) says "Returns true 
 if T is not const or immutable," but it would make sense to me 
 if it returned false when given an enum. Is this maybe 
 something to be corrected?
The question is what do you mean by enum ? --- void main() { import std.stdio : writeln; import std.traits : isMutable; struct S { enum size_t constant_0 = 0; enum const(size_t) constant_1 = 0; } assert(!(is(typeof(constant_0) == enum))); assert(!(is(typeof(constant_1) == enum))); } ---
Jan 11
parent reply Liam McSherry <mcsherry.liam gmail.com> writes:
On Monday, 11 January 2016 at 15:00:30 UTC, Basile B. wrote:
 The question is what do you mean by enum ?
I'm not certain of the correct term for it, but any constant declared as: --- enum [<type>] <identifier> = <value>; --- e.g. --- enum int root_i = -1; ---
Jan 11
parent reply Liam McSherry <mcsherry.liam gmail.com> writes:
On Monday, 11 January 2016 at 15:17:24 UTC, Liam McSherry wrote:
 ---
 enum int root_i = -1;
 ---
Or rather --- enum int sqrt_i = -1; ---
Jan 11
parent reply Liam McSherry <mcsherry.liam gmail.com> writes:
On Monday, 11 January 2016 at 15:23:34 UTC, Liam McSherry wrote:
 On Monday, 11 January 2016 at 15:17:24 UTC, Liam McSherry wrote:
 ---
 enum int root_i = -1;
 ---
Or rather --- enum int sqrt_i = -1; ---
Argh. Obviously I need more coffee today. --- enum int square_i = -1; ---
Jan 11
parent Basile B. <b2.temp gmx.com> writes:
On Monday, 11 January 2016 at 15:27:51 UTC, Liam McSherry wrote:
 On Monday, 11 January 2016 at 15:23:34 UTC, Liam McSherry wrote:
 On Monday, 11 January 2016 at 15:17:24 UTC, Liam McSherry 
 wrote:
 ---
 enum int root_i = -1;
 ---
Or rather --- enum int sqrt_i = -1; ---
Argh. Obviously I need more coffee today. --- enum int square_i = -1; ---
It's never an enum http://www.wow247.co.uk/wp-content/uploads/2015/07/terminator-smile-2-620x413.jpg "enum" can be removed.
Jan 11
prev sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 1/11/16 9:39 AM, Liam McSherry wrote:
 Are the results produced by the following program intended behaviour for
 isMutable!(T)?

 ---
 void main()
 {
      import std.stdio  : writeln;
      import std.traits : isMutable;

      struct S
      {
          enum size_t constant_0        = 0;
          enum const(size_t) constant_1 = 0;
      }

      __traits(compiles, { S s; s.constant_0 = 1; }).writeln; // false

      isMutable!(typeof(S.constant_0)).writeln; // true
      isMutable!(typeof(S.constant_1)).writeln; // false
 }
 ---

 I know the documentation for isMutable!(T) says "Returns true if T is
 not const or immutable," but it would make sense to me if it returned
 false when given an enum. Is this maybe something to be corrected?
You are confusing type with storage class. isMutable tells you that some variable declared with the given type would be mutable. In other words, it's not the type that makes constant_0 immutable, it's the location where it's stored. This should work fine: typeof(S.constant_0) foo; foo = 5; -Steve
Jan 11