Re: Question about the $ sign in arrays and strings
oooh... I used str = std.readln(); to get my string and there must have been some other sign, line break or whitespace or something at the end :( Now I understand it, thx
Re: Question about the $ sign in arrays and strings
On Wednesday, 19 February 2020 at 07:04:48 UTC, Namal wrote: Hello, I wanted to remove the lastchar in a string and figured that you can do that wit str = str[0..$-2]; but why is str = str[0..$] and str=str[0..$-1] the same ? Why do you think that they are the same? $ rdmd --eval 'auto str = "hello"; writeln(str = str[0..$]); writeln(str = str[0..$-1])' hello hell
State of MIPS
What's the current state of MIPS compiling for bare metal? Especially the R4300i processor. I see MIPS on both GDC and LDC "partial support/bare metal" lists but them being somewhat vague about it I'm not quite sure which it means and I'm sure by now the processors and instruction sets are different from what they were in 1995. Thanks, April.
Question about the $ sign in arrays and strings
Hello, I wanted to remove the lastchar in a string and figured that you can do that wit str = str[0..$-2]; but why is str = str[0..$] and str=str[0..$-1] the same ?
Re: Two problems with json and lcd
On Tuesday, 18 February 2020 at 18:05:43 UTC, AlphaPurned wrote: json has two issues, it doesn't work with tuple: (isArray!T) goes to (isArray!T || (T.stringof.length > 4 && T.stringof[0..5] == "Tuple")) and right below else { static assert(false, text(`unable to convert type "`, T.Stringof, `" to json`)); } and it used Stringof. This fixes json to work with tuples. Second, LCD gives me the error: error : function `Test.main.rate!(d, "", "").rate` cannot access frame of function `Test.main.__foreachbody1` Not sure the problem, works fine with DMD. I'm simply accessing a variable outside a templated function. I didn't understand your first point, but if I got the gist of your second one, the difference may be due to LDC not yet having implemented this: https://github.com/ldc-developers/ldc/issues/3125
Two problems with json and lcd
json has two issues, it doesn't work with tuple: (isArray!T) goes to (isArray!T || (T.stringof.length > 4 && T.stringof[0..5] == "Tuple")) and right below else { static assert(false, text(`unable to convert type "`, T.Stringof, `" to json`)); } and it used Stringof. This fixes json to work with tuples. Second, LCD gives me the error: error : function `Test.main.rate!(d, "", "").rate` cannot access frame of function `Test.main.__foreachbody1` Not sure the problem, works fine with DMD. I'm simply accessing a variable outside a templated function.
Conditional Attributes
Hello! Say I have a struct where every member function can either be static or not depending on a template parameter. Is there a simple way to do this? Like, for example: struct Foo(Condition) { static if (Condition) static: void Bar() {} void Baz() {} }
Re: How to catch RangeError in unittest?
On Tuesday, 18 February 2020 at 13:07:35 UTC, wjoe wrote: I have a function add(index, data) which throws RangeError if the index is invalid. Never mind. For whatever reason RangeError is now caught. Sorry for the noise.
Re: Alternative to friend functions?
On Tuesday, 18 February 2020 at 12:43:22 UTC, Adnan wrote: class Wife(uint N) : Female { FemaleID engagedTo = -1; const MaleID[N] preferences; this(MaleID[N] preferences) { this.preferences = preferences; } } void engage(N)(ref Wife!N, wife, ref Husband!N husband) { // Here, I want to access both husband and wife's engaged_to } Petar's answer covers your question, so I won't elaborate on that, but I'd like to point out that as Wife and Husband are classes, you probably don't intend to take them by ref - classes are always by ref in D, so you're effectively passing a reference to a reference to a class in `engage`. Basically: class Foo { int n; } void fun(Foo f) { f.n = 3; // Local copy of the reference - does not modify other references. f = null; } void gun(ref Foo f) { f = null; } unittest { Foo f = new Foo(); Foo g = f; f.n = 17; // f and g point to the same object: assert(f.n == 17); assert(g.n == 17); fun(f); // fun() changed the object that both f and g point to: assert(f.n == 3); assert(g.n == 3); gun(f); // gun() changed f to no longer point at the same object, but left g untouched: assert(f is null); assert(g !is null); assert(g.n == 3); } -- Simen
How to catch RangeError in unittest?
I have a function add(index, data) which throws RangeError if the index is invalid. unittest { // is supposed to test that RangeError is thrown on a range violation assertThrown!RangeError(add(size_t.max, something)); //which doesn't work, the test is killed with core.exception.RangeError: Range violation // this kills the test, too: core.exception.RangeError: Range violation try { add(size_t.max, something); assert(false, "Expecting RangeError"); } catch (RangeError) {} } Just for clarification, I do _not_ intend to catch RangeError outside of a unittest. What I want is that the test acknowledges the correctly thrown RangeError and gracefully reports success. What are my options?
Re: How to declare a virtual member (not a function) in a class
On Tuesday, 18 February 2020 at 12:37:45 UTC, Adnan wrote: I have a base class that has a couple of constant member variables. These variables are abstract, they will only get defined when the derived class gets constructed. class Person { const string name; const int id; } class Male : Person { this(string name = "Unnamed Male") { static int nextID = 0; this.id = nextID++; this.name = name; } } The compiler restricts me from assigning those two functions. How can I get around this? `const` members must be initialized by the same class that declares them. What you could do is have the abstract Person class declare a constructor (which would initialize the `const` members) and call it from derived class (such as `Male`) constructors by the `super(arg1, arg2)` syntax. Alternatively, you could define `abstract` accessor functions in the base class and have the derived classes implement them. In D you can use the same syntax to call functions as if they were fields. (Before you had to put the @property attribute on such functions, but for the most part it is not necessary now.)
Re: Alternative to friend functions?
On Tuesday, 18 February 2020 at 12:43:22 UTC, Adnan wrote: What is the alternative to C++'s friend functions in D? module stable_matching; alias FemaleID = int; alias MaleID = int; class Person { string name; int id; } class Male : Person { this(string name = "Unnamed Male") { static int nextID = 0; this.id = nextID++; this.name = name; } } class Female : Person { this(string name = "Unnamed Female") { static int nextID = 0; this.id = nextID++; this.name = name; } } class Husband(uint N) : Male { FemaleID engagedTo = -1; const FemaleID[N] preferences; this(FemaleID[N] preferences) { this.preferences = preferences; } } class Wife(uint N) : Female { FemaleID engagedTo = -1; const MaleID[N] preferences; this(MaleID[N] preferences) { this.preferences = preferences; } } void engage(N)(ref Wife!N, wife, ref Husband!N husband) { // Here, I want to access both husband and wife's engaged_to } class MatchPool(uint N) { Husband!N[N] husbands; Wife!N[N] wives; } In D the unit of encapsulation is not class, but module, and so `private` only restricts access from other modules. If `engage` is declared in the same module as the classes, you should have no problems accessing their private members. If you want to put `engage` in a different module, than you can use the `package` access modifier to allow all modules in a given package to access `package` members.
Re: How to declare a virtual member (not a function) in a class
On Tuesday, 18 February 2020 at 12:37:45 UTC, Adnan wrote: I have a base class that has a couple of constant member variables. These variables are abstract, they will only get defined when the derived class gets constructed. class Person { const string name; const int id; } class Male : Person { this(string name = "Unnamed Male") { static int nextID = 0; this.id = nextID++; this.name = name; } } The compiler restricts me from assigning those two functions. How can I get around this? const members can only be set in the constructor of the type that defines them. To set them in a subclass, forward the values to the superclass' constructor: class Person { const string name; const int id; protected this(string _name, int _id) { id = _id; name = _name; } } class Male : Person { this(string name = "Unnamed Male") { static int nextID = 0; super(name, nextID++); } } -- Simen
Alternative to friend functions?
What is the alternative to C++'s friend functions in D? module stable_matching; alias FemaleID = int; alias MaleID = int; class Person { string name; int id; } class Male : Person { this(string name = "Unnamed Male") { static int nextID = 0; this.id = nextID++; this.name = name; } } class Female : Person { this(string name = "Unnamed Female") { static int nextID = 0; this.id = nextID++; this.name = name; } } class Husband(uint N) : Male { FemaleID engagedTo = -1; const FemaleID[N] preferences; this(FemaleID[N] preferences) { this.preferences = preferences; } } class Wife(uint N) : Female { FemaleID engagedTo = -1; const MaleID[N] preferences; this(MaleID[N] preferences) { this.preferences = preferences; } } void engage(N)(ref Wife!N, wife, ref Husband!N husband) { // Here, I want to access both husband and wife's engaged_to } class MatchPool(uint N) { Husband!N[N] husbands; Wife!N[N] wives; }
How to declare a virtual member (not a function) in a class
I have a base class that has a couple of constant member variables. These variables are abstract, they will only get defined when the derived class gets constructed. class Person { const string name; const int id; } class Male : Person { this(string name = "Unnamed Male") { static int nextID = 0; this.id = nextID++; this.name = name; } } The compiler restricts me from assigning those two functions. How can I get around this?
Re: How to get the name of an object's class at compile time?
On Tuesday, 18 February 2020 at 03:33:21 UTC, Basile B. wrote: On Monday, 17 February 2020 at 22:34:31 UTC, Stefan Koch wrote: Upon seeing this I just implemented typeid(stuff).name; https://github.com/dlang/dmd/pull/10796 With any luck this will be possible in the next release ;) Can this work using `stuff.classinfo.name` too ? This is the same as `typeid(stuff).name Currently no. It's represented differently in the end tree.
Re: DPP: Linker issue with functions implemented in C header files
On Tuesday, 18 February 2020 at 09:20:08 UTC, Andre Pany wrote: Hi Petar, Hi Andre, I'm happy to help :) thank you very much for the explanation and the code sample. Filling the az_span anonymous member was the tricky part, I thought it would be not possible to do so, but you showed me the trick. I wouldn't call it a trick, I was using standard struct literal initialization (the very syntax that DIP1031 proposes to deprecate). For example: struct Inner { int x, y; } struct Outer { Inner inner; } // You can initialize Outer in various ways: // 1) auto o1 = Outer(Inner(1, 2)); // 2) Outer o2 = { inner: Inner(1, 2) }; // 3) Outer o3 = { Inner(1, 2) }; // 4) Outer o4 = { inner: { x: 1, y: 2} }; // 5) Outer o5 = { { x: 1, y: 2} }; // 6) Outer o6; o6.inner.x = 1; o6.inner.y = 1; For POD (plain old data) struct like that, all six variants are equivalent (of course there more possible variations). Since there's no `private` protection modifier in C, the only thing C library authors can do is make it inconvenient to access struct fields (by prefixing them with underscores), but they can't really prevent it. For example, without this syntax, in pure C you can initialize a span like this: char my_string[] = "Hey"; az_span span; span._internal.ptr = my_string; span._internal.length = sizeof(my_string) - 1; span._internal.capacity = sizeof(my_string) - 1; And with almost the same syntax you can do this in D: string my_string = "Hey"; az_span span; span._internal.ptr = cast(ubyte*)my_string.ptr; // note: I think this should be safe, because of [1] span._internal.length = my_string.length; span._internal.capacity = my_string.length; It's just that that author wanted to prevent accidental bugs by pushing you to use the inline helper functions or macros (which are technically not needed). [1]: https://github.com/Azure/azure-sdk-for-c/blob/25f8a0228e5f250c02e389f19d88c064c93959c1/sdk/core/core/inc/az_span.h#L22 I will do it like you have proposed but had also already created a ticket for the Azure SDK developer: https://github.com/Azure/azure-sdk-for-c/issues/359 There should be a more convenient way to fill a az_span structure. To be honest, I don't think the authors will agree to change this, as putting inline functions in headers files is a pretty common practice in both C and C++. There are two benefits to that: 1) Potentially better performance, because the code is easier to inline 2) It's possible to provide header-only libraries (not the case here), that don't require build steps. For reference, here is my dockerfile which does the DPP call and linking: Cool, I'll check it later! ``` dockerfile FROM dlang2/ldc-ubuntu:1.20.0 as ldc RUN apt-get install -y git libssl-dev uuid-dev libcurl4-openssl-dev curl RUN curl -OL https://cmake.org/files/v3.12/cmake-3.12.4-Linux-x86_64.sh \ && mkdir /opt/cmake \ && sh /cmake-3.12.4-Linux-x86_64.sh --prefix=/opt/cmake --skip-license \ && ln -s /opt/cmake/bin/cmake /usr/local/bin/cmake RUN git clone https://github.com/Azure/azure-sdk-for-c.git \ && cd azure-sdk-for-c \ && git submodule update --init --recursive RUN cd azure-sdk-for-c \ && mkdir build \ && cd build \ && cmake ../ \ && make RUN apt-get install -y clang-9 libclang-9-dev RUN ln -s /usr/bin/clang-9 /usr/bin/clang COPY az_storage_blobs.dpp /tmp/ RUN DFLAGS="-L=-L/usr/lib/llvm-9/lib/" dub run dpp -- --help RUN DFLAGS="-L=-L/usr/lib/llvm-9/lib/" dub run dpp -- /tmp/az_storage_blobs.dpp \ --include-path /azure-sdk-for-c/sdk/core/core/inc \ --include-path /azure-sdk-for-c/sdk/core/core/internal \ --include-path /azure-sdk-for-c/sdk/storage/blobs/inc \ --include-path /azure-sdk-for-c/sdk/transport_policies/curl/inc \ --preprocess-only ADD blobs_client_example.d /tmp/blobs_client_example.d RUN ldc2 /tmp/blobs_client_example.d /tmp/az_storage_blobs.d \ /azure-sdk-for-c/build/sdk/core/core/libaz_core.a \ /azure-sdk-for-c/build/sdk/storage/blobs/libaz_storage_blobs.a \ /azure-sdk-for-c/build/sdk/transport_policies/curl/libaz_curl.a \ -of=/tmp/app ``` Kind regards André Cheers, Petar
Re: betterC CTFE nested switch
On Monday, 17 February 2020 at 19:02:50 UTC, Stefan Koch wrote: Sorry I just realized I never published the code. I am going to add it to ctfeutils. Thank you very much
Re: DPP: Linker issue with functions implemented in C header files
On Tuesday, 18 February 2020 at 08:32:47 UTC, Petar Kirov [ZombineDev] wrote: On Tuesday, 18 February 2020 at 05:41:38 UTC, Andre Pany wrote: Hi, I try to get wrap the "Azure SDK for C" using DPP and have following issue. Functions, which are actually implemented in C header files will cause linker errors: https://github.com/Azure/azure-sdk-for-c/blob/master/sdk/core/core/inc/az_span.h#L91 Example: AZ_NODISCARD AZ_INLINE az_span az_span_init(uint8_t * ptr, int32_t length, int32_t capacity) { return (az_span){ ._internal = { .ptr = ptr, .length = length, .capacity = capacity, }, }; } Error message: /tmp/app.o:az_storage_blobs.d:function _D20blobs_client_example__T19AZ_SPAN_FROM_BUFFERTG4096hZQBdFNbQnZS16az_storage_blobs7az_span: error: undefined reference to 'az_span_init' I do not know C well, is this the expected behavior and should I ask the Azure SDK developers to not implement functions within C header files? Kind regards André I think the problem is that you haven't actually linked in the Azure SDK C library. Dpp translates the header declarations from C to D, but the actual definitions (function bodies) are not part of the process. The executable code for the function definitions should be inside either a static or dynamic library provided by the SDK. From the the project's readme file, it looks like they're using CMake as the build system generator (afterwards both make and ninja should be valid choices for building): mkdir build cd build cmake ../ make In cases like this, it's best to checkout the CMakeLists.txt files of the individual sub project, like this one: https://github.com/Azure/azure-sdk-for-c/blob/master/sdk/core/core/CMakeLists.txt As you can see, there are several outputs of the build process, among which: - add_library(az_core ...) This defines a library named az_core which can produce either a static (.a on Linux, .lib on Windows) or dynamic library file (.so on Linux, .dll on Windows). (If no configuration is specified, I think it's static by default). So the final file name would be libaz_core.{a,so} on Linux. For the .c files to be built, a list of include directories must be specified, where the various .h would located (containing function and type declarations). This done like so: target_include_directories (az_core PUBLIC ...) The 'PUBLIC' argument to the target_include_directories specifies that if you want to use the library, you need to use the same include directories, as those needed for building it. - add_executable(az_core_test ..) This defines an executable build output, which looks is only used for testing, so it's not interesting to us, except that it can serve as an example app using the az_core library. --- So in summary, if you want to use the az_core library, you need to: 1. Build it 2. Run Dpp like so: d++ \ --include-path target_include_directories> You will need to repeat the same steps for any other part of the Azure C SDK. TL;DR After I went through all those steps I got a similar linker error for az_http_response_init. After looking where is the actual function definition, it turned out that it's not defined in a .c file, but it is an inline function part of a header file. Searching for az_span_init revealed the same (I could have saved myself some time by reading your message more carefully :D). So, to answer your original question, the problem is that dpp translates only declarations, not function definitions (such as inline functions like that). For now, your best course of action is to translate all inline function definition by hand. Since in C inline functions are mostly short and simple functions (a better alternative to macros), hopefully that won't be too much work. Also, looking at macros like AZ_SPAN_FROM_STR, there's really very little chance that they could be correctly translated automatically. As the things they do are likely not valid even in @system D code (without additional casts), so it's better to write your own D functions by hand anyway. Here's what I tried: test.dpp: #include #include import std.stdio; void main() { char[] resp = "HTTP/1.2 404 We removed the\tpage!\r\n" ~ "\r\n" ~ "But there is somebody. :-)".dup; az_span response_span = {{ ptr: cast(ubyte*)resp.ptr, length: cast(int)resp.length, capacity: cast(int)resp.length }}; az_http_response response; az_result result = az_http_response_init( &response, response_span); writeln(result); } d++ --compiler ldmd2 --include-path ./inc test.dpp ./build/libaz_core.a Hi Petar, thank you very much for the explanation and the code sample. Filling the az_span anonymous member was the tricky part, I thought it would be not possible to do so, but you showed me the trick. I will do it like you have proposed but had also already created a ticket for the Azure SDK developer: https://github.
Re: DPP: Linker issue with functions implemented in C header files
On Tuesday, 18 February 2020 at 05:41:38 UTC, Andre Pany wrote: Hi, I try to get wrap the "Azure SDK for C" using DPP and have following issue. Functions, which are actually implemented in C header files will cause linker errors: https://github.com/Azure/azure-sdk-for-c/blob/master/sdk/core/core/inc/az_span.h#L91 Example: AZ_NODISCARD AZ_INLINE az_span az_span_init(uint8_t * ptr, int32_t length, int32_t capacity) { return (az_span){ ._internal = { .ptr = ptr, .length = length, .capacity = capacity, }, }; } Error message: /tmp/app.o:az_storage_blobs.d:function _D20blobs_client_example__T19AZ_SPAN_FROM_BUFFERTG4096hZQBdFNbQnZS16az_storage_blobs7az_span: error: undefined reference to 'az_span_init' I do not know C well, is this the expected behavior and should I ask the Azure SDK developers to not implement functions within C header files? Kind regards André I think the problem is that you haven't actually linked in the Azure SDK C library. Dpp translates the header declarations from C to D, but the actual definitions (function bodies) are not part of the process. The executable code for the function definitions should be inside either a static or dynamic library provided by the SDK. From the the project's readme file, it looks like they're using CMake as the build system generator (afterwards both make and ninja should be valid choices for building): mkdir build cd build cmake ../ make In cases like this, it's best to checkout the CMakeLists.txt files of the individual sub project, like this one: https://github.com/Azure/azure-sdk-for-c/blob/master/sdk/core/core/CMakeLists.txt As you can see, there are several outputs of the build process, among which: - add_library(az_core ...) This defines a library named az_core which can produce either a static (.a on Linux, .lib on Windows) or dynamic library file (.so on Linux, .dll on Windows). (If no configuration is specified, I think it's static by default). So the final file name would be libaz_core.{a,so} on Linux. For the .c files to be built, a list of include directories must be specified, where the various .h would located (containing function and type declarations). This done like so: target_include_directories (az_core PUBLIC ...) The 'PUBLIC' argument to the target_include_directories specifies that if you want to use the library, you need to use the same include directories, as those needed for building it. - add_executable(az_core_test ..) This defines an executable build output, which looks is only used for testing, so it's not interesting to us, except that it can serve as an example app using the az_core library. --- So in summary, if you want to use the az_core library, you need to: 1. Build it 2. Run Dpp like so: d++ \ --include-path target_include_directories> You will need to repeat the same steps for any other part of the Azure C SDK. TL;DR After I went through all those steps I got a similar linker error for az_http_response_init. After looking where is the actual function definition, it turned out that it's not defined in a .c file, but it is an inline function part of a header file. Searching for az_span_init revealed the same (I could have saved myself some time by reading your message more carefully :D). So, to answer your original question, the problem is that dpp translates only declarations, not function definitions (such as inline functions like that). For now, your best course of action is to translate all inline function definition by hand. Since in C inline functions are mostly short and simple functions (a better alternative to macros), hopefully that won't be too much work. Also, looking at macros like AZ_SPAN_FROM_STR, there's really very little chance that they could be correctly translated automatically. As the things they do are likely not valid even in @system D code (without additional casts), so it's better to write your own D functions by hand anyway. Here's what I tried: test.dpp: #include #include import std.stdio; void main() { char[] resp = "HTTP/1.2 404 We removed the\tpage!\r\n" ~ "\r\n" ~ "But there is somebody. :-)".dup; az_span response_span = {{ ptr: cast(ubyte*)resp.ptr, length: cast(int)resp.length, capacity: cast(int)resp.length }}; az_http_response response; az_result result = az_http_response_init( &response, response_span); writeln(result); } d++ --compiler ldmd2 --include-path ./inc test.dpp ./build/libaz_core.a