Re: Custom separator in array format
On Tuesday, 28 January 2020 at 07:36:25 UTC, Malte wrote: 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: %( } I think I see why it's not working. Essentially, for each element of vec, format is called with only that element as an argument. Essentially, rather than: foreach (e; vec) writef("%,2?d\t", '_', e); writeln(); You get: foreach (e; vec) writef("%,2?d\t", e); writeln(); For whatever reason, it doesn't throw when missing an argument for the separator - I'd say this is a bug (https://issues.dlang.org/show_bug.cgi?id=20541). For now, you can work around the issue this way: import std.stdio : writefln; import std.format : format; import std.algorithm : map; auto vec = [1000, 2000, 3000]; writefln("%-(%s\t%)", vec.map!(e => format!"%,2?d"('_', e))); -- Simen
Re: Custom separator in array format
On Tuesday, 28 January 2020 at 08:54:16 UTC, Simen Kjærås wrote: import std.stdio : writefln; import std.format : format; import std.algorithm : map; auto vec = [1000, 2000, 3000]; writefln("%-(%s\t%)", vec.map!(e => format!"%,2?d"('_', e))); That helps, thank you very much.
Re: Subrange type
On Monday, 27 January 2020 at 22:05:57 UTC, Ali Çehreli wrote: 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: [...] 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 wow, pragma, import ... only to declare a subrange type, something so simple and natural I could use it in many functions as a parameter type with one single simple declaration. It's a pitty.
Re: Subrange type
On 1/28/20 1:55 AM, Herbert wrote: > wow, pragma, pragma was to indicate the type of elements at compile time. > import ... Yes, iota is a Phobos feature, part of a module. > only to declare a subrange type, something so simple and natural Some languages think so. > I could use it in many functions as a parameter type with one single > simple declaration. Sorry for jumping in the conversation like that but no, it's not a subrange type. > It's a pitty. My apologies... Ali
Blog Post #0100: SFX - Button Interactions II
Today marks the 100th post on the GtkDcoding blog where we continue with Button interactions. You can find it here: https://gtkdcoding.com/2020/01/28/0100-sfx-button-interactions-ii-color-font-shape.html After today, I'll be on hiatus from the blog and posts may be sporadic.
Re: iopipe: Writing output to std.io File
On 1/28/20 1:25 AM, Jesse Phillips wrote: 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. Thanks! Tee is actually a pretty close approximation. For example, if you wanted to save to 2 files, you could do: push!(p => p .encodeText .outputPipe(file1) .outputPipe(file2) ) The whole advantage is that you don't need to use the output pipes as buffered output. For example, the convert function doesn't need them, it just pulls the whole thing (using the process function). Everything is pulled with iopipe, even output, so it's just a matter of who is pulling and when. Pushing is a matter of telling the other end to pull. -Steve
Question about alias and getOverloads
Hello! I have a question about `alias` template parameter and getOverloads. For instance I have some code like this: // - import std; import core.thread; void foo(string param1) {} void foo(string param1, int param2) {} template Bar(alias Func) { // Next line is not valid now. This is my understanding of how I wish it would work... pragma(msg, __traits(getOverloads, Func)); } void main() { Bar!foo; } //--- By the spec: https://dlang.org/spec/traits.html#getOverloads The signature of `getOverloads` trait requires me to pass a symbol where my function is contained (it could be class, struct or module). And a name of function that I want to get overloads for. Let's imagine that `foo` overloads and Bar template are in different modules. And `Bar` template doesn't have no direct access to module where `foo` is contained. And `Bar` is instantiated outside module where `Bar` is declared. I have two questions: 1. Whether `Bar` instantiation would get first, second or both overloads of function? Or maybe I would get two instantiations of template by the number of overloads? I don't understand well how this mechanic is working. Is it documented somewhere? Does actually one alias parameter represents only one symbol from overload set or all of them? 2. If alias parameter represents multiple symbols from overload set. Then I wish I could access to list of overloads having alias parameter. I wish to get count of overloads in my real case in order to issue an error that overloaded are not supported by my code.
Re: Question about alias and getOverloads
On Tuesday, 28 January 2020 at 19:26:03 UTC, uranuz wrote: Hello! I have a question about `alias` template parameter and getOverloads. For instance I have some code like this: // - import std; import core.thread; void foo(string param1) {} void foo(string param1, int param2) {} template Bar(alias Func) { // Next line is not valid now. This is my understanding of how I wish it would work... pragma(msg, __traits(getOverloads, Func)); } __traits(getOverloads, __traits(parent, Func), __traits(identifier, Func))
Re: Question about alias and getOverloads
Thanks for advice ;) This looks like some `standard trick` that is yet not learnt or forgoten by me personally. The key was in using `parent` trait. This is what I failed to think of. This is working as expected: //- import std; import core.thread; import std.meta: AliasSeq; void foo(string param1) {} void foo(string param1, int param2, bool param3) {} template Bar(alias Func) { alias Bar = __traits(getOverloads, __traits(parent, Func), __traits(identifier, Func)); } void main() { import std.traits: fullyQualifiedName, Parameters; import std.conv: text; static foreach( It; Bar!foo ) { pragma(msg, fullyQualifiedName!It ~ "/" ~ (Parameters!It).length.text); } } //- Output: onlineapp.foo/1 onlineapp.foo/3 //- So as far as I understand alias template parameter gives access to all of items of overload set as I was expecting. The only problem is that I didn't found something about it in specification about templates: https://dlang.org/spec/template.html
Re: Question about alias and getOverloads
On Tuesday, 28 January 2020 at 19:59:15 UTC, uranuz wrote: So as far as I understand alias template parameter gives access to all of items of overload set as I was expecting. The only problem is that I didn't found something about it in specification about templates: https://dlang.org/spec/template.html It's documented under "Alias Declaration": Aliases can also import a set of overloaded functions, that can be overloaded with functions in the current scope: https://dlang.org/spec/declaration.html#alias
Re: Looking for a Simple Doubly Linked List Implementation
your linked list seems very complex
Re: Question about alias and getOverloads
I have read it two or three times just before writing my question: https://dlang.org/spec/declaration.html#alias And also a have read all the dlang docs several time few years ago... ;) But I don't see what do you you mean by writing that it was menioned here. I don't se any words or any examples that describe how `alias` works with function overload sets. Is it a blind assumption that it was written here? The only example that someone can mix up and consider as answer to my question if actually didn't read it with attention. It is example bellow: ``` alias myint = int; void foo(int x) { ... } void foo(myint m) { ... } // error, multiply defined function foo ``` But the only thing that this example illustrates is that `myint` and `int` is actualy the same type. So this overload set is just incorrect. I don't ask you to consider the case where overload set is incorrect. So this didn't answered my question in any way. Looks like this is some kind of nasty details that language developers don't "proud of" and don't like to discover details about how it's working. Or just lazy enough to do so ;) Still I figured answer myself using your example. The answer is not full, but yet enough for me for practical usage. But the lack of description about it makes me think that I am not the last man who will have his curiosity not fully satisfied about this aspect of language. And also __traits(parent, Func) seems like not universal for all cases with functions, because it fails on lambda-functions. For instance: // import std; template Bar(alias Func) { alias Bar = __traits(getOverloads, __traits(parent, Func), __traits(identifier, Func)); } void main() { alias Test = Bar!(() { return `test`; }); } // Output: onlineapp.d(4): Error: no property __lambda1 for type void onlineapp.d(4): Error: main().__lambda1 cannot be resolved onlineapp.d(9): Error: template instance onlineapp.Bar!(function () pure nothrow @nogc @safe => "test") error instantiating // Seems that it fails to get parent for lambda. I don't know why? What parent should be for labmda? Thanks for attention!
How to convert "string" to const(wchar)* ?
How to convert "string" to const(wchar)* ? The code bellow is making confuse strange characters. cast(wchar*) str
Re: How to convert "string" to const(wchar)* ?
On Wednesday, 29 January 2020 at 05:17:03 UTC, Marcone wrote: How to convert "string" to const(wchar)* ? The code bellow is making confuse strange characters. cast(wchar*) str this seems working: string s = "test ğüişçöıı"; wstring wstr = s.to!wstring; const(wchar)* str = wstr.ptr; writeln(str[0..wstr.length]); // do not try to print pointer const(wchar)* // use slicing
Re: How to convert "string" to const(wchar)* ?
On Tuesday, January 28, 2020 10:17:03 PM MST Marcone via Digitalmars-d-learn wrote: > How to convert "string" to const(wchar)* ? > The code bellow is making confuse strange characters. > > cast(wchar*) str Of course it is. string is immutable(char)[], and the characters are in UTF-8. immutable(wchar)[] would would be UTF-16. Even casting between those two types would result in nonsense, because UTF-8 and UTF-16 are different encodings. Casting between array or pointer types basically causes one type to be interpreted as the other. It doesn't convert the underlying data in any fashion. Also, strings aren't null-terminated in D, so having a pointer to a random string could result in a buffer overflow when you try to iterate through the string via pointer as is typical in C code. D code just uses the length property of the string. I assume that with const(wchar)*, you want it to be a null-terminated string of const(wchar). For that, what you basically need is a const(wchar)[] with a null terminator, and then you need to get a pointer to its first character. So, if you were to do that yourself, you'd end up with something like wstring wstr = to!wstring(str) ~ '\0'; const(wchar)* cwstr = wstr.ptr; or more likely auto = to!wstring(str) ~ '\0'; auto cwstr = wstr.ptr; The function in the standard library for simplifying that is toUTF16z: https://dlang.org/phobos/std_utf.html#toUTF16z Then you can just do auto cwstr = str.toUTF16z(); However, if you're doing this to pass a null-terminated string of UTF-16 characters to a C program (e.g. to the Windows API), be aware that if that function stores that pointer anywhere, you will need to also store it in your D code, because toUTF16z allocates a dynamic array to hold the string that you're getting a pointer to, and if a C function holds on to that pointer, the D GC won't see that it's doing that. And if the D GC doesn't see any references to that array anywhere, it will likely collect that memory. As long as you're passing it to a C function that just operates on the memory and returns, it's not a problem, but it can definitely be a problem if the C function stores that pointer even after the function has returned. Keeping a pointer to that memory in your D code fixes that problem, because then the D GC can see that that memory is still referenced and thus should not be collected. - Jonathan M Davis
Re: How to convert "string" to const(wchar)* ?
On Wednesday, 29 January 2020 at 06:53:15 UTC, Jonathan M Davis wrote: On Tuesday, January 28, 2020 10:17:03 PM MST Marcone via Digitalmars-d-learn wrote: [...] Of course it is. string is immutable(char)[], and the characters are in UTF-8. immutable(wchar)[] would would be UTF-16. Even casting between those two types would result in nonsense, because UTF-8 and UTF-16 are different encodings. Casting between array or pointer types basically causes one type to be interpreted as the other. It doesn't convert the underlying data in any fashion. Also, strings aren't null-terminated in D, so having a pointer to a random string could result in a buffer overflow when you try to iterate through the string via pointer as is typical in C code. D code just uses the length property of the string. [...] + Just a reminder that string literals are null-terminated.