Endianness - How to test code for portability
In the portability section of the language spec, they talk about endianness (https://dlang.org/spec/portability.html#endianness) which refers "to the order in which multibyte types are stored." IMO if you wanted to actually be sure your code is portable across both big endian and little endian systems, you should actually run your code on both types of systems and test if there any issues. The problem is that I am not aware of any big-endian systems that you can actually test on and if there is any D lang compiler support for any of these systems if they exist. This is not an important issue to me but I was just curious to see if anyone actually tests for portability issues related to endianness by compiling their D Lang code for a big endian architecture and actually running it on that system.
Re: Is it possible to suppress standard lib and dlang symbols in dylib (macos)
On Thursday, 11 March 2021 at 08:34:48 UTC, David wrote: I thought it would be fun to convert some old C++/C quant utils to D. I'm starting with a simple library that I call from vba in Excel on macos: module xlutils; import core.stdc.string : strlen, strcpy; //import std.conv : to; //import std.string : toStringz; import core.stdc.stdlib : malloc, free; extern (C) double addDD_D(double a, double b) {return a + b;} ... Is there a way of not exposing the symbols that aren't mine? - I only need a simple C interface. Thx David Create a exports.lst file with: _addDD_D as the only line there. Build with: "lflags-osx-ldc": [ "-exported_symbols_list", "exports.lst", "-dead_strip" ],
Re: Is it possible to suppress standard lib and dlang symbols in dylib (macos)
On Thursday, 11 March 2021 at 18:35:37 UTC, Imperatorn wrote: On Thursday, 11 March 2021 at 17:00:06 UTC, David wrote: On Thursday, 11 March 2021 at 14:49:32 UTC, Imperatorn wrote: On Thursday, 11 March 2021 at 10:29:55 UTC, David wrote: On Thursday, 11 March 2021 at 08:40:58 UTC, Imperatorn wrote: [...] [...] Of course - but unless I've missed something I don't believe it works on macos. Hmm, I'm not sure. Have you tried? Nope the documentation implies it only works on windows so I've ruled it out. But the thing I'm trying to solve is suppression of symbols in the library. I see, there might be something similar to .def? https://docs.microsoft.com/en-us/cpp/build/reference/module-definition-dot-def-files?redirectedfrom=MSDN=msvc-160 Or I guess you could strip it? https://www.linux.org/docs/man1/strip.html But I guess you already thought of that and want to not even generate them in the first place? I was wondering if there was something similar to def - my next attempt will be a combination of ldc and export. I wasn't aware that object files could be manipulated like the strip manual page - thx for the heads up.
Re: Static initialization of associative arrays
On Thursday, 11 March 2021 at 18:41:08 UTC, Ali Çehreli wrote: On 3/11/21 10:06 AM, Chris Piker wrote: >https://dlang.org/spec/hash-map.html#static_initialization > > that this feature is not yet implemented. I use a shared static this() block: immutable string[int] aa; shared static this() { aa = [ 1: "one" ]; } void main() { assert(aa.length == 1); } And it is possible to build an AA at compile time as the initial value but it still needs a trivial assigment to the immutable variable. Assuming that we have the following file at compile time named 'my_aa': --- 8< --- 1 one 2 two --- 8< --- And remembering that we have to use the -J switch when compiling (e.g. as -J.), you can parse and build an AA from that file like this. (Sorry for insisting on the the range style; it can be done in other ways). immutable string[int] aa; shared static this() { import std.algorithm; import std.range; import std.typecons; import std.conv; enum compileTimeAA = import("my_aa") .splitter .chunks(2) .map!(a => tuple(a.front.to!int, a.dropOne.front)) .assocArray; aa = compileTimeAA; } import std.stdio; void main() { writeln(aa); } Ali You can however do like this (cheating): https://run.dlang.io/is/9TSfAB "The variable myOptions is assigned the result of the literal at runtime. But because it's immutable, the compiler knows what's in it. So it can extrapolate back to the literal what it is at compile time" We had a discussion in Discord about this last week.
Re: Static initialization of associative arrays
On Thursday, 11 March 2021 at 19:12:34 UTC, H. S. Teoh wrote: On Thu, Mar 11, 2021 at 06:06:35PM +, Chris Piker via immutable int[string] aa; shared static this() { aa = [ "abc": 123, "def": 456, /* ... */ ]; } Hi H.S.T Yes, I'm using static if, but do you know if direct implementation of immutable associative array assignment (as given on the language spec page) will be implemented at some point? This is not a make-or-break feature that I need, just an opportunity to gain meta-information. The actual problem is easy to work around, I'm mostly asking out of curiosity to learn more about the D ecosystem and how it functions. Thanks,
Re: Static initialization of associative arrays
On Thursday, 11 March 2021 at 18:41:08 UTC, Ali Çehreli wrote: On 3/11/21 10:06 AM, Chris Piker wrote: >https://dlang.org/spec/hash-map.html#static_initialization > > that this feature is not yet implemented. I use a shared static this() block: immutable string[int] aa; shared static this() { aa = [ 1: "one" ]; } Ali Hi Ali Always good to hear from an author. I picked up a copy of your book the other day... nice work! I appreciate the tips on compile-time execution (hadn't thought of that), but do you know how I find out about the implementation status of a feature in D? I'm aware of the DIP system, but I though DIPs were just for language changes. Thanks,
Re: Static initialization of associative arrays
On Thu, Mar 11, 2021 at 06:06:35PM +, Chris Piker via Digitalmars-d-learn wrote: [...] > Today I ran across a situation where an immutable associative array > would be handy. While perusing the language spec I noticed here: > > https://dlang.org/spec/hash-map.html#static_initialization > > that this feature is not yet implemented. [...] The subsequent section on the linked page gives the solution / workaround. Just declare your immutable AA without initialization, and initialize it in a static ctor: immutable int[string] aa; shared static this() { aa = [ "abc": 123, "def": 456, /* ... */ ]; } T -- INTEL = Only half of "intelligence".
Re: Static initialization of associative arrays
On 3/11/21 10:06 AM, Chris Piker wrote: >https://dlang.org/spec/hash-map.html#static_initialization > > that this feature is not yet implemented. I use a shared static this() block: immutable string[int] aa; shared static this() { aa = [ 1: "one" ]; } void main() { assert(aa.length == 1); } And it is possible to build an AA at compile time as the initial value but it still needs a trivial assigment to the immutable variable. Assuming that we have the following file at compile time named 'my_aa': --- 8< --- 1 one 2 two --- 8< --- And remembering that we have to use the -J switch when compiling (e.g. as -J.), you can parse and build an AA from that file like this. (Sorry for insisting on the the range style; it can be done in other ways). immutable string[int] aa; shared static this() { import std.algorithm; import std.range; import std.typecons; import std.conv; enum compileTimeAA = import("my_aa") .splitter .chunks(2) .map!(a => tuple(a.front.to!int, a.dropOne.front)) .assocArray; aa = compileTimeAA; } import std.stdio; void main() { writeln(aa); } Ali
Re: Is it possible to suppress standard lib and dlang symbols in dylib (macos)
On Thursday, 11 March 2021 at 17:00:06 UTC, David wrote: On Thursday, 11 March 2021 at 14:49:32 UTC, Imperatorn wrote: On Thursday, 11 March 2021 at 10:29:55 UTC, David wrote: On Thursday, 11 March 2021 at 08:40:58 UTC, Imperatorn wrote: On Thursday, 11 March 2021 at 08:34:48 UTC, David wrote: I thought it would be fun to convert some old C++/C quant utils to D. I'm starting with a simple library that I call from vba in Excel on macos: [...] *trigger warning* "vba in Excel on macos" ⚠️ Btw, have you looked at excel-d? https://code.dlang.org/packages/excel-d Btw, have you looked at excel-d? Of course - but unless I've missed something I don't believe it works on macos. Hmm, I'm not sure. Have you tried? Nope the documentation implies it only works on windows so I've ruled it out. But the thing I'm trying to solve is suppression of symbols in the library. I see, there might be something similar to .def? https://docs.microsoft.com/en-us/cpp/build/reference/module-definition-dot-def-files?redirectedfrom=MSDN=msvc-160 Or I guess you could strip it? https://www.linux.org/docs/man1/strip.html But I guess you already thought of that and want to not even generate them in the first place?
Static initialization of associative arrays
Hi D At work I've begun writing programs in D that I would typically write in python. My goal is to get away from split python/C development and just use one language most of the time. Today I ran across a situation where an immutable associative array would be handy. While perusing the language spec I noticed here: https://dlang.org/spec/hash-map.html#static_initialization that this feature is not yet implemented. So where do I go learn about the status of a feature? I'd like to check on the progress of this particular one. For now I'll use a 'static if' construct to ready the array. As someone with a python background it's nice if I can avoid extra lines of code for straight forward ideas, but I understand that switching to faster compiled code doesn't come for free and some extra boiler-plate will be needed. By the way I do like that intended features are documented up front, even if no one's had time to work on them. Thanks,
Re: can't link a code, is it a bug or my fault?
Try it with: -allinst It may just be deciding a template instance isn't required.
Re: can't link a code, is it a bug or my fault?
On 3/11/21 8:41 AM, Iliya wrote: > I am using dmd 2.094.1 on linux Your program links fine for me with 2.094.2 on Linux. Ali
Re: can't link a code, is it a bug or my fault?
On Thursday, 11 March 2021 at 17:25:35 UTC, Ali Çehreli wrote: On 3/11/21 8:41 AM, Iliya wrote: > I am using dmd 2.094.1 on linux Your program links fine for me with 2.094.2 on Linux. Ali Thank you, Ali! I also played with it on playground. Seems that it is a bug. The compilation results: Up to 2.093.1: Success and no output Since 2.094.1: Failure with output: - onlineapp.o: In function `_D9onlineapp__T4findTaTSQw__T7IsEqualTaZQlZQBcFNaNbNiNfIAaIaQBlZm': /sandbox/onlineapp.d:15: undefined reference to `_D9onlineapp__T7IsEqualTaZQl6opCallFNaNbNiNfIaIaZb' collect2: error: ld returned 1 exit status Error: linker exited with status 1 -
Re: Is it possible to suppress standard lib and dlang symbols in dylib (macos)
On Thursday, 11 March 2021 at 14:49:32 UTC, Imperatorn wrote: On Thursday, 11 March 2021 at 10:29:55 UTC, David wrote: On Thursday, 11 March 2021 at 08:40:58 UTC, Imperatorn wrote: On Thursday, 11 March 2021 at 08:34:48 UTC, David wrote: I thought it would be fun to convert some old C++/C quant utils to D. I'm starting with a simple library that I call from vba in Excel on macos: [...] *trigger warning* "vba in Excel on macos" ⚠️ Btw, have you looked at excel-d? https://code.dlang.org/packages/excel-d Btw, have you looked at excel-d? Of course - but unless I've missed something I don't believe it works on macos. Hmm, I'm not sure. Have you tried? Nope the documentation implies it only works on windows so I've ruled it out. But the thing I'm trying to solve is suppression of symbols in the library.
Re: Is it possible to suppress standard lib and dlang symbols in dylib (macos)
On Thursday, 11 March 2021 at 14:35:45 UTC, rikki cattermole wrote: Pipe it to grep should work | grep -v "__D2" Thanks - though I'm trying to suppress the symbols being generated in the library. A colleague says it can be done in ldc but not dmd. I'll think I'll try that out.
can't link a code, is it a bug or my fault?
Hello. I am using dmd 2.094.1 on linux for compiling a piece code I found on the github. Unfortunately linker can't link it as it can't find opCall symbol instantiation demangled as: pure nothrow @nogc @safe bool app.IsEqual!(char).IsEqual.opCall(in char, in char) Do you have any ideas on: 1. Why compiler searches for such function attributes - pure nothrow @nogc @safe, as I don't specify them. Is this a bug or feature?) 2. What changes are needed to compile this rather simple example. Please, help me! Reference to my piece of code in app.d: struct IsEqual(T) { static bool opCall( in T p1, in T p2 ) { return p1 == p2; } } size_t find(T, Pred = IsEqual!(T)) (in T[] haystack, in T needle, Pred pred = Pred.init) { foreach ( pos, cur; haystack ) { if( pred( cur, needle ) ) return pos; } return haystack.length; } int main(string[] args) { auto pos = find("abc" , 'b'); assert(pos == 1); return 0; } Linker error message: Linking... /usr/bin/ld: .dub/build/application-debug-linux.posix-x86_64-dmd_2094-BD99179F1B52B13DDA1C2B0172F5081E/app.bin.o: in function `_D3app__T4findTaTSQq__T7IsEqualTaZQlZQBcFNaNbNiNfIAaIaQBlZm': source/app.d:15: undefined reference to `_D3app__T7IsEqualTaZQl6opCallFNaNbNiNfIaIaZb' collect2: error: ld returned 1 exit status ddemangle output on these symbols: _D3app__T7IsEqualTaZQl6opCallFNaNbNiNfIaIaZb pure nothrow @nogc @safe bool app.IsEqual!(char).IsEqual.opCall(in char, in char) _D3app__T4findTaTSQq__T7IsEqualTaZQlZQBcFNaNbNiNfIAaIaQBlZm pure nothrow @nogc @safe ulong app.find!(char, app.IsEqual!(char).IsEqual).find(in char[], in char, app.IsEqual!(char).IsEqual) Thanks you!
Re: Two functions with different args. Taking address of the one
On Thursday, 11 March 2021 at 14:23:39 UTC, Adam D. Ruppe wrote: On Thursday, 11 March 2021 at 12:26:07 UTC, Виталий Фадеев wrote: _processMouseKey = // <-- not works _processMouseMove = // <-- not works This *should* actually work. What type are those variables? struct MouseKeyEvent {} struct MouseMoveEvent{} void process( ref MouseKeyEvent event ) { } void process( ref MouseMoveEvent event ) { } void main() { // this works because the type is given on the left // so the compiler knows which overload works void function(ref MouseMoveEvent) processMouseMove = } I was stupid. It is really simple! Thank a lot, bro!
Re: Is it possible to suppress standard lib and dlang symbols in dylib (macos)
On Thursday, 11 March 2021 at 10:29:55 UTC, David wrote: On Thursday, 11 March 2021 at 08:40:58 UTC, Imperatorn wrote: On Thursday, 11 March 2021 at 08:34:48 UTC, David wrote: I thought it would be fun to convert some old C++/C quant utils to D. I'm starting with a simple library that I call from vba in Excel on macos: [...] *trigger warning* "vba in Excel on macos" ⚠️ Btw, have you looked at excel-d? https://code.dlang.org/packages/excel-d Btw, have you looked at excel-d? Of course - but unless I've missed something I don't believe it works on macos. Hmm, I'm not sure. Have you tried?
Re: Is it possible to suppress standard lib and dlang symbols in dylib (macos)
Pipe it to grep should work | grep -v "__D2"
Re: Two functions with different args. Taking address of the one
On Thursday, 11 March 2021 at 12:26:07 UTC, Виталий Фадеев wrote: _processMouseKey = // <-- not works _processMouseMove = // <-- not works This *should* actually work. What type are those variables? struct MouseKeyEvent {} struct MouseMoveEvent{} void process( ref MouseKeyEvent event ) { } void process( ref MouseMoveEvent event ) { } void main() { // this works because the type is given on the left // so the compiler knows which overload works void function(ref MouseMoveEvent) processMouseMove = }
Re: Two functions with different args. Taking address of the one
On Thursday, 11 March 2021 at 13:14:56 UTC, Paul Backus wrote: On Thursday, 11 March 2021 at 12:56:34 UTC, Виталий Фадеев wrote: [...] Something like this: template Overloads(alias symbol) { static if (__traits(compiles, __traits(parent, symbol))) alias Overloads = __traits(getOverloads, __traits(parent, symbol), __traits(identifier, symbol) ); else alias Overloads = symbol; } auto getOverloadFor(alias fun, T)() { foreach (overload; Overloads!fun) static if (__traits(compiles, (T arg) { overload(arg); })) return } Usage: __processMouseKey = getOverloadFor!(process, MouseKeyEvent); Thanks a lot, bro!
Re: Two functions with different args. Taking address of the one
On Thursday, 11 March 2021 at 12:56:34 UTC, Виталий Фадеев wrote: What right way to call function directly with selecting one of two ? If they are not nested functions, you can also do: ``` // Separate names void processKey (ref MouseKeyEvent event) {...} void processMove(ref MouseMoveEvent event) {...} // Still create overload set alias process = processKey; alias process = processMove; _processMouseKey = _processMouseMove = ```
Re: Two functions with different args. Taking address of the one
On Thursday, 11 March 2021 at 12:56:34 UTC, Виталий Фадеев wrote: This will generate lambda: __processMouseKey = (ref MouseKeyEvent event) { process(event); }; two calls: call labnda; call process; What right way to call function directly with selecting one of two ? Something like this: template Overloads(alias symbol) { static if (__traits(compiles, __traits(parent, symbol))) alias Overloads = __traits(getOverloads, __traits(parent, symbol), __traits(identifier, symbol) ); else alias Overloads = symbol; } auto getOverloadFor(alias fun, T)() { foreach (overload; Overloads!fun) static if (__traits(compiles, (T arg) { overload(arg); })) return } Usage: __processMouseKey = getOverloadFor!(process, MouseKeyEvent);
Re: Two functions with different args. Taking address of the one
On Thursday, 11 March 2021 at 12:48:13 UTC, Paul Backus wrote: On Thursday, 11 March 2021 at 12:26:07 UTC, Виталий Фадеев wrote: Have: void process( ref MouseKeyEvent event ) { ... } void process( ref MouseMoveEvent event ) { ... } Want: _processMouseKey = // <-- not works _processMouseMove = // <-- not works What is correct way to get address of function with specific argument ? You can use __traits(getOverloads, process) plus some metaprogramming to get the address of a specific overload. But IMO the easiest way is to use lambdas: __processMouseKey = (ref MouseKeyEvent event) { process(event); }; __processMouseMove = (ref MouseMoveEvent event) { process(event); }; This will generate lambda: __processMouseKey = (ref MouseKeyEvent event) { process(event); }; two calls: call labnda; call process; What right way to call function directly with selecting one of two ?
Re: Two functions with different args. Taking address of the one
On Thursday, 11 March 2021 at 12:26:07 UTC, Виталий Фадеев wrote: Have: void process( ref MouseKeyEvent event ) { ... } void process( ref MouseMoveEvent event ) { ... } Want: _processMouseKey = // <-- not works _processMouseMove = // <-- not works What is correct way to get address of function with specific argument ? You can use __traits(getOverloads, process) plus some metaprogramming to get the address of a specific overload. But IMO the easiest way is to use lambdas: __processMouseKey = (ref MouseKeyEvent event) { process(event); }; __processMouseMove = (ref MouseMoveEvent event) { process(event); };
Two functions with different args. Taking address of the one
Have: void process( ref MouseKeyEvent event ) { ... } void process( ref MouseMoveEvent event ) { ... } Want: _processMouseKey = // <-- not works _processMouseMove = // <-- not works What is correct way to get address of function with specific argument ?
Re: Is it possible to suppress standard lib and dlang symbols in dylib (macos)
On Thursday, 11 March 2021 at 08:40:58 UTC, Imperatorn wrote: On Thursday, 11 March 2021 at 08:34:48 UTC, David wrote: I thought it would be fun to convert some old C++/C quant utils to D. I'm starting with a simple library that I call from vba in Excel on macos: [...] *trigger warning* "vba in Excel on macos" ⚠️ Btw, have you looked at excel-d? https://code.dlang.org/packages/excel-d Btw, have you looked at excel-d? Of course - but unless I've missed something I don't believe it works on macos.
Re: Is it possible to suppress standard lib and dlang symbols in dylib (macos)
On Thursday, 11 March 2021 at 08:34:48 UTC, David wrote: I thought it would be fun to convert some old C++/C quant utils to D. I'm starting with a simple library that I call from vba in Excel on macos: [...] *trigger warning* "vba in Excel on macos" ⚠️ Btw, have you looked at excel-d? https://code.dlang.org/packages/excel-d
Is it possible to suppress standard lib and dlang symbols in dylib (macos)
I thought it would be fun to convert some old C++/C quant utils to D. I'm starting with a simple library that I call from vba in Excel on macos: module xlutils; import core.stdc.string : strlen, strcpy; //import std.conv : to; //import std.string : toStringz; import core.stdc.stdlib : malloc, free; extern (C) double addDD_D(double a, double b) {return a + b;} ... which results in: nm -gU libxlutils.dylib 3f10 S __D7xlutils12__ModuleInfoZ 3e44 T _addDD_D 3ebc T _addrC 3e84 T _freeP 3e9c T _strcpyCC 3e6c T _strlenC_L If I import `to` and `toStringz` I get more symbols than will fit in my shell output. E.g. 000318bc T __D2rt5minfo11ModuleGroup9sortCtorsMFAyaZ8findDepsMFmPmZ9__lambda5MFNbNiQBjZv 00031534 T __D2rt5minfo11ModuleGroup9sortCtorsMFAyaZ8findDepsMFmPmZb 00030dcc T __D2rt5minfo11ModuleGroup9sortCtorsMFAyaZv 00031db4 T __D2rt5minfo11ModuleGroup9sortCtorsMFZv 00048ef0 S __D2rt5minfo12__ModuleInfoZ 000327e4 T __D2rt5minfo16rt_moduleTlsCtorUZ14__foreachbody1MFKSQBx19sections_osx_x86_6412SectionGroupZi ... 000183ac T _thread_resumeAll 00018660 T _thread_scanAll 00018480 T _thread_scanAllType 00019658 T _thread_suspendAll Is there a way of not exposing the symbols that aren't mine? - I only need a simple C interface. Thx David