Re: What is the difference between enum and shared immutable?

2020-10-30 Thread Max Samukha via Digitalmars-d-learn
On Thursday, 29 October 2020 at 16:45:51 UTC, Ali Çehreli wrote: immutable string p; shared static this() { p = environment["PATH"]; // <-- Run time } Immutable static variables being implicitly non-thread-local is a design mistake. Thread-local immutable variables have the same right

Re: What is the difference between enum and shared immutable?

2020-10-30 Thread Daniel Kozak via Digitalmars-d-learn
On Thu, Oct 29, 2020 at 4:13 PM H. S. Teoh via Digitalmars-d-learn < digitalmars-d-learn@puremagic.com> wrote: > > But why can't that be treated differently from explicitly writing @safe > on a declaration? I mean, yeah, it's easier to implement the compiler > that way, but ease of implementation

Re: What is the difference between enum and shared immutable?

2020-10-29 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Thursday, 29 October 2020 at 14:39:31 UTC, H. S. Teoh wrote: On Thu, Oct 29, 2020 at 09:50:28AM -0400, Steven Schveighoffer via Digitalmars-d-learn wrote: [...] D frequently allows no-op attributes. [...] I find that to be a bad smell in terms of language design, actually. Either something

Re: What is the difference between enum and shared immutable?

2020-10-29 Thread Ali Çehreli via Digitalmars-d-learn
On 10/29/20 10:16 AM, H. S. Teoh wrote: > Module-level immutable can be initialized either as part of the > declaration: > >immutable int x = 1; To add, the expression can be a call to a function that need not return immutable, as long as it is pure, which can be inferred by the compiler f

Re: What is the difference between enum and shared immutable?

2020-10-29 Thread Imperatorn via Digitalmars-d-learn
On Thursday, 29 October 2020 at 16:31:41 UTC, Ali Çehreli wrote: On 10/28/20 5:55 PM, matheus wrote: On Wednesday, 28 October 2020 at 22:07:06 UTC, H. S. Teoh wrote: ... (This is why it's a bad idea to use enum with an array literal, because every time it's referenced you get a new copy of the

Re: What is the difference between enum and shared immutable?

2020-10-29 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Oct 29, 2020 at 04:56:46PM +, IGotD- via Digitalmars-d-learn wrote: > On Thursday, 29 October 2020 at 16:45:51 UTC, Ali Çehreli wrote: > > > > import std; > > > > immutable string p; > > > > shared static this() { > > p = environment["PATH"]; // <-- Run time > > } > > > > Just t

Re: What is the difference between enum and shared immutable?

2020-10-29 Thread IGotD- via Digitalmars-d-learn
On Thursday, 29 October 2020 at 16:45:51 UTC, Ali Çehreli wrote: import std; immutable string p; shared static this() { p = environment["PATH"]; // <-- Run time } Just to clarify, immutable is allowed to be initialized in ctors but not anything later than that? Moving p = environment["P

Re: What is the difference between enum and shared immutable?

2020-10-29 Thread Ali Çehreli via Digitalmars-d-learn
On 10/28/20 3:07 PM, H. S. Teoh wrote: A shared immutable is initialized at compile-time, To prevent a misunderstanding, immutable can be initialized at run time as well. On the other hand, immutable initialized at compile time was surprising to me when I learned it recently: import std;

Re: What is the difference between enum and shared immutable?

2020-10-29 Thread Ali Çehreli via Digitalmars-d-learn
On 10/28/20 5:55 PM, matheus wrote: On Wednesday, 28 October 2020 at 22:07:06 UTC, H. S. Teoh wrote: ... (This is why it's a bad idea to use enum with an array literal, because every time it's referenced you get a new copy of the array.) ... Could you please give an example (Snippet) about th

Re: What is the difference between enum and shared immutable?

2020-10-29 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Oct 29, 2020 at 11:00:42AM -0400, Steven Schveighoffer via Digitalmars-d-learn wrote: > On 10/29/20 10:39 AM, H. S. Teoh wrote: > > On Thu, Oct 29, 2020 at 09:50:28AM -0400, Steven Schveighoffer via > > Digitalmars-d-learn wrote: > > [...] > > > D frequently allows no-op attributes. > > [

Re: What is the difference between enum and shared immutable?

2020-10-29 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/29/20 10:39 AM, H. S. Teoh wrote: On Thu, Oct 29, 2020 at 09:50:28AM -0400, Steven Schveighoffer via Digitalmars-d-learn wrote: [...] D frequently allows no-op attributes. [...] I find that to be a bad smell in terms of language design, actually. Either something should be allowed and h

Re: What is the difference between enum and shared immutable?

2020-10-29 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Oct 29, 2020 at 09:50:28AM -0400, Steven Schveighoffer via Digitalmars-d-learn wrote: [...] > D frequently allows no-op attributes. [...] I find that to be a bad smell in terms of language design, actually. Either something should be allowed and have a definite effect, or it should not be

Re: What is the difference between enum and shared immutable?

2020-10-29 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/28/20 6:28 PM, IGotD- wrote: On Wednesday, 28 October 2020 at 21:54:19 UTC, Jan Hönig wrote: shared immutable x = 1; Is there a point to add shared to an immutable? Aren't immutable implicitly also shared? You are correct: pragma(msg, typeof(x)); // immutable(int) D frequently all

Re: What is the difference between enum and shared immutable?

2020-10-29 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Oct 29, 2020 at 08:13:42AM +, Jan Hönig via Digitalmars-d-learn wrote: [...] > Is there some rule of thumb when to use what? > > I judge that using enum will slow down the compilation process, but > might increase performance. Nah. The slowdown is practically indiscernible. You should

Re: What is the difference between enum and shared immutable?

2020-10-29 Thread IGotD- via Digitalmars-d-learn
On Thursday, 29 October 2020 at 12:21:19 UTC, Ola Fosheim Grøstad wrote: You can test this with is(TYPE1==TYPE2) is(shared(immutable(int))==immutable(int)) So I got that to true, which means that shared immutable is exactly the same as immutable. Shared is implicit for immutable which makes

Re: What is the difference between enum and shared immutable?

2020-10-29 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Thursday, 29 October 2020 at 08:13:42 UTC, Jan Hönig wrote: Regarding the shared keyword, I am not sure if immutables are shared per default. I thought they are not. You can test this with is(TYPE1==TYPE2) is(shared(immutable(int))==immutable(int))

Re: What is the difference between enum and shared immutable?

2020-10-29 Thread Jan Hönig via Digitalmars-d-learn
On Wednesday, 28 October 2020 at 22:07:06 UTC, H. S. Teoh wrote: [...] An enum only exists at compile-time, and does not occupy any space. Each time it's referenced, a new instance of the value is created. (This is why it's a bad idea to use enum with an array literal, because every time it'

Re: What is the difference between enum and shared immutable?

2020-10-28 Thread matheus via Digitalmars-d-learn
On Thursday, 29 October 2020 at 01:26:38 UTC, Mike Parker wrote: enum int[] arr = [1,2,3]; someFunc(arr); This is identical to someFunc([1,2,3]); Manifest constants have no address. They are effectively aliases for their values. Hi Mike, I really didn't know about this. Thanks for the in

Re: What is the difference between enum and shared immutable?

2020-10-28 Thread Mike Parker via Digitalmars-d-learn
On Thursday, 29 October 2020 at 00:55:29 UTC, matheus wrote: On Wednesday, 28 October 2020 at 22:07:06 UTC, H. S. Teoh wrote: ... (This is why it's a bad idea to use enum with an array literal, because every time it's referenced you get a new copy of the array.) ... Could you please give an

Re: What is the difference between enum and shared immutable?

2020-10-28 Thread matheus via Digitalmars-d-learn
On Wednesday, 28 October 2020 at 22:07:06 UTC, H. S. Teoh wrote: ... (This is why it's a bad idea to use enum with an array literal, because every time it's referenced you get a new copy of the array.) ... Could you please give an example (Snippet) about this? Matheus.

Re: What is the difference between enum and shared immutable?

2020-10-28 Thread IGotD- via Digitalmars-d-learn
On Wednesday, 28 October 2020 at 21:54:19 UTC, Jan Hönig wrote: shared immutable x = 1; Is there a point to add shared to an immutable? Aren't immutable implicitly also shared?

Re: What is the difference between enum and shared immutable?

2020-10-28 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Oct 28, 2020 at 09:54:19PM +, Jan Hönig via Digitalmars-d-learn wrote: > Maybe this is a silly question, but I don't understand completely the > difference between an `enum` and a `shared immutable`. > > I could have: > > enum x = 1; > > or > > shared immutable x = 1; > > What is

What is the difference between enum and shared immutable?

2020-10-28 Thread Jan Hönig via Digitalmars-d-learn
Maybe this is a silly question, but I don't understand completely the difference between an `enum` and a `shared immutable`. I could have: enum x = 1; or shared immutable x = 1; What is the exact difference between those too? My best guess is, that shared immutable gets initialized during