Re: Mutate immutable inside shared static constructor

2024-03-24 Thread Menjanahary R. R. via Digitalmars-d-learn
On Saturday, 23 March 2024 at 21:59:57 UTC, Nick Treleaven wrote: On Saturday, 23 March 2024 at 21:53:43 UTC, Jonathan M Davis wrote: Yes, it's a bug. It's a clear violation of the type system if a non-mutable variable is ever given a value more than once. It should be initialized, and then it

Re: Mutate immutable inside shared static constructor

2024-03-23 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 23 March 2024 at 21:53:43 UTC, Jonathan M Davis wrote: Yes, it's a bug. It's a clear violation of the type system if a non-mutable variable is ever given a value more than once. It should be initialized, and then it should be treated as illegal to ever assign to it - or to do

Re: Mutate immutable inside shared static constructor

2024-03-23 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, March 23, 2024 3:23:23 PM MDT Nick Treleaven via Digitalmars-d- learn wrote: > I've not used static constructors before, but it seems like the > following should not be allowed: > > ```d > import std.stdio; > > immutable int x; > > @safe shared static this() > { > x.writeln(); //

Mutate immutable inside shared static constructor

2024-03-23 Thread Nick Treleaven via Digitalmars-d-learn
I've not used static constructors before, but it seems like the following should not be allowed: ```d import std.stdio; immutable int x; @safe shared static this() { x.writeln(); // 0 x = 5; x.writeln(); // 5 x = 6; x++; assert(x == 7); } ``` Should I file a bug to

Re: Static constructor

2021-01-18 Thread ludo via Digitalmars-d-learn
The mutex isn't more "global", it's more "local" (although, this will make it shared). Yes shared, this is what I meant by global :) Thanks, it's clearer now.

Re: Static constructor

2021-01-18 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/17/21 6:54 PM, ludo wrote: Yes alright. I think the dev made a design mistake because he called synchronized( OpenAL.mutex ) when it should be more of a global, non OpenAL specific, mutex. I mean in the code there were things like (pseudo-code) --- System {   private int[] buffer  

Re: Static constructor

2021-01-17 Thread ludo via Digitalmars-d-learn
Thanks, as explained I am indeed porting old code. No on the AA (as noted above). The mutex *is* created on demand. Every Object can have a mutex, and it's only created when you synchronize it for the first time. Yes alright. I think the dev made a design mistake because he called

Re: Static constructor

2021-01-15 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/6/21 12:05 PM, ludo wrote: I read in the documentation "Static constructors are used to initialize static class members with values that cannot be computed at compile time" I try to understand the design of the following code: --- class OpenAL { static string[int] ALErrorLookup;   

Re: Static constructor

2021-01-15 Thread Imperatorn via Digitalmars-d-learn
r the late reply. No worry and thank you. I found the low-lock pattern on my own, digging more info. Smart pattern! I put the synchronized function in front of the function. The problem with multithreading, is the difficulty to verify that it works fine :) But now the topic streams away f

Re: Static constructor

2021-01-15 Thread ludo via Digitalmars-d-learn
e low-lock pattern on my own, digging more info. Smart pattern! I put the synchronized function in front of the function. The problem with multithreading, is the difficulty to verify that it works fine :) But now the topic streams away from static constructor :) Cheers

Re: Static constructor

2021-01-14 Thread SealabJaster via Digitalmars-d-learn
On Tuesday, 12 January 2021 at 11:28:12 UTC, ludo wrote: Ok, I agree that ends up being a kind of strange singleton. But yes it was D v1 code. Do we agree that the following multi-threaded singleton pattern is the proper way as of today. It looks fine to me. The D wiki has the following

Re: Static constructor

2021-01-12 Thread ludo via Digitalmars-d-learn
)(); } } return instance; } --- As for some of the other stuff, Associative Array literals in D can't actually be used at compile-time (at least, last time I tried), so they have to be created inside of the static constructor. OK, I found https://dlang.org/spec/hash-map.html

Re: Static constructor

2021-01-06 Thread SealabJaster via Digitalmars-d-learn
On Thursday, 7 January 2021 at 01:55:07 UTC, SealabJaster wrote: ... And on a side note, I don't believe this code is working as the author intends. Someone correct me if I'm wrong, but `static` variables in D arethread-local, so a `static Object mutex` wouldn't actually be visible

Re: Static constructor

2021-01-06 Thread SealabJaster via Digitalmars-d-learn
, Associative Array literals in D can't actually be used at compile-time (at least, last time I tried), so they have to be created inside of the static constructor. Next is the mutex. `Object` is the base class that every class in D will implicitly inherit from, like in C# for example. Object

Re: Static constructor

2021-01-06 Thread Imperatorn via Digitalmars-d-learn
On Wednesday, 6 January 2021 at 17:05:02 UTC, ludo wrote: I read in the documentation "Static constructors are used to initialize static class members with values that cannot be computed at compile time" [...] Since this is not the complete code it's a bit hard to know, but I'd guess this

Static constructor

2021-01-06 Thread ludo via Digitalmars-d-learn
I read in the documentation "Static constructors are used to initialize static class members with values that cannot be computed at compile time" I try to understand the design of the following code: --- class OpenAL { static string[int] ALErrorLookup; static Object mutex;

Re: Module static constructor doesn't work?

2019-08-08 Thread Andrey Zherikov via Digitalmars-d-learn
On Thursday, 8 August 2019 at 16:04:33 UTC, a11e99z wrote: On Thursday, 8 August 2019 at 14:55:37 UTC, Andrey Zherikov wrote: I have the following code: // main.d int main() { import std.stdio; writeln("hello"); return 0; } But if I create library from lib.d first and then link it

Re: Module static constructor doesn't work?

2019-08-08 Thread kinke via Digitalmars-d-learn
On Thursday, 8 August 2019 at 14:55:37 UTC, Andrey Zherikov wrote: But if I create library from lib.d first and then link it with main.d then ctor/dtor are not called: For this to work as expected, the `lib.obj` object file needs to be linked into the final executable. As main.d doesn't need

Re: Module static constructor doesn't work?

2019-08-08 Thread a11e99z via Digitalmars-d-learn
On Thursday, 8 August 2019 at 14:55:37 UTC, Andrey Zherikov wrote: I have the following code: // main.d int main() { import std.stdio; writeln("hello"); return 0; } But if I create library from lib.d first and then link it with main.d then ctor/dtor are not called: $ dmd.exe -lib

Re: Module static constructor doesn't work?

2019-08-08 Thread Dukc via Digitalmars-d-learn
On Thursday, 8 August 2019 at 14:55:37 UTC, Andrey Zherikov wrote: I have the following code: // lib1/lib.d module lib; import std.stdio; static this() { writeln("+" ~ __FILE__); } static ~this() { writeln("-" ~ __FILE__); } // main.d int main() { import std.stdio;

Module static constructor doesn't work?

2019-08-08 Thread Andrey Zherikov via Digitalmars-d-learn
I have the following code: // lib1/lib.d module lib; import std.stdio; static this() { writeln("+" ~ __FILE__); } static ~this() { writeln("-" ~ __FILE__); } // main.d int main() { import std.stdio; writeln("hello"); return 0; } So if I compile lib.d and main.d together

Re: Shared library loading and static constructor

2019-03-23 Thread Sobaya via Digitalmars-d-learn
On Saturday, 23 March 2019 at 16:56:28 UTC, tchaloupka wrote: On Saturday, 23 March 2019 at 15:58:07 UTC, Sobaya wrote: What I am saying is that it can not be read when a code importing (a.d) a code including the static constructor (b.d) is compiled into shared library. Hi. I've tried

Re: Shared library loading and static constructor

2019-03-23 Thread tchaloupka via Digitalmars-d-learn
On Saturday, 23 March 2019 at 15:58:07 UTC, Sobaya wrote: What I am saying is that it can not be read when a code importing (a.d) a code including the static constructor (b.d) is compiled into shared library. Hi. I've tried to add your case to the repository and at it seems to be working

Re: Shared library loading and static constructor

2019-03-23 Thread Sobaya via Digitalmars-d-learn
the environment variable LD_LIBRARY_PATH. I do not know what is the correct way on linux. Kind regards Andre This is not a problem of path, I think. Because when I do not write static constructor in b.d, that code works. This problem occurs only when static constructor is used. Did you noticed

Re: Shared library loading and static constructor

2019-03-23 Thread Andre Pany via Digitalmars-d-learn
is the correct way on linux. Kind regards Andre This is not a problem of path, I think. Because when I do not write static constructor in b.d, that code works. This problem occurs only when static constructor is used. Did you noticed this thread? https://forum.dlang.org/post/eumnxgxtjrvvkhxpw

Re: Shared library loading and static constructor

2019-03-22 Thread Sobaya via Digitalmars-d-learn
This is not a problem of path, I think. Because when I do not write static constructor in b.d, that code works. This problem occurs only when static constructor is used.

Re: Shared library loading and static constructor

2019-03-22 Thread Andre Pany via Digitalmars-d-learn
On Friday, 22 March 2019 at 10:51:58 UTC, Sobaya wrote: I fail to load the shared library created in a specific situation, but I do not know the cause. a.d import b.d b.d static this() {} for above 2 files, I created shared library by following command. dmd a.d -shared

Shared library loading and static constructor

2019-03-22 Thread Sobaya via Digitalmars-d-learn
I fail to load the shared library created in a specific situation, but I do not know the cause. a.d import b.d b.d static this() {} for above 2 files, I created shared library by following command. dmd a.d -shared -of=a.so And I ran below code, but the library is not

Re: static constructor not working with WinMain GUI app

2010-11-24 Thread Andrej Mitrovic
maintest.d: http://pastebin.com/3c4CKDG1 Compiled with DMD 2.050: dmd maintest.d maintest.def The output in the log file is Lenght: 0. The static constructor is never called when using WinMain. But if I use a normal void main() function instead of WinMain, then the static constructor does get

Re: static constructor not working with WinMain GUI app

2010-11-24 Thread Andrej Mitrovic
. The static constructor is never called when using WinMain. But if I use a normal void main() function instead of WinMain, then the static constructor does get called. Here's a working example that tests a GUI dialogbox and outputs Length: 4 in the log file: maintest.def: EXETYPE NT SUBSYSTEM

Re: static constructor not working with WinMain GUI app

2010-11-23 Thread Simen kjaeraas
Andrej Mitrovic n...@none.none wrote: maintest.def: EXETYPE NT SUBSYSTEM WINDOWS maintest.d: http://pastebin.com/3c4CKDG1 Compiled with DMD 2.050: dmd maintest.d maintest.def The output in the log file is Lenght: 0. The static constructor is never called when using WinMain. But if I use

Re: static constructor not working with WinMain GUI app

2010-11-23 Thread Michal Minich
On Tue, 23 Nov 2010 15:23:12 -0500, Andrej Mitrovic wrote: The static constructor is never called when using WinMain. Make sure you use newest DMD, I think in 2.048 or 49 there was bug that module constructors were not called.