Nice example for operator overload resulting in readable linear algebra expressions

2021-11-19 Thread Martin Tschierschke via Digitalmars-d-learn
= %s*(%s + %s) =", p1, p1, p2); write(p1 * (p1 + p2)); // compare this to code without operator overload: } ``` Compare: ``p1 * (p1 + p2)`` to something with a structure like ``dot(p1,(add(p1,p2)).`` (With the Dlangs UFCS it might become: ``p1.dot(p1.add(p2))`` )

Re: how to access record[0] of a csv row? Error: no [] operator overload for type CsvRecord!(int, cast(Malformed)1, string, dchar)

2020-12-07 Thread Paul Backus via Digitalmars-d-learn
On Monday, 7 December 2020 at 06:18:33 UTC, mw wrote: Now, how to convert it to a native array: double[] row = record; Error: cannot implicitly convert expression record of type Tuple!(double, double, double, ..., double) to double[] (I know for tuple, we can do: double[] arr = [record];)

Re: how to access record[0] of a csv row? Error: no [] operator overload for type CsvRecord!(int, cast(Malformed)1, string, dchar)

2020-12-06 Thread mw via Digitalmars-d-learn
On Monday, 7 December 2020 at 04:38:07 UTC, Paul Backus wrote: On Monday, 7 December 2020 at 04:03:05 UTC, mw wrote: So my next question: given N, how do I create a Tuple!(double, double, ... n-double) type programmatically? import std.meta: Repeat; alias NDoubles = Tuple!(Repeat!(N, double))

Re: how to access record[0] of a csv row? Error: no [] operator overload for type CsvRecord!(int, cast(Malformed)1, string, dchar)

2020-12-06 Thread Paul Backus via Digitalmars-d-learn
On Monday, 7 December 2020 at 04:03:05 UTC, mw wrote: So my next question: given N, how do I create a Tuple!(double, double, ... n-double) type programmatically? import std.meta: Repeat; alias NDoubles = Tuple!(Repeat!(N, double)); Note that N must be a compile-time constant, since the number

Re: how to access record[0] of a csv row? Error: no [] operator overload for type CsvRecord!(int, cast(Malformed)1, string, dchar)

2020-12-06 Thread mw via Digitalmars-d-learn
On Monday, 7 December 2020 at 03:51:02 UTC, Paul Backus wrote: On Monday, 7 December 2020 at 02:25:23 UTC, mw wrote: onlineapp.d(8): Error: no [] operator overload for type CsvRecord!(int, cast(Malformed)1, string, dchar) should `r`'s type be integer array? and how do I access each ele

Re: how to access record[0] of a csv row? Error: no [] operator overload for type CsvRecord!(int, cast(Malformed)1, string, dchar)

2020-12-06 Thread Paul Backus via Digitalmars-d-learn
On Monday, 7 December 2020 at 02:25:23 UTC, mw wrote: onlineapp.d(8): Error: no [] operator overload for type CsvRecord!(int, cast(Malformed)1, string, dchar) should `r`'s type be integer array? and how do I access each elelment of the row? Thanks. The docs [1] say that csvReader re

how to access record[0] of a csv row? Error: no [] operator overload for type CsvRecord!(int, cast(Malformed)1, string, dchar)

2020-12-06 Thread mw via Digitalmars-d-learn
auto records = text.csvReader!int; foreach(r; records) {writeln(r[0]);} // line 8 assert(records.equal!equal([ [76, 26, 22], ])); } but I got a compile error: onlineapp.d(8): Error: no [] operator overload for type CsvRecord!(int, cast(Malformed)1, string, dchar) should `r`&#x

Re: operator overload for sh-like scripting ?

2020-02-17 Thread Simen Kjærås via Digitalmars-d-learn
On Monday, 17 February 2020 at 13:03:38 UTC, Basile B. wrote: eg Sh(echo) < "meh"; struct Sh { // you see the idea we have op overload for < here } You can't overload < separately - all the comparison operators (<, <=, >, >=) are handled via opCmp. Even if you choose to go down that

Re: operator overload for sh-like scripting ?

2020-02-17 Thread Adam D. Ruppe via Digitalmars-d-learn
On Monday, 17 February 2020 at 13:03:38 UTC, Basile B. wrote: Sh(echo) < "meh"; Not allowed in D - it only does a comparison overload which covers < and > (and <= and >=). Though we could do strings and stuff. especially with my string interpolation dip https://gist.github.com/adamdrupp

operator overload for sh-like scripting ?

2020-02-17 Thread Basile B. via Digitalmars-d-learn
eg Sh(echo) < "meh"; struct Sh { // you see the idea we have op overload for < here }

Re: no [] operator overload for type Chunks!(char[])

2018-05-31 Thread Steven Schveighoffer via Digitalmars-d-learn
On 5/30/18 5:41 PM, Malte wrote: On Wednesday, 30 May 2018 at 21:27:44 UTC, Ali Çehreli wrote: On 05/30/2018 02:19 PM, Malte wrote: Why does this code complain at the last line about a missing [] operator overload? auto buffer = new char[6]; auto chunked = buffer.chunks(3); chunked[1][2

Re: no [] operator overload for type Chunks!(char[])

2018-05-30 Thread Malte via Digitalmars-d-learn
On Wednesday, 30 May 2018 at 21:27:44 UTC, Ali Çehreli wrote: On 05/30/2018 02:19 PM, Malte wrote: Why does this code complain at the last line about a missing [] operator overload? auto buffer = new char[6]; auto chunked = buffer.chunks(3); chunked[1][2] = '!'; Same happens

Re: no [] operator overload for type Chunks!(char[])

2018-05-30 Thread Ali Çehreli via Digitalmars-d-learn
On 05/30/2018 02:19 PM, Malte wrote: Why does this code complain at the last line about a missing [] operator overload? auto buffer = new char[6]; auto chunked = buffer.chunks(3); chunked[1][2] = '!'; Same happens with wchar. Dchar and byte work as expected. UTF-8 auto decodi

no [] operator overload for type Chunks!(char[])

2018-05-30 Thread Malte via Digitalmars-d-learn
Why does this code complain at the last line about a missing [] operator overload? auto buffer = new char[6]; auto chunked = buffer.chunks(3); chunked[1][2] = '!'; Same happens with wchar. Dchar and byte work as expected.

Re: operator overload

2017-12-12 Thread dark777 via Digitalmars-d-learn
On Tuesday, 12 December 2017 at 17:13:55 UTC, Biotronic wrote: On Tuesday, 12 December 2017 at 16:54:17 UTC, Biotronic wrote: There is no way in C++ to set the format the way you want it. If you want binary output, you need to call a function like your binario function. Of course this is not

Re: operator overload

2017-12-12 Thread dark777 via Digitalmars-d-learn
On Tuesday, 12 December 2017 at 17:13:55 UTC, Biotronic wrote: On Tuesday, 12 December 2017 at 16:54:17 UTC, Biotronic wrote: There is no way in C++ to set the format the way you want it. If you want binary output, you need to call a function like your binario function. Of course this is not

Re: operator overload

2017-12-12 Thread Biotronic via Digitalmars-d-learn
On Tuesday, 12 December 2017 at 16:54:17 UTC, Biotronic wrote: There is no way in C++ to set the format the way you want it. If you want binary output, you need to call a function like your binario function. Of course this is not entirely true - there is a way, but it's ugly and probably not

Re: operator overload

2017-12-12 Thread Biotronic via Digitalmars-d-learn
On Tuesday, 12 December 2017 at 15:52:09 UTC, dark777 wrote: I know that this community is not of c ++, but some time I have been studying how to do overload of ostream operators in c ++ and I even managed to get to this result but the same is not converting to binary only presents zeros as out

operator overload

2017-12-12 Thread dark777 via Digitalmars-d-learn
I know that this community is not of c ++, but some time I have been studying how to do overload of ostream operators in c ++ and I even managed to get to this result but the same is not converting to binary only presents zeros as output to any number already tried to pass parameter of variable

Re: operator overload for enum

2016-06-10 Thread Timon Gehr via Digitalmars-d-learn
On 10.06.2016 13:02, Satoshi wrote: Hello, why operator overloading is not working as a static methods through the UFCS? ... It's an arbitrary limitation. https://issues.dlang.org/show_bug.cgi?id=8062 (The specification has been updated in the meantime, it now documents the limitation explici

operator overload for enum

2016-06-10 Thread Satoshi via Digitalmars-d-learn
Hello, why operator overloading is not working as a static methods through the UFCS? I need overload a << operator in this way: enum PlatformID { None, Trinix, Unix, MacOS, Windows } PlatformID toPlatformID(string platform) { switch (platform.to

Re: What's wrong in this templatized operator overload ?

2015-10-22 Thread MobPassenger via Digitalmars-d-learn
On Thursday, 22 October 2015 at 05:17:29 UTC, Cauterite wrote: On Thursday, 22 October 2015 at 04:25:01 UTC, MobPassenger wrote: On Thursday, 22 October 2015 at 04:01:16 UTC, Mike Parker wrote: On Thursday, 22 October 2015 at 03:19:49 UTC, MobPassenger wrote: code: --- struct Foo { bool op

Re: What's wrong in this templatized operator overload ?

2015-10-21 Thread Cauterite via Digitalmars-d-learn
On Thursday, 22 October 2015 at 04:25:01 UTC, MobPassenger wrote: On Thursday, 22 October 2015 at 04:01:16 UTC, Mike Parker wrote: On Thursday, 22 October 2015 at 03:19:49 UTC, MobPassenger wrote: code: --- struct Foo { bool opIn_r(T)(T t){return false;} } This needs to be marked with co

Re: What's wrong in this templatized operator overload ?

2015-10-21 Thread Vladimir Panteleev via Digitalmars-d-learn
On Thursday, 22 October 2015 at 03:21:35 UTC, MobPassenger wrote: On Thursday, 22 October 2015 at 03:18:25 UTC, MobPassenger wrote: code: Plz don't reply, there's been a forum bug while posting. What forum bug would that be?

Re: What's wrong in this templatized operator overload ?

2015-10-21 Thread MobPassenger via Digitalmars-d-learn
On Thursday, 22 October 2015 at 04:01:16 UTC, Mike Parker wrote: On Thursday, 22 October 2015 at 03:19:49 UTC, MobPassenger wrote: code: --- struct Foo { bool opIn_r(T)(T t){return false;} } This needs to be marked with const: struct Foo { bool opIn_r(T)(T t) const {return false;} }

Re: What's wrong in this templatized operator overload ?

2015-10-21 Thread Mike Parker via Digitalmars-d-learn
On Thursday, 22 October 2015 at 03:19:49 UTC, MobPassenger wrote: code: --- struct Foo { bool opIn_r(T)(T t){return false;} } This needs to be marked with const: struct Foo { bool opIn_r(T)(T t) const {return false;} }

Re: What's wrong in this templatized operator overload ?

2015-10-21 Thread MobPassenger via Digitalmars-d-learn
On Thursday, 22 October 2015 at 03:18:25 UTC, MobPassenger wrote: code: Plz don't reply, there's been a forum bug while posting. Full post is here: http://forum.dlang.org/thread/kaqyeiakjunqoexos...@forum.dlang.org

What's wrong in this templatized operator overload ?

2015-10-21 Thread MobPassenger via Digitalmars-d-learn
code: --- struct Foo { bool opIn_r(T)(T t){return false;} } static immutable Foo foo; // ouch //static Foo foo; // OK void main() { assert("a" !in foo); }

What's wrong in this templatized operator overload ?

2015-10-21 Thread MobPassenger via Digitalmars-d-learn
code: --- struct Foo { bool opIn_r(T)(T t){return false;} } static immutable Foo foo; // ouch //static Foo foo; // OK void main() { assert("a" !in foo); } --- output: --- Error: template Foo.opIn_r cannot deduce function from argument types !()(string) immutable, candidates are: runna

Re: Does it make sense to add attribute to operator overload functions ?

2014-10-25 Thread Jkpl via Digitalmars-d-learn
On Saturday, 25 October 2014 at 18:38:12 UTC, John Colvin wrote: On Saturday, 25 October 2014 at 17:14:51 UTC, Jkpl wrote: Everything is in the Q. I ask this because those functions are hidden behind symbols and keywords (+=, ~, in, etc.). It's not that obvious for a user who would write a cust

Re: Does it make sense to add attribute to operator overload functions ?

2014-10-25 Thread John Colvin via Digitalmars-d-learn
On Saturday, 25 October 2014 at 17:14:51 UTC, Jkpl wrote: Everything is in the Q. I ask this because those functions are hidden behind symbols and keywords (+=, ~, in, etc.). It's not that obvious for a user who would write a custom type. e.g: --- struct myType { @safe nothrow opIn

Does it make sense to add attribute to operator overload functions ?

2014-10-25 Thread Jkpl via Digitalmars-d-learn
Everything is in the Q. I ask this because those functions are hidden behind symbols and keywords (+=, ~, in, etc.). It's not that obvious for a user who would write a custom type. e.g: --- struct myType { @safe nothrow opIndexAssign(t1 paramValue,t2 paramIndex){} } --- are

Re: template operator overload

2013-11-27 Thread Dicebot
On Wednesday, 27 November 2013 at 18:40:09 UTC, Namespace wrote: Too bad. Would have been really awesome. It is pretty hard to define within common grammar rules because of implicit nature of operators.

Re: template operator overload

2013-11-27 Thread Namespace
On Wednesday, 27 November 2013 at 17:47:13 UTC, bearophile wrote: Namespace: void main() { A a; a.opIndex!int(0); // [1] a!int[0]; // [2] } [1] works, but [2] fails. How can I call opIndex with bracket syntax and a typename? Or is this not possible? I think that

Re: template operator overload

2013-11-27 Thread bearophile
Namespace: void main() { A a; a.opIndex!int(0); // [1] a!int[0]; // [2] } [1] works, but [2] fails. How can I call opIndex with bracket syntax and a typename? Or is this not possible? I think that's not supported by D syntax. So if you want that, you need to use

Re: template operator overload

2013-11-27 Thread Namespace
On Wednesday, 27 November 2013 at 17:25:49 UTC, Shammah Chancellor wrote: On 2013-11-27 16:07:50 +, Namespace said: Just out of curiosity: Is it possible to call an overloaded operator with a template type? import std.stdio; struct A { void opIndex(T)(size_t index) {

Re: template operator overload

2013-11-27 Thread Shammah Chancellor
On 2013-11-27 16:07:50 +, Namespace said: Just out of curiosity: Is it possible to call an overloaded operator with a template type? import std.stdio; struct A { void opIndex(T)(size_t index) { } } void main() { A a; a.opIndex!int(0);

template operator overload

2013-11-27 Thread Namespace
Just out of curiosity: Is it possible to call an overloaded operator with a template type? import std.stdio; struct A { void opIndex(T)(size_t index) { } } void main() { A a; a.opIndex!int(0); // [1] a!int[0]; // [2] } [1] work

Re: Any trick for defining an operator overload in a different namespace?

2013-08-31 Thread Andrej Mitrovic
On 9/1/13, Ali Çehreli wrote: > This is the limitation of inner structs' not having an 'outer' > reference, right? Right, but this was just a workaround. Anyway I did just realize I can use opDispatch for this: - class C { this() { this.opts["foo"] = 1; } private aut

Re: Any trick for defining an operator overload in a different namespace?

2013-08-31 Thread Ali Çehreli
On 08/31/2013 03:07 AM, Andrej Mitrovic wrote: > I'm trying to achieve the syntax "opts[...] = 123", rather than using > the more direct "this[...] = 123". I can use this code: > > - > class C > { > this() > { > opts = Opts(this); > opts["foo"] = 1; > } > >

Re: Any trick for defining an operator overload in a different namespace?

2013-08-31 Thread Artur Skawina
On 08/31/13 12:07, Andrej Mitrovic wrote: > But this explicitly stores the 'this' reference in the struct, I was > wondering if anyone knows of a trick to avoid having to do that. As > you can tell I just want a more convenient operator-based syntax over > calling the 'assign' method, but I don't w

Any trick for defining an operator overload in a different namespace?

2013-08-31 Thread Andrej Mitrovic
I'm trying to achieve the syntax "opts[...] = 123", rather than using the more direct "this[...] = 123". I can use this code: - class C { this() { opts = Opts(this); opts["foo"] = 1; } struct Opts { C c; void opIndexAssign(T)(T value, strin

Re: static / global operator overload

2013-08-18 Thread monarch_dodra
On Sunday, 18 August 2013 at 21:23:05 UTC, Namespace wrote: On Sunday, 18 August 2013 at 15:29:26 UTC, Ali Çehreli wrote: On 08/18/2013 07:34 AM, Namespace wrote: > In C++ you can declare operator overloads inside and outside of classes > (the latter is more popular) The latter is popular beca

Re: static / global operator overload

2013-08-18 Thread Namespace
On Sunday, 18 August 2013 at 15:29:26 UTC, Ali Çehreli wrote: On 08/18/2013 07:34 AM, Namespace wrote: > In C++ you can declare operator overloads inside and outside of classes > (the latter is more popular) The latter is popular because a global operator takes advantage of implicit type conve

Re: static / global operator overload

2013-08-18 Thread monarch_dodra
On Sunday, 18 August 2013 at 15:29:26 UTC, Ali Çehreli wrote: On 08/18/2013 07:34 AM, Namespace wrote: > In C++ you can declare operator overloads inside and outside of classes > (the latter is more popular) The latter is popular because a global operator takes advantage of implicit type conve

Re: static / global operator overload

2013-08-18 Thread Ali Çehreli
On 08/18/2013 07:34 AM, Namespace wrote: > In C++ you can declare operator overloads inside and outside of classes > (the latter is more popular) The latter is popular because a global operator takes advantage of implicit type conversions. A global operator+ allows using an int even on the lef

static / global operator overload

2013-08-18 Thread Namespace
I can't find anything so I ask here: what was the decision to disallow static or global operator overloads? In C++ you can declare operator overloads inside and outside of classes (the latter is more popular), so why wasn't this introduced in D also? Thanks in advance. :)

Re: parse json-string - operator overload error

2012-05-17 Thread Jarl André
On Wednesday, 1 December 2010 at 08:17:37 UTC, zusta wrote: Hey guys, I'm trying to read a JSON-formated file. The parsing of the file seems to be correct, but I always get the following error: "Error: no [] operator overload for type JSONValue". For testing purposes, I also

Re: primitive type operator overload

2012-04-20 Thread H. S. Teoh
On Fri, Apr 20, 2012 at 07:12:41PM +0200, Mafi wrote: > Am 20.04.2012 18:41, schrieb bearophile: > >Dominic Jones: > > > >>I want to overload a primitive type operator so that I can do > >>something like > >> > >>double a; > >>myStruct b; > >>writeln(a + b); > > > >You can't overload the operator o

Re: primitive type operator overload

2012-04-20 Thread Mafi
Am 20.04.2012 18:41, schrieb bearophile: Dominic Jones: I want to overload a primitive type operator so that I can do something like double a; myStruct b; writeln(a + b); You can't overload the operator of a primitive, but binary operators come in left and right versions: ... Bye, bearoph

Re: primitive type operator overload

2012-04-20 Thread bearophile
Dominic Jones: I want to overload a primitive type operator so that I can do something like double a; myStruct b; writeln(a + b); You can't overload the operator of a primitive, but binary operators come in left and right versions: http://dlang.org/operatoroverloading.html#Binary a.opBina

primitive type operator overload

2012-04-20 Thread dominic jones
Hello, I want to overload a primitive type operator so that I can do something like double a; myStruct b; writeln(a + b); but have no idea how to do it. Something similar(?) is already implemented in the language, i.e. double x; double[] y; writeln(x + y); but after searching the dmd2/src

parse json-string - operator overload error

2010-12-01 Thread zusta
Hey guys, I'm trying to read a JSON-formated file. The parsing of the file seems to be correct, but I always get the following error: "Error: no [] operator overload for type JSONValue". For testing purposes, I also tried the code of http://www.digitalmars.com/d/archive