www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - typeof(enum)

reply "Minas" <minas_mina1990 hotmail.co.uk> writes:
import std.stdio;

enum PI = 3.14;

void main()
{
	writeln(typeid(typeof(PI)));
}

It prints "double". Shouldn't it be immutable(double). I think it 
would make more sense, as it is a constant. Plus, it could be 
shared among threads.
Sep 07 2012
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 09/07/2012 03:55 PM, Minas wrote:
 import std.stdio;

 enum PI = 3.14;

 void main()
 {
 writeln(typeid(typeof(PI)));
 }

 It prints "double". Shouldn't it be immutable(double). I think it would
 make more sense, as it is a constant. Plus, it could be shared among
 threads.

immutable makes sense with variables (I know, it's an oxymoron. :)) PI in your code is just a manifest constant (i.e. it is just 3.14, nothing more). In other words, its immutability comes from being an rvalue. Ali
Sep 07 2012
prev sibling next sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Saturday, September 08, 2012 00:55:24 Minas wrote:
 import std.stdio;
 
 enum PI = 3.14;
 
 void main()
 {
 writeln(typeid(typeof(PI)));
 }
 
 It prints "double". Shouldn't it be immutable(double). I think it
 would make more sense, as it is a constant. Plus, it could be
 shared among threads.

PI isn't even a variable. It's a manifest constant. Its value gets copy-pasted everywhere you use it (which is why using enums with array literals or AA literals is generally a bad idea - each usage allocates a new one). So, making it immutable wouldn't really buy you anything. There's nothing there to share across threads, and whether what it's assigned to should be immutable or not depends on what it's being assigned to. And since double is a value type, it can be assigned to a variable of any constancy, making the constness or immutability of the enum pretty much a mute point. Pretty much the _only_ place that it would matter would be type inferrence. - Jonathan M Davis
Sep 07 2012
prev sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
Accidentally replied to your private email first.

On 09/08/2012 12:55 AM, Minas wrote:
 import std.stdio;

 enum PI = 3.14;

 void main()
 {
      writeln(typeid(typeof(PI)));
 }

 It prints "double". Shouldn't it be immutable(double).

No. auto x = PI; x++;
 I think it would  make more sense, as it is a constant.

It never makes sense to have mutable values, but their type may still allow mutation of variables of that type.
 Plus, it could be shared among threads.

The symbol PI does not exist at runtime.
Sep 07 2012