Re: Why allow initializers of non-static members that allocate?

2022-06-10 Thread Salih Dincer via Digitalmars-d-learn
On Saturday, 11 June 2022 at 02:22:28 UTC, matheus wrote: Well would this be annoying? Yes, mainly if I already know this, but if not, then it would be like a nice to know warning for newbies like myself. By the way this would be only in the cases that a static array field being initialized w

Re: UI Library

2022-06-10 Thread Marcone via Digitalmars-d-learn
On Friday, 20 May 2022 at 02:37:48 UTC, harakim wrote: I need to write a piece of software to track and categorize some purchases. It's the kind of thing I could probably write in a couple of hours in C#/Java + html/css/javascript. However, something keeps drawing me to D and as this is a simpl

Re: a struct as an multidimensional array index

2022-06-10 Thread Chris Katko via Digitalmars-d-learn
On Friday, 10 June 2022 at 17:26:48 UTC, Ali Çehreli wrote: On 6/10/22 08:13, z wrote: > arrays of arrays has different order for declaration and addressing, > and declaring array of arrays has different order depending on how you > declare it and wether it's static or dynamic array, *oof*) > >

Re: UI Library

2022-06-10 Thread Marcone via Digitalmars-d-learn
On Friday, 20 May 2022 at 02:37:48 UTC, harakim wrote: I need to write a piece of software to track and categorize some purchases. It's the kind of thing I could probably write in a couple of hours in C#/Java + html/css/javascript. However, something keeps drawing me to D and as this is a simpl

Re: Run a command-line process with data sent, and retrieve the data.

2022-06-10 Thread Christopher Katko via Digitalmars-d-learn
On Friday, 10 June 2022 at 17:49:20 UTC, Ali Çehreli wrote: On 6/10/22 10:40, Ali Çehreli wrote: >https://dlang.org/library/std/process/pipe_process.html I realized you may be happier with the following if all you need is stdout: https://dlang.org/library/std/process/execute.html htt

Re: Why allow initializers of non-static members that allocate?

2022-06-10 Thread matheus via Digitalmars-d-learn
On Saturday, 11 June 2022 at 01:52:58 UTC, Mike Parker wrote: ... That's because static arrays are allocated as part of the instance: ... Yes I understood the problem, but the naive me was thinking that in this example: struct S{ int[] arr = new int[](5); } For some reason this would

Re: Why allow initializers of non-static members that allocate?

2022-06-10 Thread Mike Parker via Digitalmars-d-learn
On Saturday, 11 June 2022 at 01:14:06 UTC, matheus wrote: So, in the case of "int[] arr = new int[](5)", an array of length 5 of type int will be instantiated and its address will be shared among whoever instantiates "S" and be pointed and accessed through arr. In the second case, "int[2] ar

Re: UI Library

2022-06-10 Thread harakim via Digitalmars-d-learn
On Friday, 20 May 2022 at 12:07:46 UTC, Adam D Ruppe wrote: On Friday, 20 May 2022 at 03:47:14 UTC, harakim wrote: Thank you. I will definitely give that a try. But just ask me if something comes up since I can push fixes to master p quickly. I have been using the minigui library and it's wor

Re: Why allow initializers of non-static members that allocate?

2022-06-10 Thread matheus via Digitalmars-d-learn
On Friday, 10 June 2022 at 07:49:43 UTC, Mike Parker wrote: ... And it *is* documented: Struct fields are by default initialized to whatever the Initializer for the field is, and if none is supplied, to the default initializer for the field's type. The default initializers are evaluated at co

Re: Why allow initializers of non-static members that allocate?

2022-06-10 Thread Bastiaan Veelo via Digitalmars-d-learn
On Friday, 10 June 2022 at 14:56:24 UTC, Steven Schveighoffer wrote: On 6/10/22 3:46 AM, Mike Parker wrote: I think this is a case where having a warning that's on by default, and which can be explicitly disabled, is useful. "Blah blah .init blah blah. See link-to-something-in-docs. Is this wh

Re: map! evaluates twice

2022-06-10 Thread Ali Çehreli via Digitalmars-d-learn
On 6/10/22 13:47, Steven Schveighoffer wrote: > `map` calls the lambda for each call to `front`. If you want a cached > version, use `cache`: Others don't know but as I will likely show during a lightning talk at DConf, I am trying to finish a .cached range algorithm that caches all elements t

Re: map! evaluates twice

2022-06-10 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/10/22 4:33 PM, Antonio wrote: When mapping and filtering, the last mapped element is evaluated twice... Is it the expected behaviour? ```d void main() {     import std.algorithm, std.stdio;     [1,2,3,4,5].     map!((x){     writeln("mapping ", x);     return x;

map! evaluates twice

2022-06-10 Thread Antonio via Digitalmars-d-learn
When mapping and filtering, the last mapped element is evaluated twice... Is it the expected behaviour? ```d void main() { import std.algorithm, std.stdio; [1,2,3,4,5]. map!((x){ writeln("mapping ", x); return x; }).

Re: 'each' can take static arrays

2022-06-10 Thread mw via Digitalmars-d-learn
On Friday, 10 June 2022 at 17:27:13 UTC, Adam D Ruppe wrote: On Friday, 10 June 2022 at 16:59:04 UTC, Ali Çehreli wrote: Why the inconsistency? Phobos has dozens of random special cases throughout. I'd prefer if these were all removed, but right now there's just some functions that special c

Re: Range to Nullable conversion

2022-06-10 Thread Antonio via Digitalmars-d-learn
On Friday, 10 June 2022 at 18:00:20 UTC, Paul Backus wrote: On Friday, 10 June 2022 at 17:22:53 UTC, Antonio wrote: Can this code be written as a **simple** expression? (without having to write helper methods). ```d import std.range, std.typecons; Nullable!(ElementType!R) maybeFront(R)(auto

Re: Range to Nullable conversion

2022-06-10 Thread Antonio via Digitalmars-d-learn
On Friday, 10 June 2022 at 17:37:13 UTC, Ali Çehreli wrote: On 6/10/22 10:22, Antonio wrote: > Is there any alternative to ***range front*** that returns a Nullable > (i.e. **frontAsMonad** or **frontAsNullable**)? import std; // Spelling? :) auto nullablelize(R)(R range) { ... } void main()

Re: Why allow initializers of non-static members that allocate?

2022-06-10 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/10/22 1:20 PM, Mike Parker wrote: On Friday, 10 June 2022 at 14:56:24 UTC, Steven Schveighoffer wrote: Discovered circa 2009: https://issues.dlang.org/show_bug.cgi?id=2947 It should be illegal to declare a field this way that has mutable references without being `shared`. End of story.

Re: Range to Nullable conversion

2022-06-10 Thread Paul Backus via Digitalmars-d-learn
On Friday, 10 June 2022 at 17:22:53 UTC, Antonio wrote: Can this code be written as a **simple** expression? (without having to write helper methods). ```d import std.range, std.typecons; Nullable!(ElementType!R) maybeFront(R)(auto ref R r) if (isInputRange!R) { if (r.empty) r

Re: Run a command-line process with data sent, and retrieve the data.

2022-06-10 Thread Ali Çehreli via Digitalmars-d-learn
On 6/10/22 10:40, Ali Çehreli wrote: >https://dlang.org/library/std/process/pipe_process.html I realized you may be happier with the following if all you need is stdout: https://dlang.org/library/std/process/execute.html https://dlang.org/library/std/process/execute_shell.html Ali

Re: Run a command-line process with data sent, and retrieve the data.

2022-06-10 Thread Ali Çehreli via Digitalmars-d-learn
On 6/10/22 10:37, Chris Katko wrote: > I want to pipe in string data to a shell/commandline program, then > retrieve the output. But the documentation I read appears to only show > usage for 'Files' for stdin/stdout/stderr. > > ala something like this: > D > string input = "hello\nworld"; > st

Re: Run a command-line process with data sent, and retrieve the data.

2022-06-10 Thread Chris Katko via Digitalmars-d-learn
On Friday, 10 June 2022 at 17:37:30 UTC, Chris Katko wrote: I want to pipe in string data to a shell/commandline program, then retrieve the output. But the documentation I read appears to only show usage for 'Files' for stdin/stdout/stderr. ala something like this: D string input = "hello\

Re: Range to Nullable conversion

2022-06-10 Thread Ali Çehreli via Digitalmars-d-learn
On 6/10/22 10:22, Antonio wrote: > Is there any alternative to ***range front*** that returns a Nullable > (i.e. **frontAsMonad** or **frontAsNullable**)? import std; // Spelling? :) auto nullablelize(R)(R range) { alias E = Nullable!(ElementType!R); struct Nullablelize { enum empty = f

Run a command-line process with data sent, and retrieve the data.

2022-06-10 Thread Chris Katko via Digitalmars-d-learn
I want to pipe in string data to a shell/commandline program, then retrieve the output. But the documentation I read appears to only show usage for 'Files' for stdin/stdout/stderr. ala something like this: D string input = "hello\nworld"; string output; runProcess("grep hello", input, outpu

Re: a struct as an multidimensional array index

2022-06-10 Thread Ali Çehreli via Digitalmars-d-learn
On 6/10/22 08:13, z wrote: > arrays of arrays has different order for declaration and addressing, > and declaring array of arrays has different order depending on how you > declare it and wether it's static or dynamic array, *oof*) > > To give you an idea of the situation : > ```D > int[3][1

Re: 'each' can take static arrays

2022-06-10 Thread Adam D Ruppe via Digitalmars-d-learn
On Friday, 10 June 2022 at 16:59:04 UTC, Ali Çehreli wrote: Why the inconsistency? Phobos has dozens of random special cases throughout. I'd prefer if these were all removed, but right now there's just some functions that special case to allow it and others that don't. Apparently each is on

Range to Nullable conversion

2022-06-10 Thread Antonio via Digitalmars-d-learn
Is there any alternative to ***range front*** that returns a Nullable (i.e. **frontAsMonad** or **frontAsNullable**)? I'm using Vibe.d and Nullable is the standard way to return an optional element: ```d @safe Nullable!Item getItem(int _id) { import std.algorithm : filter; with (

Re: Why allow initializers of non-static members that allocate?

2022-06-10 Thread Mike Parker via Digitalmars-d-learn
On Friday, 10 June 2022 at 14:56:24 UTC, Steven Schveighoffer wrote: Discovered circa 2009: https://issues.dlang.org/show_bug.cgi?id=2947 It should be illegal to declare a field this way that has mutable references without being `shared`. End of story. -Steve The docs do say that: The d

Re: a struct as an multidimensional array index

2022-06-10 Thread Ali Çehreli via Digitalmars-d-learn
On 6/10/22 08:01, Ali Çehreli wrote: > I still don't understand the reason though. The rows would be copied > without ref but should retain their type as bool[3], a static array. (?) Ok, now I see the very sinister problem: It is a disaster to combine static array lambda parameters with the laz

'each' can take static arrays

2022-06-10 Thread Ali Çehreli via Digitalmars-d-learn
I know static arrays are not ranges but somehow they work with 'each'. With map, I need to slice as 'arr[]': import std; void main() { int[3] arr; arr.each!(e => e);// Compiles // arr.map!(e => e); // Fails to compile arr[].map!(e => e); // Compiles } Why the inconsistency? Al

Re: a struct as an multidimensional array index

2022-06-10 Thread z via Digitalmars-d-learn
On Friday, 10 June 2022 at 08:08:45 UTC, Chris Katko wrote: Is it somehow possible to use a struct as a [multidimensional] array index: D struct indexedPair { size_t x, y; } bool isMapPassable[100][100]; auto p = indexedPair(50, 50); if(isMapPassable[p]) return true; Probably not,

Re: a struct as an multidimensional array index

2022-06-10 Thread Ali Çehreli via Digitalmars-d-learn
On 6/10/22 07:38, Ali Çehreli wrote: > I played with that toString function but for some reason it prints all > Ts. (?) Fixed it by changing one of the lambdas to take by reference: void toString(scope void delegate(in char[]) sink) const { import std.algorithm; sink.formattedWrite!"%

Re: Why allow initializers of non-static members that allocate?

2022-06-10 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/10/22 3:46 AM, Mike Parker wrote: On Friday, 10 June 2022 at 07:35:17 UTC, Bastiaan Veelo wrote: Is there a use case where this makes sense? I would have much appreciated the compiler slapping me on the fingers, but it doesn't. I understand that it is safe and that the compiler can allow

Re: a struct as an multidimensional array index

2022-06-10 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jun 10, 2022 at 08:08:45AM +, Chris Katko via Digitalmars-d-learn wrote: > Is it somehow possible to use a struct as a [multidimensional] array index: > > D > > struct indexedPair > { > size_t x, y; > } > > bool isMapPassable[100][100]; > auto p = indexedPair(50, 50); > > if(is

Re: a struct as an multidimensional array index

2022-06-10 Thread Ali Çehreli via Digitalmars-d-learn
On 6/10/22 01:08, Chris Katko wrote: > Is it somehow possible to use a struct as a [multidimensional] array index: You can define an opIndex that takes any index type if the array is defined by you: import std.stdio; import std.format; struct indexedPair { size_t x, y; } struct MyArray {

Re: Why allow initializers of non-static members that allocate?

2022-06-10 Thread Arafel via Digitalmars-d-learn
On 10/6/22 14:58, Salih Dincer wrote: On Friday, 10 June 2022 at 07:35:17 UTC, Bastiaan Veelo wrote: I have been foolish enough to make a mistake like this: ```d struct S {     int[] arr = new int[](5); } ``` Well, if the b's may not be equal, there's a simple solution. But why are a's like t

Re: Why allow initializers of non-static members that allocate?

2022-06-10 Thread Salih Dincer via Digitalmars-d-learn
On Friday, 10 June 2022 at 07:35:17 UTC, Bastiaan Veelo wrote: I have been foolish enough to make a mistake like this: ```d struct S { int[] arr = new int[](5); } ``` Well, if the b's may not be equal, there's a simple solution. But why are a's like that, they're not static! ```d void m

How To Teach Children The Importance of Thankfulness

2022-06-10 Thread Vikesh Sharma via Digitalmars-d-learn
Most parents have instilled in their children the value of saying thank you by the time they reach elementary school, if not earlier. But how many have actually taught kids how to be grateful? Just Visit justtutors Blog For Complete Information

Re: Why allow initializers of non-static members that allocate?

2022-06-10 Thread Bastiaan Veelo via Digitalmars-d-learn
On Friday, 10 June 2022 at 07:49:43 UTC, Mike Parker wrote: And it *is* documented: Struct fields are by default initialized to whatever the Initializer for the field is, and if none is supplied, to the default initializer for the field's type. The default initializers are evaluated at compil

a struct as an multidimensional array index

2022-06-10 Thread Chris Katko via Digitalmars-d-learn
Is it somehow possible to use a struct as a [multidimensional] array index: D struct indexedPair { size_t x, y; } bool isMapPassable[100][100]; auto p = indexedPair(50, 50); if(isMapPassable[p]) return true; Probably not, but I'm curious.

Re: Why allow initializers of non-static members that allocate?

2022-06-10 Thread Bastiaan Veelo via Digitalmars-d-learn
On Friday, 10 June 2022 at 07:46:36 UTC, Mike Parker wrote: On Friday, 10 June 2022 at 07:35:17 UTC, Bastiaan Veelo wrote: Is there a use case where this makes sense? I would have much appreciated the compiler slapping me on the fingers, but it doesn't. I understand that it is safe and that th

Re: Why allow initializers of non-static members that allocate?

2022-06-10 Thread Mike Parker via Digitalmars-d-learn
On Friday, 10 June 2022 at 07:46:36 UTC, Mike Parker wrote: I think this is a case where having a warning that's on by default, and which can be explicitly disabled, is useful. "Blah blah .init blah blah. See link-to-something-in-docs. Is this what you intended?" And it *is* documented: S

Re: Why allow initializers of non-static members that allocate?

2022-06-10 Thread Mike Parker via Digitalmars-d-learn
On Friday, 10 June 2022 at 07:35:17 UTC, Bastiaan Veelo wrote: Is there a use case where this makes sense? I would have much appreciated the compiler slapping me on the fingers, but it doesn't. I understand that it is safe and that the compiler can allow this, but why would anyone want that? D

Why allow initializers of non-static members that allocate?

2022-06-10 Thread Bastiaan Veelo via Digitalmars-d-learn
I have been foolish enough to make a mistake like this: ```d struct S { int[] arr = new int[](5); } ``` This is terrible because ```d S s1; S s2; s2.arr[0] = 42; writeln(s1.arr[0]); // 42 Gotcha! ``` Of course there are less obvious variants of the same mistake: ```d import std; struct S {