Re: Operator "+=" overloading for class?

2023-12-20 Thread Ki Rill via Digitalmars-d-learn
On Wednesday, 20 December 2023 at 02:56:24 UTC, Steven Schveighoffer wrote: Instead you are trying to reassign the `this` reference, which is a local (and also forbidden). Think about it a second, your `this + rhs` is going to allocate a *new* object. Then if the assignment to the local `this`

Re: Operator "+=" overloading for class?

2023-12-19 Thread Steven Schveighoffer via Digitalmars-d-learn
auto opOpAssign(string op)(Value rhs) { this = this + rhs; return this; } // Error: `this` is not an lvalue and cannot be modified ``` Assigning an object is like copying a pointer. You may think you can try overloading the assignment, but it is [forbidden](https:/

Re: Operator "+=" overloading for class?

2023-12-18 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Sunday, 17 December 2023 at 04:13:20 UTC, Ki Rill wrote: I am trying to overload `opOpAssign` for my class. The code compiles, but it does not seem to be working. ```d // binary operations have already been implemented for Value // i need +=, -=, *=, /= auto opOpAssign(string op)(Value rhs)

Re: Operator "+=" overloading for class?

2023-12-18 Thread Ki Rill via Digitalmars-d-learn
On Monday, 18 December 2023 at 04:49:53 UTC, novice2 wrote: On Monday, 18 December 2023 at 03:39:16 UTC, Ki Rill wrote: On Sunday, 17 December 2023 at 07:05:12 UTC, Adam D. Ruppe wrote: On Sunday, 17 December 2023 at 04:13:20 UTC, Ki Rill wrote: [...] check what `op` is. pretty sure it is "+

Re: Operator "+=" overloading for class?

2023-12-17 Thread novice2 via Digitalmars-d-learn
On Monday, 18 December 2023 at 03:39:16 UTC, Ki Rill wrote: On Sunday, 17 December 2023 at 07:05:12 UTC, Adam D. Ruppe wrote: On Sunday, 17 December 2023 at 04:13:20 UTC, Ki Rill wrote: [...] check what `op` is. pretty sure it is "+" not "+=" so your element isnt' saved anywhere. also a bit

Re: Operator "+=" overloading for class?

2023-12-17 Thread Ki Rill via Digitalmars-d-learn
On Sunday, 17 December 2023 at 07:05:12 UTC, Adam D. Ruppe wrote: On Sunday, 17 December 2023 at 04:13:20 UTC, Ki Rill wrote: [...] check what `op` is. pretty sure it is "+" not "+=" so your element isnt' saved anywhere. also a bit iffy there isn't a member here to work on Yes, op is '+'.

Re: Operator "+=" overloading for class?

2023-12-16 Thread Adam D. Ruppe via Digitalmars-d-learn
On Sunday, 17 December 2023 at 04:13:20 UTC, Ki Rill wrote: auto opOpAssign(string op)(in ElementType rhs) { mixin("return this" ~ op ~ "rhs;"); } ``` check what `op` is. pretty sure it is "+" not "+=" so your element isnt' saved anywhere. also a bit iffy there isn't a member here to work

Re: Operator "+=" overloading for class?

2023-12-16 Thread Ki Rill via Digitalmars-d-learn
On Sunday, 17 December 2023 at 04:15:02 UTC, Ki Rill wrote: On Sunday, 17 December 2023 at 04:13:20 UTC, Ki Rill wrote: I am trying to overload `opOpAssign` for my class. The code [...] I forgot to mention, it is relevant to [`Value`](https://github.com/rillki/tiny-grad/blob/main/source/rk/

Re: Operator "+=" overloading for class?

2023-12-16 Thread Ki Rill via Digitalmars-d-learn
On Sunday, 17 December 2023 at 04:13:20 UTC, Ki Rill wrote: I am trying to overload `opOpAssign` for my class. The code [...] I forgot to mention, it is relevant to [`Value`](https://github.com/rillki/tiny-grad/blob/main/source/rk/tgrad/core/value.d) class only.

Operator "+=" overloading for class?

2023-12-16 Thread Ki Rill via Digitalmars-d-learn
I am trying to overload `opOpAssign` for my class. The code compiles, but it does not seem to be working. ```d // binary operations have already been implemented for Value // i need +=, -=, *=, /= auto opOpAssign(string op)(Value rhs) { mixin("return this" ~ op ~ "rhs;"); } auto opOpAssign(

Re: Function Overloading

2023-11-03 Thread Gaurav Negi via Digitalmars-d-learn
c.add(2);    c.writeln; // x + 2 +        // with constructor:    c = Calculate("x"); c.add(2);    c.writeln; // x + 2 + } ``` There is a simple struct like the one above that is not related to reality. There can be more than 2 function overloading in it, but in our example there

Re: Function Overloading

2023-11-02 Thread Basile B. via Digitalmars-d-learn
On Thursday, 2 November 2023 at 11:17:42 UTC, Salih Dincer wrote: On Wednesday, 1 November 2023 at 20:04:52 UTC, Basile B. wrote: Yes. D constructors are not named but the current implementation adds a name that is `__ctor`, so add ```d alias add = __ctor; ``` to you struct. Yeah, it wor

Re: Function Overloading

2023-11-02 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 1 November 2023 at 20:04:52 UTC, Basile B. wrote: Yes. D constructors are not named but the current implementation adds a name that is `__ctor`, so add ```d alias add = __ctor; ``` to you struct. Yeah, it works! Thanks...:) SDB@79

Re: Function Overloading

2023-11-01 Thread Basile B. via Digitalmars-d-learn
On Tuesday, 31 October 2023 at 20:09:44 UTC, Salih Dincer wrote: [...] is it possible to do something like `alias add = this;`? ```d struct Calculate {   int memory; string result;    auto toString() => result;    // alias add = this;     import std.string : format;    this(stri

Function Overloading

2023-10-31 Thread Salih Dincer via Digitalmars-d-learn
 c = Calculate("x"); c.add(2);    c.writeln; // x + 2 + } ``` There is a simple struct like the one above that is not related to reality. There can be more than 2 function overloading in it, but in our example there are only 2. Without implementing a similar function again for each c

Re: overloading main

2022-10-31 Thread Imperatorn via Digitalmars-d-learn
On Sunday, 30 October 2022 at 23:43:03 UTC, NonNull wrote: On Sunday, 30 October 2022 at 18:24:22 UTC, Adam D Ruppe wrote: [...] Ah, makes sense to limit the possible low level error messages with separate compilation because of the linker not knowing D signatures. Thanks for the intuition.

Re: overloading main

2022-10-30 Thread NonNull via Digitalmars-d-learn
On Sunday, 30 October 2022 at 18:24:22 UTC, Adam D Ruppe wrote: it prolly just to keep newbs from making confusing mistakes and getting weird behavior at startup. If there's two mains, which one is the expected entry? Perhaps it could just be the one that matches the permitted signature, and if

Re: overloading main

2022-10-30 Thread NonNull via Digitalmars-d-learn
On Sunday, 30 October 2022 at 17:41:07 UTC, Imperatorn wrote: On Sunday, 30 October 2022 at 17:29:25 UTC, NonNull wrote: On Sunday, 30 October 2022 at 16:31:45 UTC, Imperatorn wrote: You should not have multiple mains. Rename it and call it Doesn't answer my questions. I wasn't asking for pra

Re: overloading main

2022-10-30 Thread Adam D Ruppe via Digitalmars-d-learn
On Sunday, 30 October 2022 at 16:09:54 UTC, NonNull wrote: I am linking to a C project with some C already automatically translated into D including the C main function `int main(int argc, char** argv){/* ... */}` which I wanted to call from a D main function in a new module. did you put exte

Re: overloading main

2022-10-30 Thread Imperatorn via Digitalmars-d-learn
On Sunday, 30 October 2022 at 17:29:25 UTC, NonNull wrote: On Sunday, 30 October 2022 at 16:31:45 UTC, Imperatorn wrote: You should not have multiple mains. Rename it and call it Doesn't answer my questions. I wasn't asking for practical, moral or esthetic advice. Try to phrase your questio

Re: overloading main

2022-10-30 Thread NonNull via Digitalmars-d-learn
On Sunday, 30 October 2022 at 16:31:45 UTC, Imperatorn wrote: You should not have multiple mains. Rename it and call it Doesn't answer my questions. I wasn't asking for practical, moral or esthetic advice.

Re: overloading main

2022-10-30 Thread Imperatorn via Digitalmars-d-learn
On Sunday, 30 October 2022 at 16:09:54 UTC, NonNull wrote: I am linking to a C project with some C already automatically translated into D including the C main function `int main(int argc, char** argv){/* ... */}` which I wanted to call from a D main function in a new module. But then I found t

overloading main

2022-10-30 Thread NonNull via Digitalmars-d-learn
I am linking to a C project with some C already automatically translated into D including the C main function `int main(int argc, char** argv){/* ... */}` which I wanted to call from a D main function in a new module. But then I found that the former C main in D will not compile! ldc2 complains

Re: nested function overloading

2022-06-23 Thread Chris Katko via Digitalmars-d-learn
On Wednesday, 22 June 2022 at 12:42:48 UTC, Steven Schveighoffer wrote: On 6/22/22 2:05 AM, monkyyy wrote: On Monday, 20 June 2022 at 13:20:51 UTC, Steven Schveighoffer wrote: And you can also use an inner struct to define overloaded functions. I believe templates make a better bandaid ```d v

Re: nested function overloading

2022-06-22 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/22/22 2:05 AM, monkyyy wrote: On Monday, 20 June 2022 at 13:20:51 UTC, Steven Schveighoffer wrote: And you can also use an inner struct to define overloaded functions. I believe templates make a better bandaid ```d void main(){ template bar(){     void bar_(int){}     void

Re: nested function overloading

2022-06-21 Thread monkyyy via Digitalmars-d-learn
On Monday, 20 June 2022 at 13:20:51 UTC, Steven Schveighoffer wrote: And you can also use an inner struct to define overloaded functions. I believe templates make a better bandaid ```d void main(){ template bar(){ void bar_(int){} void bar_(float){}

Re: nested function overloading

2022-06-20 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/17/22 8:09 AM, Chris Katko wrote: I don't need this functionality, but I wanted to be sure. Does function overloading not work with nested functions? I got a compiler error (something like "function already defined") when I tried it. Correct, it's not allowed. Howev

Re: nested function overloading

2022-06-17 Thread bauss via Digitalmars-d-learn
On Friday, 17 June 2022 at 13:04:47 UTC, Chris Katko wrote: On Friday, 17 June 2022 at 12:19:33 UTC, bauss wrote: On Friday, 17 June 2022 at 12:09:33 UTC, Chris Katko wrote: I don't need this functionality, but I wanted to be sure. Does function overloading not work with nested functio

Re: nested function overloading

2022-06-17 Thread Chris Katko via Digitalmars-d-learn
On Friday, 17 June 2022 at 12:19:33 UTC, bauss wrote: On Friday, 17 June 2022 at 12:09:33 UTC, Chris Katko wrote: I don't need this functionality, but I wanted to be sure. Does function overloading not work with nested functions? I got a compiler error (something like "functi

Re: nested function overloading

2022-06-17 Thread bauss via Digitalmars-d-learn
On Friday, 17 June 2022 at 12:09:33 UTC, Chris Katko wrote: I don't need this functionality, but I wanted to be sure. Does function overloading not work with nested functions? I got a compiler error (something like "function already defined") when I tried it. According t

nested function overloading

2022-06-17 Thread Chris Katko via Digitalmars-d-learn
I don't need this functionality, but I wanted to be sure. Does function overloading not work with nested functions? I got a compiler error (something like "function already defined") when I tried it.

Re: Function prototype overloading does not work ?

2022-01-19 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/19/22 3:47 AM, Enjoys Math wrote: ``` module expr; import dots; import operator; import equation; import var; import var_expr; import zz_const; class Expr { public:    void opBinary(string op)(string s) const    {   static if (op == "+")   { Expr right = null;  

Re: Function prototype overloading does not work ?

2022-01-19 Thread vit via Digitalmars-d-learn
On Wednesday, 19 January 2022 at 08:47:27 UTC, Enjoys Math wrote: ``` module expr; import dots; import operator; import equation; import var; import var_expr; import zz_const; class Expr { public: void opBinary(string op)(string s) const { static if (op == "+") { Expr

Re: Function prototype overloading does not work ?

2022-01-19 Thread Hipreme via Digitalmars-d-learn
On Wednesday, 19 January 2022 at 08:47:27 UTC, Enjoys Math wrote: ``` module expr; import dots; import operator; import equation; import var; import var_expr; import zz_const; class Expr { public: void opBinary(string op)(string s) const { static if (op == "+") { Expr

Function prototype overloading does not work ?

2022-01-19 Thread Enjoys Math via Digitalmars-d-learn
``` module expr; import dots; import operator; import equation; import var; import var_expr; import zz_const; class Expr { public: void opBinary(string op)(string s) const { static if (op == "+") { Expr right = null; if (s == ".." || s == "..." || s == ""

Re: Operator Overloading for Enum

2021-02-08 Thread DolphinChips via Digitalmars-d-learn
On Monday, 8 February 2021 at 15:56:24 UTC, Michael Brown wrote: Hi all, Is it possible to operator overload on enums? I'd like to do a opCmp() Kind regards, Mike You can create custom struct type with opCmp() and create enum with that type. Example: https://run.dlang.io/is/m7DN66

Operator Overloading for Enum

2021-02-08 Thread Michael Brown via Digitalmars-d-learn
Hi all, Is it possible to operator overload on enums? I'd like to do a opCmp() Kind regards, Mike

Re: Operator Overloading for Enum

2021-02-08 Thread Paul Backus via Digitalmars-d-learn
On Monday, 8 February 2021 at 15:56:24 UTC, Michael Brown wrote: Hi all, Is it possible to operator overload on enums? I'd like to do a opCmp() Kind regards, Mike No, it isn't. Only structs and classes can have overloaded operators.

Re: Easy way to accept X and immutable X in parameters without overloading?

2021-01-11 Thread Paul Backus via Digitalmars-d-learn
On Monday, 11 January 2021 at 18:51:04 UTC, Jack wrote: alias Callback = void function(const C, int); void main() { auto l = SList!Callback(); auto a = (C c, int d) { }; auto b = (C c, int d) { }; auto c = (const C c, int d) { }; l.insert(a); l.insert(b); l.insert(c);

Re: Easy way to accept X and immutable X in parameters without overloading?

2021-01-11 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 11 January 2021 at 18:51:04 UTC, Jack wrote: Here's what I'm trying to make to work: import std.container : SList; class C { static immutable Foo = new C(); // } alias Callback = void function(const C, int); void main() { auto l = SList!Callback(); auto a = (C c

Re: Easy way to accept X and immutable X in parameters without overloading?

2021-01-11 Thread Jack via Digitalmars-d-learn
On Monday, 11 January 2021 at 18:37:58 UTC, ag0aep6g wrote: On Monday, 11 January 2021 at 18:12:17 UTC, Jack wrote: thanks! now, how would I add const here? import std.container : SList; auto l = SList!Callabck(); doesn't work: auto l = SList!(const(Callabck())); auto l = SList!(const Callabc

Re: Easy way to accept X and immutable X in parameters without overloading?

2021-01-11 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 11 January 2021 at 18:12:17 UTC, Jack wrote: thanks! now, how would I add const here? import std.container : SList; auto l = SList!Callabck(); doesn't work: auto l = SList!(const(Callabck())); auto l = SList!(const Callabck()); You said you want the callbacks to accept both mutabl

Re: Easy way to accept X and immutable X in parameters without overloading?

2021-01-11 Thread Jack via Digitalmars-d-learn
On Monday, 11 January 2021 at 16:56:05 UTC, ag0aep6g wrote: On Monday, 11 January 2021 at 16:40:01 UTC, Jack wrote: let's say a I have this: void f(X foo) { } but I'd like to make f() accept immutable X too so instead of cast away everywhere in the code where immutable(X) is passed to f() or

Re: Easy way to accept X and immutable X in parameters without overloading?

2021-01-11 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 11 January 2021 at 16:40:01 UTC, Jack wrote: let's say a I have this: void f(X foo) { } but I'd like to make f() accept immutable X too so instead of cast away everywhere in the code where immutable(X) is passed to f() or make a overload for this, are there any way to accept both

Easy way to accept X and immutable X in parameters without overloading?

2021-01-11 Thread Jack via Digitalmars-d-learn
let's say a I have this: void f(X foo) { } but I'd like to make f() accept immutable X too so instead of cast away everywhere in the code where immutable(X) is passed to f() or make a overload for this, are there any way to accept both in same function? those function are callback-like functi

Re: opCast / operator overloading with additional template arguments

2021-01-11 Thread Ali Çehreli via Digitalmars-d-learn
On 1/10/21 7:27 PM, Paul wrote: > On Monday, 11 January 2021 at 02:37:24 UTC, Ali Çehreli wrote: >> >> T opCast(T)() const if (is(T : Vec!(size, S2), S2)) { > >> The is expression can be so complicated that I used a different >> approach below. > >> if (isInstanceOfVec!T && >> T.init.

Re: opCast / operator overloading with additional template arguments

2021-01-10 Thread Paul Backus via Digitalmars-d-learn
On Monday, 11 January 2021 at 03:40:41 UTC, Paul wrote: On Monday, 11 January 2021 at 00:48:49 UTC, Steven Schveighoffer wrote: I would think though, that this should work: T opCast(T : Vec!(vecsize, S), S)() Oh wouw, this seems to work perfectly! Awesome thanks ^^ Any Idea why T opCast(T,

Re: opCast / operator overloading with additional template arguments

2021-01-10 Thread Paul via Digitalmars-d-learn
On Monday, 11 January 2021 at 00:48:49 UTC, Steven Schveighoffer wrote: I would think though, that this should work: T opCast(T : Vec!(vecsize, S), S)() Oh wouw, this seems to work perfectly! Awesome thanks ^^ Any Idea why T opCast(T, S)() const if (is(T : Vec!(grootte, S))) { yields the er

Re: opCast / operator overloading with additional template arguments

2021-01-10 Thread Paul via Digitalmars-d-learn
On Monday, 11 January 2021 at 02:37:24 UTC, Ali Çehreli wrote: >> T opCast(T)() const if (is(T : Vec!(size, S2), S2)) { The is expression can be so complicated that I used a different approach below. if (isInstanceOfVec!T && T.init.content.length == size) { // ADDED: ali

Re: opCast / operator overloading with additional template arguments

2021-01-10 Thread Ali Çehreli via Digitalmars-d-learn
On 1/10/21 6:37 PM, Ali Çehreli wrote: >// OBSERVATION: Should the cast below be S? >converted.content[i] = cast(S2) content[i]; I take that back. Yes, it should be S2. (I've been off lately. :) ) Ali

Re: opCast / operator overloading with additional template arguments

2021-01-10 Thread Ali Çehreli via Digitalmars-d-learn
On 1/10/21 5:09 PM, Paul wrote: > I'll paste more of my file, I hope that's ok. Not only ok but much appreciated. :) >> T opCast(T)() const if (is(T : Vec!(size, S2), S2)) { The is expression can be so complicated that I used a different approach below. I left notes in capital letters bel

Re: opCast / operator overloading with additional template arguments

2021-01-10 Thread Paul via Digitalmars-d-learn
On Monday, 11 January 2021 at 00:25:36 UTC, Ali Çehreli wrote: You don't show complete code; so, I hope I came up with something that reflects your case. Thank you, sadly S (S2 now) is not any specific type, sorry I'll paste more of my file, I hope that's ok. (Sidenote, I'm not sure it's the

Re: opCast / operator overloading with additional template arguments

2021-01-10 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/10/21 7:09 PM, Paul wrote: Is there a way to have additional template arguments in operator overloads? The problem I'm having is mostly caused by casting a templated struct to other templated structs. I had the following code; T opCast(T)() const if (is(T : Vec!(vecsize, S), S)) { T

Re: opCast / operator overloading with additional template arguments

2021-01-10 Thread Ali Çehreli via Digitalmars-d-learn
On 1/10/21 4:09 PM, Paul wrote: > Is there a way to have additional template arguments in operator overloads? I haven't tried that but the following method seems to work for you. You don't show complete code; so, I hope I came up with something that reflects your case. import std; struct

opCast / operator overloading with additional template arguments

2021-01-10 Thread Paul via Digitalmars-d-learn
Is there a way to have additional template arguments in operator overloads? The problem I'm having is mostly caused by casting a templated struct to other templated structs. I had the following code; T opCast(T)() const if (is(T : Vec!(vecsize, S), S)) { T converted; static for

Re: Using multiple mixin templates to implement operator overloading

2020-12-12 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 12 December 2020 at 20:25:48 UTC, Adam D. Ruppe wrote: On Saturday, 12 December 2020 at 18:14:31 UTC, Paul Backus wrote: IMO this is one of the stupider design decisions in D, but it's unlikely it will ever be fixed. It is useful in several other contexts though, including user o

Re: Using multiple mixin templates to implement operator overloading

2020-12-12 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 12 December 2020 at 20:26:00 UTC, Dennis wrote: If issue 19365 got fixed eeek I thought that was fixed but apparently not :( so yeah alias won't work for operator overloads. Does work for other functions so good technique to know but not here. So for op prolly go with the strin

Re: Using multiple mixin templates to implement operator overloading

2020-12-12 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 12 December 2020 at 18:14:31 UTC, Paul Backus wrote: IMO this is one of the stupider design decisions in D, but it's unlikely it will ever be fixed. It is useful in several other contexts though, including user overriding and private data stores for the mixin. The easiest workar

Re: Using multiple mixin templates to implement operator overloading

2020-12-12 Thread Dennis via Digitalmars-d-learn
On Saturday, 12 December 2020 at 18:14:31 UTC, Paul Backus wrote: IMO this is one of the stupider design decisions in D, but it's unlikely it will ever be fixed. The easiest workaround is to use string mixins instead, which work the way you'd expect them to. If issue 19365 got fixed, it could

Re: Using multiple mixin templates to implement operator overloading

2020-12-12 Thread Tobias Pankrath via Digitalmars-d-learn
On Saturday, 12 December 2020 at 18:14:31 UTC, Paul Backus wrote: Functions from different mixin templates can't overload each other. The reason for this is that, when you mix in a mixin template, it does not *actually* add the declarations inside it to a current scope: instead, it adds them to

Re: Using multiple mixin templates to implement operator overloading

2020-12-12 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 12 December 2020 at 17:36:57 UTC, Tobias Pankrath wrote: I want to wrap e.g. an int and implement basic arithmetic. In the provided example [1] I use two mixin templates to separately implement scaling (multiplication with int/double) and addition and subtraction with the type its

Using multiple mixin templates to implement operator overloading

2020-12-12 Thread Tobias Pankrath via Digitalmars-d-learn
I want to wrap e.g. an int and implement basic arithmetic. In the provided example [1] I use two mixin templates to separately implement scaling (multiplication with int/double) and addition and subtraction with the type itself. In the end I want to have several distinct wrappers and allow s

Re: Wich: opIndex overloading by return type

2020-04-19 Thread Steven Schveighoffer via Digitalmars-d-learn
ave a geomentry object and in one case I get x0,y0,x1,y1 and in the other case x,y,w,h IMO that would make a lot of sense. D doesn't do overloading based on return type. You can do some things like return an item that alias this'd to one of the two, and then use an accessor for the

Re: Wich: opIndex overloading by return type

2020-04-19 Thread unDEFER via Digitalmars-d-learn
It is easy. You can make both types as children of common parent class myT, and when return myT.

Wich: opIndex overloading by return type

2020-04-19 Thread Robert M. Münch via Digitalmars-d-learn
Wouldn't it make a lot of sense to allow different opIndex implementations based on return type? class myC { myT1 opIndex(int x) myT2 opIndex(int x) } Depending on the types involved myC[1] woudl return myT1 or myT2. Use-case: I have a geomentry object and in one case I get x0,

Re: `This` reference not accessible when overloading an operator?

2020-01-21 Thread Mathias Lang via Digitalmars-d-learn
On Tuesday, 21 January 2020 at 20:48:44 UTC, Enjoys Math wrote: I have an Integer class in integer.d. A RationalNumber class in rational_number.d, and they each import each other (so that could be the issue). However, this is not working: Symbol opBinary(string op : "/")(const Integer z)

Re: `This` reference not accessible when overloading an operator?

2020-01-21 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/21/20 3:48 PM, Enjoys Math wrote: I have an Integer class in integer.d.  A RationalNumber class in rational_number.d, and they each import each other (so that could be the issue).  However, this is not working:    Symbol opBinary(string op : "/")(const Integer z) const    {   retu

`This` reference not accessible when overloading an operator?

2020-01-21 Thread Enjoys Math via Digitalmars-d-learn
I have an Integer class in integer.d. A RationalNumber class in rational_number.d, and they each import each other (so that could be the issue). However, this is not working: Symbol opBinary(string op : "/")(const Integer z) const { return new RationalNumber(this, z); } Getti

Re: Template mixin + operator overloading question

2019-10-11 Thread Boyan Lazov via Digitalmars-d-learn
On Friday, 11 October 2019 at 13:13:46 UTC, Dennis wrote: On Friday, 11 October 2019 at 12:45:59 UTC, Boyan Lazov wrote: Any ideas what I'm doing wrong? Nothing, it's a bug. https://issues.dlang.org/show_bug.cgi?id=19476 Alright, I see Well, the alias workaround works, so that seems just as

Re: Template mixin + operator overloading question

2019-10-11 Thread Dennis via Digitalmars-d-learn
On Friday, 11 October 2019 at 12:45:59 UTC, Boyan Lazov wrote: Any ideas what I'm doing wrong? Nothing, it's a bug. https://issues.dlang.org/show_bug.cgi?id=19476

Template mixin + operator overloading question

2019-10-11 Thread Boyan Lazov via Digitalmars-d-learn
Hello, I seem to have a problem when I use a template mixin and then try to overload operators both in the mixin and in a struct from where it's instantiated. So the idea is that I have a few operators defined in the mixin, then a few more in the struct, and I want to forward all operations n

Re: How to do operator overloading for <, >, <=, >=, !=, and == between struct and int?

2019-04-21 Thread Ferhat Kurtulmuş via Digitalmars-d-learn
On Sunday, 21 April 2019 at 18:17:00 UTC, Adam D. Ruppe wrote: On Sunday, 21 April 2019 at 18:07:08 UTC, Ferhat Kurtulmuş wrote: I am writing an opencv binding and need something like: Mat m = another_mat > 5; D does not support that. The comparison operators are always just true or false (as

Re: How to do operator overloading for <, >, <=, >=, !=, and == between struct and int?

2019-04-21 Thread Adam D. Ruppe via Digitalmars-d-learn
On Sunday, 21 April 2019 at 18:07:08 UTC, Ferhat Kurtulmuş wrote: I am writing an opencv binding and need something like: Mat m = another_mat > 5; D does not support that. The comparison operators are always just true or false (as determined by the int opCmp or the bool opEquals returns), the

How to do operator overloading for <, >, <=, >=, !=, and == between struct and int?

2019-04-21 Thread Ferhat Kurtulmuş via Digitalmars-d-learn
I am writing an opencv binding and need something like: Mat m = another_mat > 5; Docs does not cover this sitiuation: https://dlang.org/spec/operatoroverloading.html#compare opBinary does not support those operators, and Section "Overloading <, <=, >, and >=" descr

Re: Operator Overloading with multiple return types

2019-03-15 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Mar 15, 2019 at 04:29:22PM -0700, Ali Çehreli via Digitalmars-d-learn wrote: > On 03/15/2019 03:48 PM, H. S. Teoh wrote: [...] > > Ali's example was unfortunately deceptively formatted. > > My editor did that. :) This is why I don't trust auto-formatters. ;-) > On my work computer, I'v

Re: Operator Overloading with multiple return types

2019-03-15 Thread Ali Çehreli via Digitalmars-d-learn
On 03/15/2019 03:48 PM, H. S. Teoh wrote: On Fri, Mar 15, 2019 at 10:30:41PM +, eXodiquas via Digitalmars-d-learn wrote: On Friday, 15 March 2019 at 21:46:50 UTC, Ali Çehreli wrote: [...] Or use template constraints: struct Vector { Vector opBinary(string op)(Vector rhs) if (op =

Re: Operator Overloading with multiple return types

2019-03-15 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Mar 15, 2019 at 10:30:41PM +, eXodiquas via Digitalmars-d-learn wrote: > On Friday, 15 March 2019 at 21:46:50 UTC, Ali Çehreli wrote: [...] > > Or use template constraints: > > > > struct Vector { > > Vector opBinary(string op)(Vector rhs) > > if (op == "+") { > > return V

Re: Operator Overloading with multiple return types

2019-03-15 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Mar 15, 2019 at 09:35:12PM +, eXodiquas via Digitalmars-d-learn wrote: [...] > Vector opBinary(string op)(Vector rhs) > { > static if (op == "+") return Vector(this.x + rhs.x, this.y + rhs.y); > else static if (op == "-") return Vector(this.x - rhs.x, this.y - > rhs.y); > } >

Re: Operator Overloading with multiple return types

2019-03-15 Thread drug via Digitalmars-d-learn
16.03.2019 1:30, eXodiquas пишет: On Friday, 15 March 2019 at 21:46:50 UTC, Ali Çehreli wrote: On 03/15/2019 02:43 PM, Sebastiaan Koppe wrote: On Friday, 15 March 2019 at 21:35:12 UTC, eXodiquas wrote: Is there any way to achive this behaivour with D2? Yep. Just make the return type in the f

Re: Operator Overloading with multiple return types

2019-03-15 Thread eXodiquas via Digitalmars-d-learn
On Friday, 15 March 2019 at 21:46:50 UTC, Ali Çehreli wrote: On 03/15/2019 02:43 PM, Sebastiaan Koppe wrote: On Friday, 15 March 2019 at 21:35:12 UTC, eXodiquas wrote: Is there any way to achive this behaivour with D2? Yep. Just make the return type in the function declaration `auto`. You ar

Re: Operator Overloading with multiple return types

2019-03-15 Thread Ali Çehreli via Digitalmars-d-learn
On 03/15/2019 02:43 PM, Sebastiaan Koppe wrote: On Friday, 15 March 2019 at 21:35:12 UTC, eXodiquas wrote: Is there any way to achive this behaivour with D2? Yep. Just make the return type in the function declaration `auto`. You are then free to return a different type in each static branch.

Re: Operator Overloading with multiple return types

2019-03-15 Thread Sebastiaan Koppe via Digitalmars-d-learn
On Friday, 15 March 2019 at 21:35:12 UTC, eXodiquas wrote: Is there any way to achive this behaivour with D2? Yep. Just make the return type in the function declaration `auto`. You are then free to return a different type in each static branch.

Operator Overloading with multiple return types

2019-03-15 Thread eXodiquas via Digitalmars-d-learn
Hi everyone, i'm currently working on a small physics engine and I thought it would be a nice feature to overload the operators of my vector struct so I don't have to make ugly function calls just to add and "multiply" my vectors. The problem now is that overloadi

Re: Operator overloading for size_t

2019-03-15 Thread Jani Hur via Digitalmars-d-learn
On Thursday, 14 March 2019 at 19:39:53 UTC, Alec Stewart wrote: For < and >, would one do this? I think you'd benefit a lot by reading http://ddili.org/ders/d.en/operator_overloading.html (just search for opCmp). I bet that will eliminate most of your confusion !

Re: Operator overloading for size_t

2019-03-14 Thread Alec Stewart via Digitalmars-d-learn
On Thursday, 14 March 2019 at 18:25:17 UTC, H. S. Teoh wrote: On Thu, Mar 14, 2019 at 06:07:46PM +, Alec Stewart via Digitalmars-d-learn wrote: [...] bool opEquals(ref const Interval i) const { // probably would be a bit more than just this, but for this issue // let's

Re: Operator overloading for size_t

2019-03-14 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Mar 14, 2019 at 06:07:46PM +, Alec Stewart via Digitalmars-d-learn wrote: [...] > bool opEquals(ref const Interval i) const { > // probably would be a bit more than just this, but for this issue > // let's just stick with this. > return d_start.opEquals(othe

Re: Operator overloading for size_t

2019-03-14 Thread Adam D. Ruppe via Digitalmars-d-learn
bother with operator overloading here, or just make a member function? You shouldn't often call .opEquals yourself, just write a == b and let the compiler translate it if it needs to.

Operator overloading for size_t

2019-03-14 Thread Alec Stewart via Digitalmars-d-learn
uals` are callable using argument types `(const(ulong), const(ulong))`, candidates are:` and it doesn't say the candidates. So should I bother with operator overloading here, or just make a member function? [1] https://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_algorithm

Re: Templated operator overloading

2018-08-22 Thread XavierAP via Digitalmars-d-learn
On Wednesday, 22 August 2018 at 12:36:39 UTC, Simen Kjærås wrote: Since both your opOpAssigns match equally, the compiler throws up. The solution is to add some sort of restriction: This doesn't happen apparently: the operator has a left and a right side, even if both types define the operat

Re: Templated operator overloading

2018-08-22 Thread XavierAP via Digitalmars-d-learn
On Wednesday, 22 August 2018 at 13:20:01 UTC, aliak wrote: "void opOpAssign(string op, T)(ref Tthis, const ref T x)" looks like the wrong signature for opOpAssign. Oh I'll put on my stupid hat now... I realize I had copy-pasted the wrong syntax from the global function attempt, but I swear

Re: Templated operator overloading

2018-08-22 Thread aliak via Digitalmars-d-learn
hoice is not as advantageous as I think, and I may change this design from structs to classes, or else the code duplication would be small and never subject to change. But now I'm just trying for the sake of learning to find out what works or not in terms of templated operator overloading, an

Re: Templated operator overloading

2018-08-22 Thread Simen Kjærås via Digitalmars-d-learn
On Wednesday, 22 August 2018 at 11:58:25 UTC, XavierAP wrote: When I want to have the same operator overloading code in both types however, I can't make it work: From https://dlang.org/spec/operatoroverloading.html#binary: "the one with the ‘better’ match is selected. It is an error

Templated operator overloading

2018-08-22 Thread XavierAP via Digitalmars-d-learn
gn from structs to classes, or else the code duplication would be small and never subject to change. But now I'm just trying for the sake of learning to find out what works or not in terms of templated operator overloading, and whether the reason something doesn't work is by design

Re: Virtual table in class without overloading

2018-04-28 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 28 April 2018 at 11:52:50 UTC, Jonathan M Davis wrote: unless you're declaring the type as a class purely so that it's forced to be a reference type. You can do a struct as a reference type too - just make it a pimpl handle.

Re: Virtual table in class without overloading

2018-04-28 Thread Jonathan M Davis via Digitalmars-d-learn
optimize and remove table of virtual functions because in these > examples we see no method overloading. > > Thanks. Object has virtual functions, so it's not possible to ever remove the virtual table from a D class. Also, even if Object didn't have virtual functions, I do

Virtual table in class without overloading

2018-04-28 Thread Andrey via Digitalmars-d-learn
classes don't have any vtables. What about D? In D all methods are virtual by default in classes. Will complier optimize and remove table of virtual functions because in these examples we see no method overloading. Thanks.

Re: Function overloading between modules

2018-02-23 Thread bauss via Digitalmars-d-learn
On Thursday, 22 February 2018 at 21:12:45 UTC, JN wrote: Is this expected behaviour? bar.d --- void foo(string s) { } app.d --- import std.stdio; import bar; void foo(int x) { } void main() { foo("hi"); }; === Error: function app.foo (int x) is not callable using argument types (string

Re: Function overloading between modules

2018-02-22 Thread ketmar via Digitalmars-d-learn
JN wrote: same idea? absolutely the same. non-qualified imports (be it template, or function) won't take part in overload resoultion.

Re: Function overloading between modules

2018-02-22 Thread JN via Digitalmars-d-learn
On Thursday, 22 February 2018 at 21:19:12 UTC, ketmar wrote: yes. this is done so unqualified won't silently "steal" your functions. this can cause some unexpected (and hard to find) bugs. if you want it to work, you can either do qualified import import bar : foo; or manuall bring

Re: Function overloading between modules

2018-02-22 Thread ketmar via Digitalmars-d-learn
JN wrote: Is this expected behaviour? bar.d --- void foo(string s) { } app.d --- import std.stdio; import bar; void foo(int x) { } void main() { foo("hi"); }; === Error: function app.foo (int x) is not callable using argument types (string) yes. this is done so unqualified won't si

  1   2   3   4   5   6   >