www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Receiving "pthread_mutexattr_init" during compilation when using

reply tastyminerals <tastyminerals gmail.com> writes:
I getting
```
import/core/sync/mutex.d(87,36): Error: `pthread_mutexattr_init` 
cannot be interpreted at compile time, because it has no 
available source code
```
compile time error. One of those D exceptions which doesn't say 
where it happened in your code so you need to comment out the 
lines that might cause it one by one :D

I found that this was caused by the "FileLogger" which I create 
at the top of the file. But why?

```d
import std.experimental.logger: FileLogger;

auto fileLogger = new FileLogger("last.log");
```

Moving the FileLogger instantiation to a function fixes the issue 
though.
Jun 07 2021
parent reply Mike Parker <aldacron gmail.com> writes:
On Monday, 7 June 2021 at 09:29:08 UTC, tastyminerals wrote:
 I getting
 ```
 import/core/sync/mutex.d(87,36): Error: 
 `pthread_mutexattr_init` cannot be interpreted at compile time, 
 because it has no available source code
 ```
 compile time error. One of those D exceptions which doesn't say 
 where it happened in your code so you need to comment out the 
 lines that might cause it one by one :D

 I found that this was caused by the "FileLogger" which I create 
 at the top of the file. But why?
You can't initialize classes at module scope. You can declare them, but then they must be initialized in a function or a module constructor. Normally for a type `MyClass` in module `foo`, you'd see an error like this: ```D Error: variable `foo.MyClass` is a thread-local class and cannot have a static initializer. Use `static this()` to initialize instead. ``` I guess CTFE runs before the check that catches this, though, and the `FileLogger` constructor ultimately triggers the error your seeing.
Jun 07 2021
parent reply tastyminerals <tastyminerals gmail.com> writes:
On Monday, 7 June 2021 at 10:36:23 UTC, Mike Parker wrote:
 On Monday, 7 June 2021 at 09:29:08 UTC, tastyminerals wrote:
 I getting
 ```
 import/core/sync/mutex.d(87,36): Error: 
 `pthread_mutexattr_init` cannot be interpreted at compile 
 time, because it has no available source code
 ```
 compile time error. One of those D exceptions which doesn't 
 say where it happened in your code so you need to comment out 
 the lines that might cause it one by one :D

 I found that this was caused by the "FileLogger" which I 
 create at the top of the file. But why?
You can't initialize classes at module scope. You can declare them, but then they must be initialized in a function or a module constructor. Normally for a type `MyClass` in module `foo`, you'd see an error like this: ```D Error: variable `foo.MyClass` is a thread-local class and cannot have a static initializer. Use `static this()` to initialize instead. ``` I guess CTFE runs before the check that catches this, though, and the `FileLogger` constructor ultimately triggers the error your seeing.
I see, I only remember that one cannot create associative arrays in global scope. This is explicitly written in the language reference. It's interesting how there is no information about class instantiation at module level anywhere in the docs. Even the Ali's book does not explicitly say that you cannot instantiate a class this way. Maybe I missed it but just doing the key search on "scope", "global", "module" in class related chapters didn't give me any results.
Jun 07 2021
parent reply tastyminerals <tastyminerals gmail.com> writes:
On Monday, 7 June 2021 at 11:29:44 UTC, tastyminerals wrote:
 On Monday, 7 June 2021 at 10:36:23 UTC, Mike Parker wrote:
 On Monday, 7 June 2021 at 09:29:08 UTC, tastyminerals wrote:
 I getting
 ```
 import/core/sync/mutex.d(87,36): Error: 
 `pthread_mutexattr_init` cannot be interpreted at compile 
 time, because it has no available source code
 ```
 compile time error. One of those D exceptions which doesn't 
 say where it happened in your code so you need to comment out 
 the lines that might cause it one by one :D

 I found that this was caused by the "FileLogger" which I 
 create at the top of the file. But why?
You can't initialize classes at module scope. You can declare them, but then they must be initialized in a function or a module constructor. Normally for a type `MyClass` in module `foo`, you'd see an error like this: ```D Error: variable `foo.MyClass` is a thread-local class and cannot have a static initializer. Use `static this()` to initialize instead. ``` I guess CTFE runs before the check that catches this, though, and the `FileLogger` constructor ultimately triggers the error your seeing.
I see, I only remember that one cannot create associative arrays in global scope. This is explicitly written in the language reference.
It's interesting how there is no information about class instantiation at module level anywhere in the docs. Even the Ali's book does not explicitly say that you cannot instantiate a class this way. Maybe I totally missed it but just doing the key search on "scope", "global", "module" in class related chapters didn't give me any results.
Jun 07 2021
parent Mike Parker <aldacron gmail.com> writes:
On Monday, 7 June 2021 at 11:31:13 UTC, tastyminerals wrote:

 It's interesting how there is no information about class 
 instantiation at module level anywhere in the docs. Even the 
 Ali's book does not explicitly say that you cannot instantiate 
 a class this way. Maybe I totally missed it but just doing the 
 key search on "scope", "global", "module" in class related 
 chapters didn't give me any results.
It's covered by this: https://dlang.org/spec/declaration.html#global_static_init
 The Initializer for a global or static variable must be 
 evaluatable at compile time. Runtime initialization is done 
 with static constructors.
It obviously could be more explicit. I'll add a note to my todo list to make it so.
Jun 07 2021