Re: general questions about static this() at module level
On Monday, 31 October 2016 at 18:46:31 UTC, Jonathan M Davis wrote: though there's no reason to ever use a static constructor (shared or otherwise) when you can directly initialize the variable. It's really meant for more complicated initialization that can't be done directly. Also, I would point out that in general, you'll be better off if you avoid static constructors and destructors. They can be extremely useful, but if multiple modules use them, and one imports the other (even indirectly), and the runtime thinks that that dependency is circular, then it'll throw an Error when you start your program (this comes from the fact that the runtime has to determine the order to run the static constructors so that everything is initialized before it's used, but it's not very smart about it, since it bases what it does solely on the presense of static constructors in a module and not what they actually do). Thanks! This is probably why I was not able to find a good code example in github. Not that I tried very hard when search returned over 10K occurrences of "static this". Seems this kind of higher level reasoning is missing from the books and documentation that I've read. Hope this kind of knowledge gets recorded somewhere.
Re: strange -fPIC compilation error
On 10/31/2016 12:31 PM, Daniel Kozak via Digitalmars-d-learn wrote: Dne 31.10.2016 v 20:20 Charles Hixson via Digitalmars-d-learn napsal(a): ... but dmd -defaultlib=libphobos2.so -fPIC test.d works. It shouldn't be required (as in the default /etc/dmd.conf should handle it correctly, but I can deal with it now. It should work, it is possible that you have some another dmd.conf somewhere? I did have a couple lying around, but they worked fine in the past, and renaming them didn't fix, or even just change, anything. I've still got some others on my backup partition, but I can't imagine that they would be in use. One of them was there because I had a few macros that were specified in an external ddoc file that was used by one project, e.g.
Re: newbie problem with nothrow
On Monday, October 31, 2016 22:20:59 Kapps via Digitalmars-d-learn wrote: > Assuming you're sure it'll never throw. To enforce this, use try > { } catch { throw new Error("blah"); }. You can still throw > errors, just not exceptions (as errors are not meant to be > caught). I always use assert(0). e.g. try return format("%s", 42); catch(Exception) assert(0, "format threw when it shouldn't be possible."); - Jonathan M Davis
Re: newbie problem with nothrow
On Monday, 31 October 2016 at 17:04:28 UTC, Temtaime wrote: On Monday, 31 October 2016 at 16:55:51 UTC, WhatMeWorry wrote: Is there a way to turn off nothrow or work around it? Because to me it looks like nothrow prevents me from doing anything useful. extern(C) void onKeyEvent(GLFWwindow* window, int key, int scancode, int action, int modifier) nothrow { if(queue.roomInQueue()) { auto event = new Event; event.type = EventType.keyboard; event.keyboard.key = cast(Key) key; // etc. } Error: function 'event_handler.CircularQueue.roomInQueue' is not nothrow Error: function 'event_handler.onKeyEvent' is nothrow yet may throw The compiler wouldn't let me just remove "nothrow" from the function. I tried a kludge where I had this function just pass all its parameters to another throwable function, but this caused errors as well. So I'm stuck. Anyone know how to proceed. Thanks. Wrap a body of the function to try {} catch {} and it'll work. Assuming you're sure it'll never throw. To enforce this, use try { } catch { throw new Error("blah"); }. You can still throw errors, just not exceptions (as errors are not meant to be caught).
Re: Can someone please explain why the following assertion fails?
On 10/31/2016 12:08 PM, Gary Willoughby wrote: Can someone please explain why the following assertion fails? import std.stdio; import std.conv; void main(string[] args) { auto x = 1; assert(hashOf(x.to!(string)) == hashOf(x.to!(string))); } Thanks. I think you need TypeInfo.getHash. https://dlang.org/phobos/object.html#.TypeInfo.getHash import std.conv; auto myHashOf(T)(auto ref T value) { return typeid(T).getHash(); } void main() { auto x = 1; auto s = "1"; assert(myHashOf(x.to!string) == myHashOf(x.to!string)); assert(myHashOf(s) == myHashOf(s)); assert(myHashOf(s) == myHashOf(x.to!string)); } Ali
Re: Can someone please explain why the following assertion fails?
On Monday, 31 October 2016 at 19:24:13 UTC, Ali Çehreli wrote: On 10/31/2016 12:08 PM, Gary Willoughby wrote: [...] Because it considers the .ptr property of arrays as well: https://github.com/dlang/druntime/blob/master/src/core/internal/hash.d#L61 [...] Ah right. Is there an alternative built-in, generic, nogc hash function that would return the same values for Strings?
Re: strange -fPIC compilation error
Dne 31.10.2016 v 20:20 Charles Hixson via Digitalmars-d-learn napsal(a): ... but dmd -defaultlib=libphobos2.so -fPIC test.d works. It shouldn't be required (as in the default /etc/dmd.conf should handle it correctly, but I can deal with it now. It should work, it is possible that you have some another dmd.conf somewhere?
Re: Can someone please explain why the following assertion fails?
On 10/31/2016 12:08 PM, Gary Willoughby wrote: Can someone please explain why the following assertion fails? import std.stdio; import std.conv; void main(string[] args) { auto x = 1; assert(hashOf(x.to!(string)) == hashOf(x.to!(string))); } Thanks. Because it considers the .ptr property of arrays as well: https://github.com/dlang/druntime/blob/master/src/core/internal/hash.d#L61 //dynamic array hash size_t hashOf(T)(auto ref T val, size_t seed = 0) if (!is(T == enum) && !is(T : typeof(null)) && is(T S: S[]) && !__traits(isStaticArray, T) && !is(T == struct) && !is(T == class) && !is(T == union)) { alias ElementType = typeof(val[0]); static if (is(ElementType == interface) || is(ElementType == class) || ((is(ElementType == struct) || is(ElementType == union)) && is(typeof(val[0].toHash()) == size_t))) //class or interface array or struct array with toHash(); CTFE depend on toHash() method { size_t hash = seed; foreach (o; val) { hash = hashOf(o, hash); } return hash; } else static if (is(typeof(toUbyte(val)) == const(ubyte)[])) //ubyteble array (arithmetic types and structs without toHash) CTFE ready for arithmetic types and structs without reference fields { auto bytes = toUbyte(val); return bytesHash(bytes.ptr, bytes.length, seed);// <-- HERE } else //Other types. CTFE unsupported { assert(!__ctfe, "unable to compute hash of "~T.stringof); return bytesHash(val.ptr, ElementType.sizeof*val.length, seed); } } Ali
Re: strange -fPIC compilation error
On 10/31/2016 11:23 AM, Daniel Kozak via Digitalmars-d-learn wrote: Dne 31.10.2016 v 18:06 Charles Hixson via Digitalmars-d-learn napsal(a): On 10/31/2016 09:26 AM, Charles Hixson via Digitalmars-d-learn wrote: On 10/30/2016 11:34 PM, Daniel Kozak via Digitalmars-d-learn wrote: Dne 31.10.2016 v 02:30 Charles Hixson via Digitalmars-d-learn napsal(a): Well, that certainly changed the error messages. With dmd -defaultlib=/usr/lib/x86_64-linux-gnu/libphobos2.so test.d I get: /usr/include/dmd/druntime/import/core/stdc/stdio.d(1121): Error: found 'nothrow' when expecting '{' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1123): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1124): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1125): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1126): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1127): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1128): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1129): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1133): Error: asm statements must end in ';' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1136): Error: found 'private' instead of statement /usr/include/dmd/druntime/import/core/stdc/stdio.d(1146): Error: no identifier for declarator add /usr/include/dmd/druntime/import/core/stdc/stdio.d(1149): Error: no identifier for declarator usDone /usr/include/dmd/druntime/import/core/stdc/stdio.d(1149): Error: Declaration expected, not ':' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1157): Error: Declaration expected, not '(' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1159): Error: Declaration expected, not 'foreach' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1159): Error: Declaration expected, not '0' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1164): Error: no identifier for declarator __fhnd_info[fd] /usr/include/dmd/druntime/import/core/stdc/stdio.d(1164): Error: Declaration expected, not '=' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1165): Error: Declaration expected, not 'return' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1167): Error: unrecognized declaration /usr/include/dmd/phobos/std/typecons.d(1124): Error: semicolon expected following function declaration This seems to be problem with your installation, you probably have diferen version of dmd compiler and phobos library. So you should uninstall all your dmd packages and make sure there is no /usr/include/dmd left in your system. And instal dmd only from one source (d-apt idealy). I've done that 2 or 3 times. If that's the problem, then there are different versions stored in the repository. Since I'm on debian testing I'd been assuming that there'd been some system change since I'd last used the compiler, and the debs weren't yet up to date. The only updates to my system prior to the compiler breaking HAD been via apt-get. Since then I've used dpkg remove and install a couple of times to try other versions of dmd with no benefit. Currently dmd-bin version 2.071.2-0 libphobos2.071.2-0 libphobos2.071.2-0 so they're LISTED as being the same version. And dmd.conf was installed by the deb, and is (eliminating the comments): [Environment32] DFLAGS=-I/usr/include/dmd/phobos -I/usr/include/dmd/druntime/import -L-L/usr/lib/i386-linux-gnu -L--export-dynamic [Environment64] DFLAGS=-I/usr/include/dmd/phobos -I/usr/include/dmd/druntime/import -L-L/usr/lib/x86_64-linux-gnu -L--export-dynamic But somewhere during the process (which included the nightly system update) the error messages changed, and now: dmd test.d yields: /usr/include/dmd/druntime/import/core/stdc/stdio.d(1121): Error: found 'nothrow' when expecting '{' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1123): Error: mismatched number of curly brackets ... /usr/include/dmd/druntime/import/core/stdc/stdio.d(1167): Error: unrecognized declaration /usr/include/dmd/phobos/std/typecons.d(1124): Error: semicolon expected following function declaration FWIW starting at /usr/include/dmd/druntime/import/core/stdc/stdio.d(1121):: asm nothrow @nogc { mov EDX, num; lock; inc _iSemLockCtrs[EDX * 2]; so nothrow isn't being seen as appropriate at the beginning of an asm block. After that I think it gets confused as 1123 doesn't HAVE a brace (i.e. curly bracket) in it. when you type dmd --version what it prints? THAT WAS THE CLUE! (that which follows is how I proceeded to the answer after that clue.) dmd -version Error: unrecognized switch '-version' and dmd --version Error:
Re: Can someone please explain why the following assertion fails?
On Monday, 31 October 2016 at 19:08:50 UTC, Gary Willoughby wrote: Can someone please explain why the following assertion fails? import std.stdio; import std.conv; void main(string[] args) { auto x = 1; assert(hashOf(x.to!(string)) == hashOf(x.to!(string))); } Thanks. DMD64 D Compiler v2.072.0 Copyright (c) 1999-2016 by Digital Mars written by Walter Bright Ubuntu 16.04
Can someone please explain why the following assertion fails?
Can someone please explain why the following assertion fails? import std.stdio; import std.conv; void main(string[] args) { auto x = 1; assert(hashOf(x.to!(string)) == hashOf(x.to!(string))); } Thanks.
Re: general questions about static this() at module level
On Monday, October 31, 2016 16:02:13 WhatMeWorry via Digitalmars-d-learn wrote: > On Monday, 31 October 2016 at 05:42:16 UTC, sarn wrote: > > On Monday, 31 October 2016 at 04:35:35 UTC, WhatMeWorry wrote: > >> [...] > > > > I've seen hacks to do the same thing in C++. They're not > > pretty, though. > > > >> [...] > > > > Class/struct static constructors are good for initialising > > class/struct static data. Module static constructors are good > > for initialising module "static data" (i.e., globals). They're > > especially handy for initialising immutable global data (which > > is the kind of global data D encourages). > > > > BTW, immutable data is shared between threads, so you should > > use "shared static this" to initialise it. Regular static > > constructors are executed per thread. > > Thanks! If you don't mind a follow up question, is this: > > immutable uint maxSize = 128; > > identical to this: > > immutable uint maxSize; > > static this() > { > maxSize = 128; > > } As Ali points out, the first one is initialized at compile time and usable at compile time, whereas the second is initialized at runtime and thus is not usable at compile time. It should be pointed out however, that it's an outstanding bug that initializing an immutable variable with a non-shared static this is allowed. As it stands, with the second example, maxSize would actually be initialized once per thread, which is a problem, because immutable is implicitly shared. It wouldn't really matter in this case, because it's a value type, and it's always given the same value, but it's still not something that should be allowed. Rather, it should be shared static this() { maxSize = 128; } though there's no reason to ever use a static constructor (shared or otherwise) when you can directly initialize the variable. It's really meant for more complicated initialization that can't be done directly. Also, I would point out that in general, you'll be better off if you avoid static constructors and destructors. They can be extremely useful, but if multiple modules use them, and one imports the other (even indirectly), and the runtime thinks that that dependency is circular, then it'll throw an Error when you start your program (this comes from the fact that the runtime has to determine the order to run the static constructors so that everything is initialized before it's used, but it's not very smart about it, since it bases what it does solely on the presense of static constructors in a module and not what they actually do). So, if you ever end up with any kind of circular imports (even indirectly), you can run into problems. Because of issues related to that, static constructors border on being banned in Phobos (they're still used in rare cases, but they're avoided if they're not truly needed, and we try not to need them). So, while static constructors are a great feature, they can cause problems if use them heavily. As to your original question about other languages that have them, IIRC, Java has static constructors (but I don't think that it has static destructors), if Java has it, C# almost certainly does as well. C++ does not though, which can be really annoying. It can be faked via RAII and static variables, but in general, using static variables in C++ is pretty iffy, because the order of intializaton is undefined (which shows that the annoyances with druntime detecting circular imports with static constructors and complaining about them are well worth the pain, though it would be nice if druntime were able to be smarter about it). So, D's static constructors and destructors are a huge improvement over what C++ has. - Jonathan M Davis
Re: strange -fPIC compilation error
Dne 31.10.2016 v 18:06 Charles Hixson via Digitalmars-d-learn napsal(a): On 10/31/2016 09:26 AM, Charles Hixson via Digitalmars-d-learn wrote: On 10/30/2016 11:34 PM, Daniel Kozak via Digitalmars-d-learn wrote: Dne 31.10.2016 v 02:30 Charles Hixson via Digitalmars-d-learn napsal(a): Well, that certainly changed the error messages. With dmd -defaultlib=/usr/lib/x86_64-linux-gnu/libphobos2.so test.d I get: /usr/include/dmd/druntime/import/core/stdc/stdio.d(1121): Error: found 'nothrow' when expecting '{' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1123): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1124): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1125): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1126): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1127): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1128): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1129): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1133): Error: asm statements must end in ';' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1136): Error: found 'private' instead of statement /usr/include/dmd/druntime/import/core/stdc/stdio.d(1146): Error: no identifier for declarator add /usr/include/dmd/druntime/import/core/stdc/stdio.d(1149): Error: no identifier for declarator usDone /usr/include/dmd/druntime/import/core/stdc/stdio.d(1149): Error: Declaration expected, not ':' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1157): Error: Declaration expected, not '(' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1159): Error: Declaration expected, not 'foreach' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1159): Error: Declaration expected, not '0' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1164): Error: no identifier for declarator __fhnd_info[fd] /usr/include/dmd/druntime/import/core/stdc/stdio.d(1164): Error: Declaration expected, not '=' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1165): Error: Declaration expected, not 'return' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1167): Error: unrecognized declaration /usr/include/dmd/phobos/std/typecons.d(1124): Error: semicolon expected following function declaration This seems to be problem with your installation, you probably have diferen version of dmd compiler and phobos library. So you should uninstall all your dmd packages and make sure there is no /usr/include/dmd left in your system. And instal dmd only from one source (d-apt idealy). I've done that 2 or 3 times. If that's the problem, then there are different versions stored in the repository. Since I'm on debian testing I'd been assuming that there'd been some system change since I'd last used the compiler, and the debs weren't yet up to date. The only updates to my system prior to the compiler breaking HAD been via apt-get. Since then I've used dpkg remove and install a couple of times to try other versions of dmd with no benefit. Currently dmd-bin version 2.071.2-0 libphobos2.071.2-0 libphobos2.071.2-0 so they're LISTED as being the same version. And dmd.conf was installed by the deb, and is (eliminating the comments): [Environment32] DFLAGS=-I/usr/include/dmd/phobos -I/usr/include/dmd/druntime/import -L-L/usr/lib/i386-linux-gnu -L--export-dynamic [Environment64] DFLAGS=-I/usr/include/dmd/phobos -I/usr/include/dmd/druntime/import -L-L/usr/lib/x86_64-linux-gnu -L--export-dynamic But somewhere during the process (which included the nightly system update) the error messages changed, and now: dmd test.d yields: /usr/include/dmd/druntime/import/core/stdc/stdio.d(1121): Error: found 'nothrow' when expecting '{' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1123): Error: mismatched number of curly brackets ... /usr/include/dmd/druntime/import/core/stdc/stdio.d(1167): Error: unrecognized declaration /usr/include/dmd/phobos/std/typecons.d(1124): Error: semicolon expected following function declaration FWIW starting at /usr/include/dmd/druntime/import/core/stdc/stdio.d(1121):: asm nothrow @nogc { mov EDX, num; lock; inc _iSemLockCtrs[EDX * 2]; so nothrow isn't being seen as appropriate at the beginning of an asm block. After that I think it gets confused as 1123 doesn't HAVE a brace (i.e. curly bracket) in it. when you type dmd --version what it prints?
Re: general questions about static this() at module level
On 10/31/2016 09:02 AM, WhatMeWorry wrote: > Thanks! If you don't mind a follow up question, is this: > > immutable uint maxSize = 128; > > identical to this: > > immutable uint maxSize; > > static this() > { > maxSize = 128; > > } As usual, yes and no. :) The former is initialized at compile-time, meaning that it's burned into the binary program to be placed on a page for such immutable values. The latter is initialized at run-time, meaning that its location in memory will be filled with the run-time computed value of the expression. As long as we treat immutable as immutable, from the point of view of the program the two behave the same. If we attempt to mutate immutable data, the outcome is undefined. The following program demonstrates that 1) The two kinds of immutables are placed in different places in memory ('a' is nearby 'a0' but 'b' is elsewhere) 2) Although both 'a' and 'b' are mutated, the last assert fails presumably because the compiler happens to treat 'a' differently from 'b' by using its compile-time value like an enum. In other words, although 'a' has a place in memory and we manage to change it, assert(a == 43) is compiled as assert(42 == 43) and fails. That's not the same with b. Again, none of this is defined anywhere in the language spec. If we mutate const or immutable data, the behavior is undefined. import std.stdio; immutable uint a0 = 10; immutable uint a = 42; immutable uint b; static this() { b = 42; } void info(T)(string tag, ref T t) { writefln("%20s: %s %s @ %s", tag, T.stringof, t, ); } void mutate(alias t)() { info(t.stringof ~ " before", t); import std.traits : Unqual; auto p = cast(Unqual!(typeof(t))*) *p = *p + 1; info(t.stringof ~ " after ", t); } void main() { info("a0 for reference", a0); mutate!a; mutate!b; assert(b == 43); assert(a == 43); // <-- FAILS } May print a0 for reference: immutable(uint) 10 @ 69D390 a before: immutable(uint) 42 @ 69D394 a after : immutable(uint) 43 @ 69D394 b before: immutable(uint) 42 @ 6A9F70 b after : immutable(uint) 43 @ 6A9F70 core.exception.AssertError@deneme.d(851): Assertion failure Ali
Re: Newbie: can't manage some types...
On Monday, 31 October 2016 at 16:06:48 UTC, Adam D. Ruppe wrote: dmd yourfile.d winmm.lib i personally like https://dlang.org/spec/pragma.html#lib pragma(lib, "winmm.lib");
Re: strange -fPIC compilation error
On 10/31/2016 09:26 AM, Charles Hixson via Digitalmars-d-learn wrote: On 10/30/2016 11:34 PM, Daniel Kozak via Digitalmars-d-learn wrote: Dne 31.10.2016 v 02:30 Charles Hixson via Digitalmars-d-learn napsal(a): Well, that certainly changed the error messages. With dmd -defaultlib=/usr/lib/x86_64-linux-gnu/libphobos2.so test.d I get: /usr/include/dmd/druntime/import/core/stdc/stdio.d(1121): Error: found 'nothrow' when expecting '{' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1123): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1124): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1125): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1126): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1127): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1128): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1129): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1133): Error: asm statements must end in ';' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1136): Error: found 'private' instead of statement /usr/include/dmd/druntime/import/core/stdc/stdio.d(1146): Error: no identifier for declarator add /usr/include/dmd/druntime/import/core/stdc/stdio.d(1149): Error: no identifier for declarator usDone /usr/include/dmd/druntime/import/core/stdc/stdio.d(1149): Error: Declaration expected, not ':' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1157): Error: Declaration expected, not '(' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1159): Error: Declaration expected, not 'foreach' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1159): Error: Declaration expected, not '0' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1164): Error: no identifier for declarator __fhnd_info[fd] /usr/include/dmd/druntime/import/core/stdc/stdio.d(1164): Error: Declaration expected, not '=' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1165): Error: Declaration expected, not 'return' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1167): Error: unrecognized declaration /usr/include/dmd/phobos/std/typecons.d(1124): Error: semicolon expected following function declaration This seems to be problem with your installation, you probably have diferen version of dmd compiler and phobos library. So you should uninstall all your dmd packages and make sure there is no /usr/include/dmd left in your system. And instal dmd only from one source (d-apt idealy). I've done that 2 or 3 times. If that's the problem, then there are different versions stored in the repository. Since I'm on debian testing I'd been assuming that there'd been some system change since I'd last used the compiler, and the debs weren't yet up to date. The only updates to my system prior to the compiler breaking HAD been via apt-get. Since then I've used dpkg remove and install a couple of times to try other versions of dmd with no benefit. Currently dmd-bin version 2.071.2-0 libphobos2.071.2-0 libphobos2.071.2-0 so they're LISTED as being the same version. And dmd.conf was installed by the deb, and is (eliminating the comments): [Environment32] DFLAGS=-I/usr/include/dmd/phobos -I/usr/include/dmd/druntime/import -L-L/usr/lib/i386-linux-gnu -L--export-dynamic [Environment64] DFLAGS=-I/usr/include/dmd/phobos -I/usr/include/dmd/druntime/import -L-L/usr/lib/x86_64-linux-gnu -L--export-dynamic But somewhere during the process (which included the nightly system update) the error messages changed, and now: dmd test.d yields: /usr/include/dmd/druntime/import/core/stdc/stdio.d(1121): Error: found 'nothrow' when expecting '{' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1123): Error: mismatched number of curly brackets ... /usr/include/dmd/druntime/import/core/stdc/stdio.d(1167): Error: unrecognized declaration /usr/include/dmd/phobos/std/typecons.d(1124): Error: semicolon expected following function declaration FWIW starting at /usr/include/dmd/druntime/import/core/stdc/stdio.d(1121):: asm nothrow @nogc { mov EDX, num; lock; inc _iSemLockCtrs[EDX * 2]; so nothrow isn't being seen as appropriate at the beginning of an asm block. After that I think it gets confused as 1123 doesn't HAVE a brace (i.e. curly bracket) in it.
Re: newbie problem with nothrow
On Monday, 31 October 2016 at 16:55:51 UTC, WhatMeWorry wrote: Is there a way to turn off nothrow or work around it? Because to me it looks like nothrow prevents me from doing anything useful. extern(C) void onKeyEvent(GLFWwindow* window, int key, int scancode, int action, int modifier) nothrow { if(queue.roomInQueue()) { auto event = new Event; event.type = EventType.keyboard; event.keyboard.key = cast(Key) key; // etc. } Error: function 'event_handler.CircularQueue.roomInQueue' is not nothrow Error: function 'event_handler.onKeyEvent' is nothrow yet may throw The compiler wouldn't let me just remove "nothrow" from the function. I tried a kludge where I had this function just pass all its parameters to another throwable function, but this caused errors as well. So I'm stuck. Anyone know how to proceed. Thanks. Wrap a body of the function to try {} catch {} and it'll work.
newbie problem with nothrow
Is there a way to turn off nothrow or work around it? Because to me it looks like nothrow prevents me from doing anything useful. extern(C) void onKeyEvent(GLFWwindow* window, int key, int scancode, int action, int modifier) nothrow { if(queue.roomInQueue()) { auto event = new Event; event.type = EventType.keyboard; event.keyboard.key = cast(Key) key; // etc. } Error: function 'event_handler.CircularQueue.roomInQueue' is not nothrow Error: function 'event_handler.onKeyEvent' is nothrow yet may throw The compiler wouldn't let me just remove "nothrow" from the function. I tried a kludge where I had this function just pass all its parameters to another throwable function, but this caused errors as well. So I'm stuck. Anyone know how to proceed. Thanks.
Re: Newbie: can't manage some types...
Thanks Adam for your kindness, it Works now. Thanks Alfred for the hint on setting the console output to 65001, will be useful as well. Greetings Cleverson
Re: strange -fPIC compilation error
On 10/30/2016 11:34 PM, Daniel Kozak via Digitalmars-d-learn wrote: Dne 31.10.2016 v 02:30 Charles Hixson via Digitalmars-d-learn napsal(a): Well, that certainly changed the error messages. With dmd -defaultlib=/usr/lib/x86_64-linux-gnu/libphobos2.so test.d I get: /usr/include/dmd/druntime/import/core/stdc/stdio.d(1121): Error: found 'nothrow' when expecting '{' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1123): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1124): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1125): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1126): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1127): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1128): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1129): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1133): Error: asm statements must end in ';' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1136): Error: found 'private' instead of statement /usr/include/dmd/druntime/import/core/stdc/stdio.d(1146): Error: no identifier for declarator add /usr/include/dmd/druntime/import/core/stdc/stdio.d(1149): Error: no identifier for declarator usDone /usr/include/dmd/druntime/import/core/stdc/stdio.d(1149): Error: Declaration expected, not ':' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1157): Error: Declaration expected, not '(' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1159): Error: Declaration expected, not 'foreach' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1159): Error: Declaration expected, not '0' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1164): Error: no identifier for declarator __fhnd_info[fd] /usr/include/dmd/druntime/import/core/stdc/stdio.d(1164): Error: Declaration expected, not '=' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1165): Error: Declaration expected, not 'return' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1167): Error: unrecognized declaration /usr/include/dmd/phobos/std/typecons.d(1124): Error: semicolon expected following function declaration This seems to be problem with your installation, you probably have diferen version of dmd compiler and phobos library. So you should uninstall all your dmd packages and make sure there is no /usr/include/dmd left in your system. And instal dmd only from one source (d-apt idealy). I've done that 2 or 3 times. If that's the problem, then there are different versions stored in the repository. Since I'm on debian testing I'd been assuming that there'd been some system change since I'd last used the compiler, and the debs weren't yet up to date. The only updates to my system prior to the compiler breaking HAD been via apt-get. Since then I've used dpkg remove and install a couple of times to try other versions of dmd with no benefit. Currently dmd-bin version 2.071.2-0 libphobos2.071.2-0 libphobos2.071.2-0 so they're LISTED as being the same version. And dmd.conf was installed by the deb, and is (eliminating the comments): [Environment32] DFLAGS=-I/usr/include/dmd/phobos -I/usr/include/dmd/druntime/import -L-L/usr/lib/i386-linux-gnu -L--export-dynamic [Environment64] DFLAGS=-I/usr/include/dmd/phobos -I/usr/include/dmd/druntime/import -L-L/usr/lib/x86_64-linux-gnu -L--export-dynamic But somewhere during the process (which included the nightly system update) the error messages changed, and now: dmd test.d yields: /usr/include/dmd/druntime/import/core/stdc/stdio.d(1121): Error: found 'nothrow' when expecting '{' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1123): Error: mismatched number of curly brackets ... /usr/include/dmd/druntime/import/core/stdc/stdio.d(1167): Error: unrecognized declaration /usr/include/dmd/phobos/std/typecons.d(1124): Error: semicolon expected following function declaration
Re: Newbie: can't manage some types...
On Monday, 31 October 2016 at 14:02:01 UTC, Cleverson Casarin Uliana wrote: Error 42: Symbol Undefined _PlaySoundW@12 This is the most common linker error, it means you used a function without including the library. PlaySound's docs https://msdn.microsoft.com/en-us/library/windows/desktop/dd743680%28v=vs.85%29.aspx at the bottom list some facts about it. One is "Library - winmm.lib" When you use that function, gotta include the library file somehow. Easiest is to just list it on your compile command: dmd yourfile.d winmm.lib since winmm.lib is provided with the operating system, that should just work.
Re: general questions about static this() at module level
On Monday, 31 October 2016 at 05:42:16 UTC, sarn wrote: On Monday, 31 October 2016 at 04:35:35 UTC, WhatMeWorry wrote: [...] I've seen hacks to do the same thing in C++. They're not pretty, though. [...] Class/struct static constructors are good for initialising class/struct static data. Module static constructors are good for initialising module "static data" (i.e., globals). They're especially handy for initialising immutable global data (which is the kind of global data D encourages). BTW, immutable data is shared between threads, so you should use "shared static this" to initialise it. Regular static constructors are executed per thread. Thanks! If you don't mind a follow up question, is this: immutable uint maxSize = 128; identical to this: immutable uint maxSize; static this() { maxSize = 128; }
Re: Newbie: can't manage some types...
On Monday, 31 October 2016 at 11:44:25 UTC, Cleverson Casarin Uliana wrote: Hello all, I'm trying to do two tasks which involves some type conversion, and I'm having dificulties, probably because I haven't yet understood well how such types works. First, I wanted to convert UTF-8 strings to ansi, so it displays correctly at the Windows command prompt. One function to do that is "toMBSz" from std.windows.charset, which is declared as follows: const(char)* toMBSz(in char[] s, uint codePage = 0); So, after some struggling, I managed to write the following code: import std.stdio; import std.windows.charset; void main() { string s = "Testando acentuação"; auto r = toMBSz (s, 1); writeln (r[0..19]); } Although the above code works, I have an impression that it could be more elegant and concise, but don't know how to improve it... I'd also like to play a sound file, so tried to use the function "PlaySound" from core.sys.windows.mmsystem, declared as follows: BOOL PlaySoundW(LPCWSTR, HMODULE, DWORD); According to some error messages I receive, the first parameter is of type const(char)*, which corresponds to the sound file name, but I just can't figure out how to convert the string (or a char array) to that type... Could you please give some snippet example? The closest I have come, which doesn't compile at all, is as follows: import core.sys.windows.mmsystem; void main() { char[] f = "C:/base/portavox/som/_fon102.wav".dup; const(wchar)* arq = cast(const(wchar)*) void* nulo; uint SND_FILENAME; PlaySound (arq, nulo, SND_FILENAME); } Thank you, Cleverson Cleverson, About your question related to "Testando acentuação", and assuming you're using Windows, you can also do the following: import std.stdio, std.string; //A Windows function to set the code page of the console output extern (Windows): private int SetConsoleOutputCP(uint codepage); void main() { SetConsoleOutputCP(65001); string s = "Testando acentuação"; writeln("Output: ", s.toUpper()); } Cheers
Re: ACM paper: CPU is the new bottleneck
On the other hand if you do more IO, you can have higher CPU load due to compression and serialization.
Re: Newbie: can't manage some types...
Thank you very much, Adam, now I'm receiving another strange error. My code is this: import core.sys.windows.mmsystem; void main() { PlaySoundW("C:/base/portavox/som/_fon102.wav"w.ptr, null, SND_FILENAME); } When trying to compile, it returns: OPTLINK (R) for Win32 Release 8.00.17 Copyright (C) Digital Mars 1989-2013 All rights reserved. http://www.digitalmars.com/ctg/optlink.html tocaSom.obj(tocaSom) Error 42: Symbol Undefined _PlaySoundW@12 --- errorlevel 1 I made some quick searches, but not sure whether there is some error messages' index ? Thanks for helping, Cleverson
Re: Newbie: can't manage some types...
On Monday, 31 October 2016 at 11:44:25 UTC, Cleverson Casarin Uliana wrote: Although the above code works, I have an impression that it could be more elegant and concise, but don't know how to improve it... That's OK except for the last line... the length isn't necessarily correct so your slice can be wrong. I'd just use printf or some other function that handles the zero-terminated char* instead of slicing it. char[] f = "C:/base/portavox/som/_fon102.wav".dup; const(wchar)* arq = cast(const(wchar)*) void* nulo; uint SND_FILENAME; PlaySound (arq, nulo, SND_FILENAME); Oh, that can be much, much, much simpler. Try PlaySoundW("c:/file.wav"w.ptr, null, SND_FILENAME); So a few notes: * SND_FILENAME is a constant defined in the header. You shouldn't define it yourself, it isn't meant to be a variable. * The PlaySoundW function takes a wstring, which you can get in D by sticking the `w` at the end of the literal. So `"foo"` is a normal string, but `"foo"w` is a wstring. (The difference is normal is utf-8, wstring is utf-16, which Windows uses internally.) * It furthermore takes a pointer, but you want a pointer to data. A D string or array has a pointer internally you can fetch via the `.ptr` property. Windows expects this string to be zero-terminated... which D string literals are, but other D strings may not be. There's a function in `std.utf` that guarantees it: http://dpldocs.info/experimental-docs/std.utf.toUTF16z.html const(wchar)* safe_to_pass_to_windows = toUTF16z("your string");
Re: general questions about static this() at module level
On Monday, 31 October 2016 at 04:35:35 UTC, WhatMeWorry wrote: I am fascinated with D's module static this. I have shamelessly stolen Ali's code directly from his book. module cat; static this() { // ... the initial operations of the module ... } static ~this() { // ... the final operations of the module ... } First, are there any other languages that has this feature? The few I know, certainly don't. Object Pascal has them: unit foo; // like a D module. interface // declarations..., // like the content of a C/C++ *.h/*.hpp implementation // implementation like the content of a C/C++ *.c/*.cpp initialization // statements... // like in a D static this(){} finalization // statements... // like in a D static ~this(){} end. Actually without them a lot of stuffs wouldn't work properly. They are more used than their D equivalent, particularly to register classes for the object streaming system but not only.
Newbie: can't manage some types...
Hello all, I'm trying to do two tasks which involves some type conversion, and I'm having dificulties, probably because I haven't yet understood well how such types works. First, I wanted to convert UTF-8 strings to ansi, so it displays correctly at the Windows command prompt. One function to do that is "toMBSz" from std.windows.charset, which is declared as follows: const(char)* toMBSz(in char[] s, uint codePage = 0); So, after some struggling, I managed to write the following code: import std.stdio; import std.windows.charset; void main() { string s = "Testando acentuação"; auto r = toMBSz (s, 1); writeln (r[0..19]); } Although the above code works, I have an impression that it could be more elegant and concise, but don't know how to improve it... I'd also like to play a sound file, so tried to use the function "PlaySound" from core.sys.windows.mmsystem, declared as follows: BOOL PlaySoundW(LPCWSTR, HMODULE, DWORD); According to some error messages I receive, the first parameter is of type const(char)*, which corresponds to the sound file name, but I just can't figure out how to convert the string (or a char array) to that type... Could you please give some snippet example? The closest I have come, which doesn't compile at all, is as follows: import core.sys.windows.mmsystem; void main() { char[] f = "C:/base/portavox/som/_fon102.wav".dup; const(wchar)* arq = cast(const(wchar)*) void* nulo; uint SND_FILENAME; PlaySound (arq, nulo, SND_FILENAME); } Thank you, Cleverson
Re: Neural Networks / ML Libraries for D
On Tuesday, 25 October 2016 at 11:17:29 UTC, Saurabh Das wrote: Hello, Are there any good ML libraries for D? In particular, looking for a neural network library currently. Any leads would be appreciated. Thanks, Saurabh There is also Henry Gouk's dnnet library[1]. I'm not sure how far is it in development, since there's no dub project registered at code.dlang.org, but you could give it a spin. AFAIK it relies another package of his, dopt[2][3], which depends on CUDA (cuBLAS and cuDNN). [1] https://github.com/henrygouk/dnnet [2] https://github.com/henrygouk/dopt [3] http://code.dlang.org/packages/dopt
Re: Neural Networks / ML Libraries for D
On Wednesday, 26 October 2016 at 12:13:16 UTC, Ilya Yaroshenko wrote: https://github.com/ljubobratovicrelja/mir.experimental.model.rbf Now moved to https://github.com/libmir/mir-neural
Re: strange -fPIC compilation error
Hello, From GCC 6.2, -fpie is becoming the default setting at compile and at link time. As dmd uses GCC to link, now the code needs to be compiled with a special option. Which means you need, at the moment, to add the following options to your dmd.conf: -defaultlib=libphobos2.so -fPIC (the change from GCC is related to security and address space randomization).
Re: strange -fPIC compilation error
Dne 31.10.2016 v 02:30 Charles Hixson via Digitalmars-d-learn napsal(a): Well, that certainly changed the error messages. With dmd -defaultlib=/usr/lib/x86_64-linux-gnu/libphobos2.so test.d I get: /usr/include/dmd/druntime/import/core/stdc/stdio.d(1121): Error: found 'nothrow' when expecting '{' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1123): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1124): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1125): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1126): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1127): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1128): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1129): Error: mismatched number of curly brackets /usr/include/dmd/druntime/import/core/stdc/stdio.d(1133): Error: asm statements must end in ';' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1136): Error: found 'private' instead of statement /usr/include/dmd/druntime/import/core/stdc/stdio.d(1146): Error: no identifier for declarator add /usr/include/dmd/druntime/import/core/stdc/stdio.d(1149): Error: no identifier for declarator usDone /usr/include/dmd/druntime/import/core/stdc/stdio.d(1149): Error: Declaration expected, not ':' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1157): Error: Declaration expected, not '(' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1159): Error: Declaration expected, not 'foreach' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1159): Error: Declaration expected, not '0' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1164): Error: no identifier for declarator __fhnd_info[fd] /usr/include/dmd/druntime/import/core/stdc/stdio.d(1164): Error: Declaration expected, not '=' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1165): Error: Declaration expected, not 'return' /usr/include/dmd/druntime/import/core/stdc/stdio.d(1167): Error: unrecognized declaration /usr/include/dmd/phobos/std/typecons.d(1124): Error: semicolon expected following function declaration This seems to be problem with your installation, you probably have diferen version of dmd compiler and phobos library. So you should uninstall all your dmd packages and make sure there is no /usr/include/dmd left in your system. And instal dmd only from one source (d-apt idealy).