Custom separator in array format
I want to format an array using the %(...%) syntax. How can I change the separator? I tried to use ? and add it as additional parameter, but that doesn't seem to work on arrays: import std; void main() { writeln("This works:"); writefln("%,2?d", '_', 2000); // 20_00 auto vec = [1000, 2000, 3000]; writeln("This should fail (separator character expected) but the ? is just ignored:"); writefln("%(%,2?d\t%)", vec); // 10,0020,00 30,00 writeln("This throws:"); writefln("%(%,2?d\t%)", '_', vec); // std.format.FormatException@/dlang/dmd/linux/bin64/../../src/phobos/std/format.d(2271): incompatible format character for integral argument: %( }
Re: iopipe: Writing output to std.io File
On Monday, 27 January 2020 at 18:12:40 UTC, Steven Schveighoffer wrote: Before I show you what to do, let me explain what the above actually does. 1. You constructed a buffer of characters. Good, this is the first step. 2. You used encodeText to convert the data to ubyte. Note that for char buffer, this basically is just a cast. Other encodings might do byteswapping. But you have done this step a bit early. At this point now, the window type is ubyte[]. You want to put data into the buffer as char[]. 3. You appended an outputPipe, which is a pass through while writing the data to a file (which is fed a stream of uninitialized data I think, so you probably got a file with garbage in it). Writing to this pipe's buffer is kind of pointless because the writing has already happened (pretty much all effects and actions performed on the buffer happen in the .extend function). What you need is to access the buffer for writing BEFORE encoding and streaming to the file. For this, you need the push [1] mechanism, which wraps up everything you want to happen to data you have written to the buffer into a lambda template: auto outputBuffered = bufd!char .push!(p => p .encodeText .outputPipe(output())); [1] http://schveiguy.github.io/iopipe/iopipe/valve/push.html I really feel like this is all very well thought out and clean, I don't appear to have a previous model to help visualize this output approach. Right now something like tee is coming to mind. Thank you for explaining with the answer.
Re: Static fields with shared fields
On 1/27/20 5:39 PM, Marcel wrote: Hello! If I declare a static variable inside a struct that contains a shared field, is that new variable also shared? For example, say I have a custom atomic wrapper type, like C++'s std::atomic, that contains only one shared variable: Does it remain shared when I instantiate one or do I have to rely on shared again (or __gshared)? I would say the type itself would have to be shared. You can (I think) declare shared types, which are automatically shared even without declaring the variable shared. At least, it used to be that way. Note that anything you declare shared can be shared. So even if you can't share the enclosing struct, you can share the member. -Steve
Re: Class member function with a Callback funtion parameter
On Monday, 27 January 2020 at 22:20:42 UTC, H. S. Teoh wrote: On Mon, Jan 27, 2020 at 10:13:47PM +, Herbert via Digitalmars-d-learn wrote: On Monday, 27 January 2020 at 21:51:35 UTC, Adam D. Ruppe wrote: > On Monday, 27 January 2020 at 21:21:55 UTC, Herbert wrote: > > My project does not allow dynamic memory. So I can't use > > delegates. > > delegates do not require dynamic memory. > > doesn't allocate any new memory (it just points > to the existing object) yet yields a delegate. As I understand the D language documentation says: delegates sometimes use dynamic memory. Only if you're using a lambda or a delegate that captures local variables. T Chapter "Memory Management": "D has built-in types that may be difficult to use without the GC: exceptions, strings, dynamic arrays, associative arrays, and delegate closures." I would like to do something like this: interface Timer { void delegate pCallbackFunction(void); this(); void Start(ushort milliseconds, pCallbackFunction); void Halt(); void Restart(); }
Static fields with shared fields
Hello! If I declare a static variable inside a struct that contains a shared field, is that new variable also shared? For example, say I have a custom atomic wrapper type, like C++'s std::atomic, that contains only one shared variable: Does it remain shared when I instantiate one or do I have to rely on shared again (or __gshared)?
Re: Class member function with a Callback funtion parameter
On Mon, Jan 27, 2020 at 10:13:47PM +, Herbert via Digitalmars-d-learn wrote: > On Monday, 27 January 2020 at 21:51:35 UTC, Adam D. Ruppe wrote: > > On Monday, 27 January 2020 at 21:21:55 UTC, Herbert wrote: > > > My project does not allow dynamic memory. So I can't use > > > delegates. > > > > delegates do not require dynamic memory. > > > > doesn't allocate any new memory (it just points to the > > existing object) yet yields a delegate. > > As I understand the D language documentation says: delegates sometimes > use dynamic memory. Only if you're using a lambda or a delegate that captures local variables. T -- Fact is stranger than fiction.
Re: Class member function with a Callback funtion parameter
On Monday, 27 January 2020 at 21:51:35 UTC, Adam D. Ruppe wrote: On Monday, 27 January 2020 at 21:21:55 UTC, Herbert wrote: My project does not allow dynamic memory. So I can't use delegates. delegates do not require dynamic memory. doesn't allocate any new memory (it just points to the existing object) yet yields a delegate. As I understand the D language documentation says: delegates sometimes use dynamic memory.
Re: Subrange type
On 1/27/20 1:15 PM, Herbert wrote: On Monday, 27 January 2020 at 20:15:33 UTC, Steven Schveighoffer wrote: On 1/27/20 3:06 PM, Herbert wrote: How can I create a subrange type, for example ushort DiceValue {1 ... 6}? D doesn't have a "Range" type like this. But you can use ranges of different types by typing the literals. Note that D numeric ranges are always exclusive at the upper end. e.g.: ushort(1) .. ushort(6+1) -Steve Thank you Steven! How can I have a function parameter with this type (DiceValue)? There is also iota() than generates a range: import std.stdio; import std.range; void foo(R)(R range) { pragma(msg, "Element type: ", ElementType!R); writefln!"Using as a range:\n%-(%s\n%)"(range); writeln("Using in a foreach loop:"); foreach (element; range) { writeln(element); } } void main() { auto range = iota(ushort(1), ushort(7)); foo(range); } The output: Using as a range: 1 2 3 4 5 6 Using in a foreach loop: 1 2 3 4 5 6 Ali
Re: Subrange type
On Mon, Jan 27, 2020 at 09:15:58PM +, Herbert via Digitalmars-d-learn wrote: [...] > How can I have a function parameter with this type (DiceValue)? Just take an integer type and add a contract that enforces range. For example: auto myFunc(int diceValue) in (diceValue >= 1 && diceValue <= 6) { // do stuff here } T -- Which is worse: ignorance or apathy? Who knows? Who cares? -- Erich Schubert
Re: Class member function with a Callback funtion parameter
On Monday, 27 January 2020 at 21:21:55 UTC, Herbert wrote: My project does not allow dynamic memory. So I can't use delegates. delegates do not require dynamic memory. doesn't allocate any new memory (it just points to the existing object) yet yields a delegate.
Class member function with a Callback funtion parameter
My project does not allow dynamic memory. So I can't use delegates. How can I declare a class member function with a callback function pointer?
Re: Subrange type
On Monday, 27 January 2020 at 20:15:33 UTC, Steven Schveighoffer wrote: On 1/27/20 3:06 PM, Herbert wrote: How can I create a subrange type, for example ushort DiceValue {1 ... 6}? D doesn't have a "Range" type like this. But you can use ranges of different types by typing the literals. Note that D numeric ranges are always exclusive at the upper end. e.g.: ushort(1) .. ushort(6+1) -Steve Thank you Steven! How can I have a function parameter with this type (DiceValue)?
Re: Subrange type
On Monday, 27 January 2020 at 20:06:14 UTC, Herbert wrote: How can I create a subrange type, for example ushort DiceValue {1 .. 6}? Probably the closest you can get is a struct with an invariant: import std.traits: isOrderingComparable; struct Subrange(T, T min, T max) if (isOrderingComparable!T) { private T value_ = min; invariant(min <= value && value <= max); this(T t) { value_ = t; } T value() { return value_; } alias value this; ref Subrage opAssign(T t) { value_ = t; return this; } }
Re: Subrange type
On 1/27/20 3:06 PM, Herbert wrote: How can I create a subrange type, for example ushort DiceValue {1 ... 6}? D doesn't have a "Range" type like this. But you can use ranges of different types by typing the literals. Note that D numeric ranges are always exclusive at the upper end. e.g.: ushort(1) .. ushort(6+1) -Steve
Re: Public constants and types in class
On 1/27/20 3:05 PM, Herbert wrote: How can a class provide it's users with named constants, enums, types, structs and not just member functions? Just declare them inside the class. They then go inside the class namespace, but are not strictly part of the class instance itself. e.g.: class C { enum Enum { one, two, three} struct S { int x; int y; } } C.Enum e = C.Enum.one; C.S s = C.S(1, 2); // can also access the namespace via an instance: C instance; instance.Enum e = instance.Enum.one; -Steve
Subrange type
How can I create a subrange type, for example ushort DiceValue {1 .. 6}?
Public constants and types in class
How can a class provide it's users with named constants, enums, types, structs and not just member functions?
Re: iopipe: Writing output to std.io File
On 1/27/20 1:12 PM, Steven Schveighoffer wrote: void writeln(Pipe)(ref Pipe sink, string text) { enforce(sink.ensureElems(text.size) >= text.length); // make sure there's enough buffer space to hold the text sink[0 .. text.length] = text; // write to the buffer sink.release(text.length); // release to be written } Ugh, should have tested. sink.window[0 .. text.length] = text; -Steve
Re: iopipe: Writing output to std.io File
On 1/26/20 11:59 PM, Jesse Phillips wrote: On Monday, 27 January 2020 at 01:50:00 UTC, Jesse Phillips wrote: Just as I'm hitting send the part I'm missing clicked: I needed to add the text encoding because my buffer is `char` but File writes `ubyte` ```dlang auto output() { return std.io.File("somefile.txt", mode!"w").refCounted; } auto outputBuffered = bufd!char .encodeText // Missing This part .outputPipe(output()); } ``` Unfortunately this did not write a text file as I expected. This is because output is buffered differently from input. With output you are writing to the buffer, then flushing the result as needed. But I constructed iopipe to be able to utilize all mechanisms as both input and output. Before I show you what to do, let me explain what the above actually does. 1. You constructed a buffer of characters. Good, this is the first step. 2. You used encodeText to convert the data to ubyte. Note that for char buffer, this basically is just a cast. Other encodings might do byteswapping. But you have done this step a bit early. At this point now, the window type is ubyte[]. You want to put data into the buffer as char[]. 3. You appended an outputPipe, which is a pass through while writing the data to a file (which is fed a stream of uninitialized data I think, so you probably got a file with garbage in it). Writing to this pipe's buffer is kind of pointless because the writing has already happened (pretty much all effects and actions performed on the buffer happen in the .extend function). What you need is to access the buffer for writing BEFORE encoding and streaming to the file. For this, you need the push [1] mechanism, which wraps up everything you want to happen to data you have written to the buffer into a lambda template: auto outputBuffered = bufd!char .push!(p => p .encodeText .outputPipe(output())); now, you write to the outputBuffered "buffer" as needed. releasing any data that is ready to go. As soon as it needs more buffer space and there is pending data, it will write the data, giving you back the buffer space. It also auto-flushes on the last reference destructor (this behavior is configurable). A simple writeln on such a pipe would look like: void writeln(Pipe)(ref Pipe sink, string text) { enforce(sink.ensureElems(text.size) >= text.length); // make sure there's enough buffer space to hold the text sink[0 .. text.length] = text; // write to the buffer sink.release(text.length); // release to be written } I really need to do an article on iopipe. I've been neglecting that... -Steve [1] http://schveiguy.github.io/iopipe/iopipe/valve/push.html
Re: bindbc-opengl: Now drawing triangle
On Saturday, 25 January 2020 at 21:33:09 UTC, JN wrote: I assume it's working now? Yup it works. For future, learn to use RenderDoc: https://renderdoc.org/ it allows you to debug your OpenGL application and see what kind of data is sent by your app. Wow that's what I need. Thanks for sharing this.
Re: How to convert this C++ code to Dlang? Please send me Dlang version of this C++ code
On Monday, 27 January 2020 at 13:41:13 UTC, Ferhat Kurtulmuş wrote: On Monday, 27 January 2020 at 13:24:03 UTC, Marcone wrote: On Monday, 27 January 2020 at 13:07:20 UTC, rumbu wrote: [...] Thank you very much! But I have this error when I try to compile: Error 42: Symbol Undefined _InitCommonControls@0 Error: linker exited with status 1 I am using Resedit.exe for create dialog resource and compiled .rc to .res for link. probably, you need to link your exec with comctl32.lib Very good!! Working fine adding: pragma(lib, "comctl32.lib");
Re: How to convert this C++ code to Dlang? Please send me Dlang version of this C++ code
On Monday, 27 January 2020 at 13:24:03 UTC, Marcone wrote: On Monday, 27 January 2020 at 13:07:20 UTC, rumbu wrote: [...] Thank you very much! But I have this error when I try to compile: Error 42: Symbol Undefined _InitCommonControls@0 Error: linker exited with status 1 I am using Resedit.exe for create dialog resource and compiled .rc to .res for link. probably, you need to link your exec with comctl32.lib
Re: How to convert this C++ code to Dlang? Please send me Dlang version of this C++ code
On Monday, 27 January 2020 at 13:07:20 UTC, rumbu wrote: On Monday, 27 January 2020 at 11:34:47 UTC, Marcone wrote: [...] Translated (including errors & comments): import core.sys.windows.windows; import core.sys.windows.commctrl; import std.stdio; HINSTANCE hInst; extern(Windows): BOOL DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) nothrow { switch(uMsg) { case WM_INITDIALOG: { } return TRUE; case WM_CLOSE: { EndDialog(hwndDlg, 0); } return TRUE; case WM_COMMAND: { switch(LOWORD(wParam)) { //case BT_1: //{ //writeln("Ola Mundo!"); //} } } return TRUE; } return FALSE; } int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { hInst=hInstance; InitCommonControls(); return DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), NULL, ); } Thank you very much! But I have this error when I try to compile: Error 42: Symbol Undefined _InitCommonControls@0 Error: linker exited with status 1 I am using Resedit.exe for create dialog resource and compiled .rc to .res for link.
Re: How to convert this C++ code to Dlang? Please send me Dlang version of this C++ code
On Monday, 27 January 2020 at 11:34:47 UTC, Marcone wrote: #include #include #include #include "resource.h" #include HINSTANCE hInst; BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg) { case WM_INITDIALOG: { } return TRUE; case WM_CLOSE: { EndDialog(hwndDlg, 0); } return TRUE; case WM_COMMAND: { switch(LOWORD(wParam)) { //case BT_1: //{ //std::cout << "Ola Mundo!\n"; //} } } return TRUE; } return FALSE; } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { hInst=hInstance; InitCommonControls(); return DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)DlgMain); } Translated (including errors & comments): import core.sys.windows.windows; import core.sys.windows.commctrl; import std.stdio; HINSTANCE hInst; extern(Windows): BOOL DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) nothrow { switch(uMsg) { case WM_INITDIALOG: { } return TRUE; case WM_CLOSE: { EndDialog(hwndDlg, 0); } return TRUE; case WM_COMMAND: { switch(LOWORD(wParam)) { //case BT_1: //{ //writeln("Ola Mundo!"); //} } } return TRUE; } return FALSE; } int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { hInst=hInstance; InitCommonControls(); return DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), NULL, ); }
How to convert this C++ code to Dlang? Please send me Dlang version of this C++ code
#include #include #include #include "resource.h" #include HINSTANCE hInst; BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg) { case WM_INITDIALOG: { } return TRUE; case WM_CLOSE: { EndDialog(hwndDlg, 0); } return TRUE; case WM_COMMAND: { switch(LOWORD(wParam)) { //case BT_1: //{ //std::cout << "Ola Mundo!\n"; //} } } return TRUE; } return FALSE; } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { hInst=hInstance; InitCommonControls(); return DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)DlgMain); }