Re: Passing a derived class where base class is defined as ref parameter

2021-12-14 Thread chopchop via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 20:58:21 UTC, Adam D Ruppe wrote: nice. You might want to look at my terminal.d (arsd-official:terminal on dub) too which has pretty comprehensive functionality for tons of things. Prolly more than you need looking at your interface tho but it includes the

Re: Passing a derived class where base class is defined as ref parameter

2021-12-14 Thread chopchop via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 17:27:13 UTC, Stanislav Blinov wrote: Simple. Don't take a `ref`. Just take a `Console`. Classes in D are reference types, you're not making a copy as you would in C++ if you were to write `updateFoodToken(Console c)`. Ah, ok, Reference types! That's great.

Re: Immutability and arrays

2021-12-14 Thread rumbu via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 16:21:03 UTC, Steven Schveighoffer wrote: On 12/14/21 11:19 AM, Steven Schveighoffer wrote: Er... scratch that, this isn't construction, it should use opAssign. Again, probably because memcpy+postblit is used by the runtime. If not reported, it should be.

Re: Why code failed to compile for foo2?

2021-12-14 Thread Tejas via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 15:14:40 UTC, Steven Schveighoffer wrote: On 12/14/21 12:04 AM, Tejas wrote: Is there anything wrong with the answer I posted? Can you please tell me if there's anything dissatisfactory about it? I feel like it does everything the OP wants. Also, am I wrong

Re: Why code failed to compile for foo2?

2021-12-14 Thread Tejas via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 13:25:04 UTC, apz28 wrote: On Tuesday, 14 December 2021 at 05:04:46 UTC, Tejas wrote: Is there anything wrong with the answer I posted? Can you please tell me if there's anything dissatisfactory about it? I feel like it does everything the OP wants. Also, am

Re: Passing a derived class where base class is defined as ref parameter

2021-12-14 Thread Adam D Ruppe via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 17:20:18 UTC, chopchop wrote: I am using the "ref" here (I put tinyurl to avoid over-referencing the post instead of the github page itself): https://tinyurl.com/bdddkmub yeah D classes are automatically ref unlike c++ so you don't need the second level of it

Re: template ctor overload Segmentation fault

2021-12-14 Thread vit via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 14:40:00 UTC, RazvanN wrote: On Sunday, 12 December 2021 at 11:57:43 UTC, vit wrote: Hello, why does this code fail to compile? ```d struct Foo(T){ this(Rhs, this This)(scope Rhs rhs){ } this(ref scope typeof(this) rhs){ } } struct Bar{

Re: Passing a derived class where base class is defined as ref parameter

2021-12-14 Thread Stanislav Blinov via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 17:20:18 UTC, chopchop wrote: I am using the "ref" here (I put tinyurl to avoid over-referencing the post instead of the github page itself): https://tinyurl.com/bdddkmub I would like to be able to pass any kind of console to updateFoodToken ( Console c ), ie

Re: Passing a derived class where base class is defined as ref parameter

2021-12-14 Thread chopchop via Digitalmars-d-learn
On Monday, 13 December 2021 at 22:30:59 UTC, Adam D Ruppe wrote: On Monday, 13 December 2021 at 22:06:45 UTC, chopchop wrote: If I remove the ref, it works as expected, that is to say I can give a derived class as parameter. Why are you using the ref to begin with? What the logic here?

Re: Passing a derived class where base class is defined as ref parameter

2021-12-14 Thread chopchop via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 05:38:17 UTC, Tejas wrote: On Monday, 13 December 2021 at 22:30:59 UTC, Adam D Ruppe wrote: On Monday, 13 December 2021 at 22:06:45 UTC, chopchop wrote: If I remove the ref, it works as expected, that is to say I can give a derived class as parameter. Why are

Re: Immutability and arrays

2021-12-14 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/14/21 11:19 AM, Steven Schveighoffer wrote: Er... scratch that, this isn't construction, it should use opAssign. Again, probably because memcpy+postblit is used by the runtime. If not reported, it should be. Simple proof that it is a bug: ```d immutable (ComplexStruct)[] arr;

Re: Immutability and arrays

2021-12-14 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/14/21 11:14 AM, Steven Schveighoffer wrote: On 12/14/21 10:53 AM, Stanislav Blinov wrote: Now, since we have copy ctors, slice assignment should, ostensibly, attempt to copy-assign elements (i.e. absent opAssign, try the copy ctor first). I agree slice-assign should work here with a

Re: Immutability and arrays

2021-12-14 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/14/21 10:53 AM, Stanislav Blinov wrote: Now, since we have copy ctors, slice assignment should, ostensibly, attempt to copy-assign elements (i.e. absent opAssign, try the copy ctor first). I agree slice-assign should work here with a copy ctor. What I think is happening is that it's

Re: Immutability and arrays

2021-12-14 Thread Stanislav Blinov via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 15:28:30 UTC, Steven Schveighoffer wrote: All the other problems you are having are deriving from this problem. Not exactly. One of the problems seems to be a genuine bug: ```d struct S { int[] x; // doesn't even participate here, neither would

Re: Immutability and arrays

2021-12-14 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/14/21 3:44 AM, rumbu wrote: I am trying to understand why in this two different cases (Simple and Complex), the compiler behaviour is different. ```d struct SimpleStruct { int x;} struct ComplexStruct { int[] x; } void main() {     SimpleStruct[] buf1;     immutable(SimpleStruct)[]

Re: Why code failed to compile for foo2?

2021-12-14 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/14/21 12:04 AM, Tejas wrote: Is there anything wrong with the answer I posted? Can you please tell me if there's anything dissatisfactory about it? I feel like it does everything the OP wants. Also, am I wrong in using `Unconst` over `Unqual`? Isn't `Unqual` overkill if you just want

Re: template ctor overload Segmentation fault

2021-12-14 Thread RazvanN via Digitalmars-d-learn
On Sunday, 12 December 2021 at 11:57:43 UTC, vit wrote: Hello, why does this code fail to compile? ```d struct Foo(T){ this(Rhs, this This)(scope Rhs rhs){ } this(ref scope typeof(this) rhs){ } } struct Bar{ Foo!int foo; } void main(){ } ``` error: Segmentation

Re: template ctor overload Segmentation fault

2021-12-14 Thread RazvanN via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 13:02:16 UTC, Tejas wrote: On Tuesday, 14 December 2021 at 12:04:36 UTC, RazvanN wrote: [...] Then why did my modification work? ```d struct Foo(T){ this(Rhs, this This)(scope Rhs rhs){ } this(scope Foo!(T)* rhs){ //replaced typeof(this) with

Re: Why code failed to compile for foo2?

2021-12-14 Thread apz28 via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 05:04:46 UTC, Tejas wrote: Is there anything wrong with the answer I posted? Can you please tell me if there's anything dissatisfactory about it? I feel like it does everything the OP wants. Also, am I wrong in using `Unconst` over `Unqual`? Isn't `Unqual`

Re: Immutability and arrays

2021-12-14 Thread rumbu via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 12:13:23 UTC, Stanislav Blinov wrote: Because is(typeof(immutable(ComplexStruct).x) == immutable(int[])). Can't bind an array of immutable to array of mutable. This would require a deep copy, i.e. copy constructor. This means that the only way to write a

Re: template ctor overload Segmentation fault

2021-12-14 Thread Tejas via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 12:04:36 UTC, RazvanN wrote: On Sunday, 12 December 2021 at 11:57:43 UTC, vit wrote: [...] The problem is that the compiler will try to generate an inout copy constructor for Bar that looks roughly like this: ``` this(ref scope inout(Bar) p) inout {

Re: Immutability and arrays

2021-12-14 Thread Stanislav Blinov via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 08:44:02 UTC, rumbu wrote: I am trying to understand why in this two different cases (Simple and Complex), the compiler behaviour is different. ```d struct SimpleStruct { int x;} struct ComplexStruct { int[] x; } void main() { SimpleStruct[] buf1;

Re: Immutability and arrays

2021-12-14 Thread WebFreak001 via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 08:44:02 UTC, rumbu wrote: I am trying to understand why in this two different cases (Simple and Complex), the compiler behaviour is different. ```d struct SimpleStruct { int x;} struct ComplexStruct { int[] x; } void main() { SimpleStruct[] buf1;

Re: template ctor overload Segmentation fault

2021-12-14 Thread RazvanN via Digitalmars-d-learn
On Sunday, 12 December 2021 at 11:57:43 UTC, vit wrote: Hello, why does this code fail to compile? ```d struct Foo(T){ this(Rhs, this This)(scope Rhs rhs){ } this(ref scope typeof(this) rhs){ } } struct Bar{ Foo!int foo; } void main(){ } ``` error: Segmentation

Re: A debug class has started

2021-12-14 Thread forkit via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 08:07:43 UTC, WebFreak001 wrote: The best way would be not doing this at all - when you manipulate strings/arrays in D you can do so by just assigning the elements like this: ```d immutable(char)[] replaceChar(char[] str, char ch1, char ch2) { for (ulong

Immutability and arrays

2021-12-14 Thread rumbu via Digitalmars-d-learn
I am trying to understand why in this two different cases (Simple and Complex), the compiler behaviour is different. ```d struct SimpleStruct { int x;} struct ComplexStruct { int[] x; } void main() { SimpleStruct[] buf1; immutable(SimpleStruct)[] ibuf1; buf1[0 .. 10] = ibuf1[0 ..

Re: How to define property type to Array!struct?

2021-12-14 Thread WebFreak001 via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 08:12:04 UTC, zoujiaqing wrote: My code: ```D module http.HttpRequest; import std.container; import std.array : Appender; struct HttpRequest { struct Header() { Appender!string name; Appender!string value; } string method;

How to define property type to Array!struct?

2021-12-14 Thread zoujiaqing via Digitalmars-d-learn
My code: ```D module http.HttpRequest; import std.container; import std.array : Appender; struct HttpRequest { struct Header() { Appender!string name; Appender!string value; } string method; string uri; int versionMajor = 0; int versionMinor = 0;

Re: A debug class has started

2021-12-14 Thread WebFreak001 via Digitalmars-d-learn
On Monday, 13 December 2021 at 22:43:14 UTC, forkit wrote: [...] //char* w = cast(char*)str; // nope. a pointer to a string constant is // (supposed to be) immutable, so expect undefined behaviour. note: //char* w = cast(char*)str.toStringz; // also