Thread overview | ||||||
---|---|---|---|---|---|---|
|
September 07, 2012 typeof(enum) | ||||
---|---|---|---|---|
| ||||
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. |
September 07, 2012 Re: typeof(enum) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Minas | 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
|
September 07, 2012 Re: typeof(enum) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Minas | 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
|
September 08, 2012 Re: typeof(enum) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Minas | 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. |
Copyright © 1999-2021 by the D Language Foundation