Re: Segmentation fault while reading a file

2024-08-01 Thread IchorDev via Digitalmars-d-learn
On Thursday, 1 August 2024 at 14:42:36 UTC, Ruby The Roobster wrote: Thank you. I am not very well acquainted with the standard library, and this cleans up things significantly. Question: Is there a good guide to Phobos anywhere? I would like to learn the more commonly used algorithms / con

Re: Any way to automatically convert structs on return?

2024-08-01 Thread IchorDev via Digitalmars-d-learn
On Thursday, 1 August 2024 at 14:20:29 UTC, user1234 wrote: That was a general criticism of implicit construction. We are only talking about it in the context of returning from a function. The classic example is ```d struct S {int i;} function f(S s); function f(int i); unittest { f(0); }

Re: Any way to automatically convert structs on return?

2024-08-01 Thread Emma via Digitalmars-d-learn
Thanks everyone for the replies! I just thought it was weird that implicit construction is seemingly supported when directly initialising a struct but not in any other case. I guess it's because it's clearly less ambiguous. On Thursday, 1 August 2024 at 13:07:09 UTC, Lance Bachmeier wrote: F

Re: Any way to automatically convert structs on return?

2024-08-01 Thread Nick Treleaven via Digitalmars-d-learn
On Thursday, 1 August 2024 at 08:46:00 UTC, IchorDev wrote: P.S. You might want to put `value = void`, otherwise it’ll always be default-constructed. Doing `= void` can violate the assumptions of a destructor of T. Nullable uses a union to store T, so it can decide when to call the destructor

Re: Segmentation fault while reading a file

2024-08-01 Thread Ruby The Roobster via Digitalmars-d-learn
On Thursday, 1 August 2024 at 07:03:04 UTC, IchorDev wrote: Hey just a heads up, you might wanna use [`readText`](https://dlang.org/library/std/file/read_text.html) and [`lineSplitter`](https://dlang.org/library/std/string/line_splitter.html) just so you don’t have to deal with file handles. .

Re: Any way to automatically convert structs on return?

2024-08-01 Thread user1234 via Digitalmars-d-learn
On Thursday, 1 August 2024 at 10:59:03 UTC, IchorDev wrote: On Thursday, 1 August 2024 at 09:55:08 UTC, user1234 wrote: The problem would be that sorting the candidates of an overload set would be more complicated. Also in certain cases it would be less obvious to get which one is selected. P

Re: Any way to automatically convert structs on return?

2024-08-01 Thread Lance Bachmeier via Digitalmars-d-learn
On Thursday, 1 August 2024 at 07:25:53 UTC, Emma wrote: but this doesn't: ```d Option!int something() { return None(); // Error: cannot implicitly convert expression `None()` of type `None` to `Option!int` } ``` For the program you've written, I'm happy it doesn't compile, because a lot

Re: Any way to automatically convert structs on return?

2024-08-01 Thread IchorDev via Digitalmars-d-learn
On Thursday, 1 August 2024 at 09:55:08 UTC, user1234 wrote: The problem would be that sorting the candidates of an overload set would be more complicated. Also in certain cases it would be less obvious to get which one is selected. Please elaborate about how this would interact with function

Re: Any way to automatically convert structs on return?

2024-08-01 Thread user1234 via Digitalmars-d-learn
On Thursday, 1 August 2024 at 08:46:00 UTC, IchorDev wrote: [...] I’m pretty sure this is intentional to prevent ambiguity, but I can’t quite remember what the point of that is. The problem would be that sorting the candidates of an overload set would be more complicated. Also in certain case

Re: Any way to automatically convert structs on return?

2024-08-01 Thread Dukc via Digitalmars-d-learn
Emma kirjoitti 1.8.2024 klo 10.25: This kind of prevents ergonomic code like the above. Instead you have to use a function like `Option!T None(T)() => Option!T()` and then you have to repeat yourself with `return None!int` and etc... it's quite annoying :( While this isn't exactly less verbos

Re: Any way to automatically convert structs on return?

2024-08-01 Thread IchorDev via Digitalmars-d-learn
On Thursday, 1 August 2024 at 07:25:53 UTC, Emma wrote: This code works: ```d struct None {} struct Option(T) { bool hasSome; T value; this(None) {} this(T v) { hasSome = true; value = v; } } Option!int a = 123;// automatically constructs an Option!in

Re: Any way to automatically convert structs on return?

2024-08-01 Thread Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
No, D does not support implicit construction. However in saying that, I will continue to argue in support of sum types when they get added to the language to support implicit construction!

Any way to automatically convert structs on return?

2024-08-01 Thread Emma via Digitalmars-d-learn
This code works: ```d struct None {} struct Option(T) { bool hasSome; T value; this(None) {} this(T v) { hasSome = true; value = v; } } Option!int a = 123;// automatically constructs an Option!int from a bare int Option!int b = None(); // same as abov

Re: Segmentation fault while reading a file

2024-08-01 Thread IchorDev via Digitalmars-d-learn
Hey just a heads up, you might wanna use [`readText`](https://dlang.org/library/std/file/read_text.html) and [`lineSplitter`](https://dlang.org/library/std/string/line_splitter.html) just so you don’t have to deal with file handles. Also you can use something like [`formattedRead`](https://dlang