Re: How to call a templated overload of opIndex

2025-08-18 Thread MelisaTreutel via Digitalmars-d-learn
On Friday, 15 August 2025 at 21:45:09 UTC, Marc wrote: On Friday, 15 August 2025 at 21:09:50 UTC, Ali Çehreli wrote: > > Any help would be really appreciated. This design doesn't seem useful. How about something like the following: import std.conv : to; x[i, j].to!double; y[i, j].to

Re: Debug help - foreach opApply overload School example: Programming in D

2025-08-18 Thread Brother Bill via Digitalmars-d-learn
On Monday, 18 August 2025 at 17:58:54 UTC, Dejan Lekic wrote: So, to fix your error, you can remove the "const" method qualifier from opApply, or cast the const away from the member when you use it. Thanks for the tip. It just needed more 'const' keywords sprinkled in here and there. Here

Re: Debug help - foreach opApply overload School example: Programming in D

2025-08-18 Thread Dejan Lekic via Digitalmars-d-learn
On Monday, 18 August 2025 at 13:32:57 UTC, Brother Bill wrote: Page 496 of Programming in D Going in circles. Please correct so it compiles and works. An explanation would be helpful also. Ali's book have a section about const member qualifier - "51.5 const member functions", but I think it

Debug help - foreach opApply overload School example: Programming in D

2025-08-18 Thread Brother Bill via Digitalmars-d-learn
Page 496 of Programming in D Going in circles. Please correct so it compiles and works. An explanation would be helpful also. Error Message ``` c:\dev\D\71 - 80\c73_2d_School_Student_Teacher\source\app.d(23): Error: cannot implicitly convert expression `this.students[cast(ulong)i]` of type `c

Re: How to call a templated overload of opIndex

2025-08-15 Thread Marc via Digitalmars-d-learn
On Friday, 15 August 2025 at 21:09:50 UTC, Ali Çehreli wrote: > > Any help would be really appreciated. This design doesn't seem useful. How about something like the following: import std.conv : to; x[i, j].to!double; y[i, j].to!float; Ali Thanks a lot for your reply. It should be

Re: How to call a templated overload of opIndex

2025-08-15 Thread Ali Çehreli via Digitalmars-d-learn
upposed to allow the [i, j] syntax. Additionally, it is not clear what the meaning of !double vs. !float is. Wouldn't it be a bug to call !double when the data is float? > It just doesn't work and I get the error `is not a > template declaration, it is a variable`. > > ``

Re: How to call a templated overload of opIndex

2025-08-15 Thread Marc via Digitalmars-d-learn
On Friday, 15 August 2025 at 17:51:50 UTC, Nick Treleaven wrote: On Friday, 15 August 2025 at 17:38:53 UTC, Marc wrote: Hello, In my typed data frames I've this type of code. It works for default value (float) but I can't call `opIndex!double[i, j]` or `opIndex!string[i,j]`. You need to writ

Re: How to call a templated overload of opIndex

2025-08-15 Thread Nick Treleaven via Digitalmars-d-learn
On Friday, 15 August 2025 at 17:38:53 UTC, Marc wrote: Hello, In my typed data frames I've this type of code. It works for default value (float) but I can't call `opIndex!double[i, j]` or `opIndex!string[i,j]`. You need to write `opIndex!double(i, j)` to manually call the method. (You're usi

How to call a templated overload of opIndex

2025-08-15 Thread Marc via Digitalmars-d-learn
Hello, In my typed data frames I've this type of code. It works for default value (float) but I can't call `opIndex!double[i, j]` or `opIndex!string[i,j]`. It just doesn't work and I get the error `is not a template declaration, it is a variable`. ```d // Overload [i, j]

Re: implicit cast and overload priority

2025-02-17 Thread ShadoLight via Digitalmars-d-learn
On Monday, 17 February 2025 at 09:33:32 UTC, mig09 wrote: On Monday, 17 February 2025 at 09:17:24 UTC, ShadoLight wrote: On Sunday, 16 February 2025 at 17:04:39 UTC, Jonathan M Davis wrote: [..] Ok, gotcha. I suspected that is what you meant and agree with that (in my example an implicit m

Re: implicit cast and overload priority

2025-02-17 Thread mig09 via Digitalmars-d-learn
On Monday, 17 February 2025 at 09:17:24 UTC, ShadoLight wrote: On Sunday, 16 February 2025 at 17:04:39 UTC, Jonathan M Davis wrote: [..] Ok, gotcha. I suspected that is what you meant and agree with that (in my example an implicit match would more sense on 'long', not 'bool'). Thanks for

Re: implicit cast and overload priority

2025-02-17 Thread ShadoLight via Digitalmars-d-learn
On Sunday, 16 February 2025 at 17:04:39 UTC, Jonathan M Davis wrote: [..] Ok, gotcha. I suspected that is what you meant and agree with that (in my example an implicit match would more sense on 'long', not 'bool'). Thanks for the detailed answer.

Re: implicit cast and overload priority

2025-02-16 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, February 16, 2025 4:54:46 AM MST ShadoLight via Digitalmars-d-learn wrote: > On Sunday, 16 February 2025 at 05:39:07 UTC, Jonathan M Davis > wrote: > > _especially_ when VRP comes into play, because then you get > > nonsense like foo(1) calling the bool overload. &g

Re: implicit cast and overload priority

2025-02-16 Thread ShadoLight via Digitalmars-d-learn
On Sunday, 16 February 2025 at 05:39:07 UTC, Jonathan M Davis wrote: _especially_ when VRP comes into play, because then you get nonsense like foo(1) calling the bool overload. I have often seen this mentioned, but I only see this happen if there *isn't* an integer overload i.e.

Re: implicit cast and overload priority

2025-02-15 Thread Jonathan M Davis via Digitalmars-d-learn
eventually end up with so many that no human > can remember them all, and code becomes much harder to understand. > > In C++, for example, overload resolution is extremely > complicated, because they added a bunch of rules to make these > kinds of "obvious" examples work: >

Re: implicit cast and overload priority

2025-02-15 Thread Paul Backus via Digitalmars-d-learn
rule so that the compiler can also see it! The problem with this approach is that, if you keep adding more and more rules, you eventually end up with so many that no human can remember them all, and code becomes much harder to understand. In C++, for example, overload resolution is extremely

Re: implicit cast and overload priority

2025-02-15 Thread Dom DiSc via Digitalmars-d-learn
On Friday, 14 February 2025 at 15:58:26 UTC, Paul Backus wrote: In your example, both overloads have match level 2: "match with implicit conversions". So the compiler attempts to figure out which one is more specialized by checking whether the parameters of one overload can be us

Re: implicit cast and overload priority

2025-02-14 Thread Paul Backus via Digitalmars-d-learn
On Friday, 14 February 2025 at 17:29:33 UTC, Andy Valencia wrote: On Friday, 14 February 2025 at 15:58:26 UTC, Paul Backus wrote: void foo(long x) { } void foo(ulong x) { } So I take it that a template with a static isSigned test would be the way to bifurcate foo()'s behavior? Andy Yes, t

Re: implicit cast and overload priority

2025-02-14 Thread Andy Valencia via Digitalmars-d-learn
On Friday, 14 February 2025 at 15:58:26 UTC, Paul Backus wrote: void foo(long x) { } void foo(ulong x) { } So I take it that a template with a static isSigned test would be the way to bifurcate foo()'s behavior? Andy

Re: implicit cast and overload priority

2025-02-14 Thread Paul Backus via Digitalmars-d-learn
unsigned type with same priority as a signed type?!? Clearly a longer type with matching signedness should always be a better match than a type that will discard the sign, no? Here's the spec for overload resolution: https://dlang.org/spec/function.html#function-overloading In your ex

Re: implicit cast and overload priority

2025-02-13 Thread bkoie via Digitalmars-d-learn
On Friday, 14 February 2025 at 04:28:25 UTC, Dom DiSc wrote: Why does this happen: ```d void foo(long x) { } void foo(ulong x) { } [...] trying to confused the compiler try that in c++ it will compile with no warnings how would the compiler actually know the dif between ulong 1, long 1, if n

implicit cast and overload priority

2025-02-13 Thread Dom DiSc via Digitalmars-d-learn
Why does this happen: ```d void foo(long x) { } void foo(ulong x) { } main() { foo(short(17)); // error: `foo` called with argument types `(short)` matches both: `foo(long x)` and: foo(ulong x)` } ``` Why does a short match an unsigned type with same priority as a signed type?!? Clearly a

Patterns for overload set recursion disambiguated by named arguments

2023-12-12 Thread monkyyy via Digitalmars-d-learn
an extremely overload draw function that eventually calls some set of these functions, but Im pretty sure that "named arguments dont survive T..." is causing problems for me, and I'm unsure how to work around it as I make more complex versions. https://github.com/crazymonkyyy/r

Re: Concepts like c++20 with specialized overload resolution.

2023-05-28 Thread Dom DiSc via Digitalmars-d-learn
more obfuscated for which cases there is an overload and for which not. A good (?!?) example of how bad this can get is the "to" template.

Re: Concepts like c++20 with specialized overload resolution.

2023-05-27 Thread H. S. Teoh via Digitalmars-d-learn
On Sat, May 27, 2023 at 05:49:27PM +, vushu via Digitalmars-d-learn wrote: > On Saturday, 27 May 2023 at 16:38:43 UTC, Steven Schveighoffer wrote: [...] > > void make_lava(T)(ref T lava) if (hasMagma!T) { > > lava.magma(); > > } > > > > void make_lava(T)(ref T lava_thing) if (!hasMagma!T){

Re: Concepts like c++20 with specialized overload resolution.

2023-05-27 Thread vushu via Digitalmars-d-learn
lava(); } ``` -Steve I see thanks for the example :), I think this probably the closest equivalent i dlang. I feel like overload in that case make things harder to read My example has less context switch, and hte logic is correctly understandable at first sight Only one make_lava functio

Re: Concepts like c++20 with specialized overload resolution.

2023-05-27 Thread ryuukk_ via Digitalmars-d-learn
:), I think this probably the closest equivalent i dlang. I feel like overload in that case make things harder to read My example has less context switch, and hte logic is correctly understandable at first sight Only one make_lava function

Re: Concepts like c++20 with specialized overload resolution.

2023-05-27 Thread vushu via Digitalmars-d-learn
On Saturday, 27 May 2023 at 16:38:43 UTC, Steven Schveighoffer wrote: On 5/27/23 9:50 AM, vushu wrote: On Saturday, 27 May 2023 at 13:42:29 UTC, Basile B. wrote: [...] Yes I know there is template constraint, but not with specialized overloading right? so you need to use static if for chec

Re: Concepts like c++20 with specialized overload resolution.

2023-05-27 Thread Steven Schveighoffer via Digitalmars-d-learn
On 5/27/23 9:50 AM, vushu wrote: On Saturday, 27 May 2023 at 13:42:29 UTC, Basile B. wrote: On Saturday, 27 May 2023 at 13:23:38 UTC, vushu wrote: [...] Is there something equivalent in dlang, doesn't seem like d support specialized overload? D solution is called [template constr

Re: Concepts like c++20 with specialized overload resolution.

2023-05-27 Thread ryuukk_ via Digitalmars-d-learn
On Saturday, 27 May 2023 at 13:23:38 UTC, vushu wrote: you can use: ``static if (__traits(hasMember, T, "magma"))`` ```D import std; struct LavaMan { void magma() { writeln(" LavaMan is throwing LAVA"); } } struct FakeVulcano { void try_making_lava() { writeln( " Making fake lava"); } }

Re: Concepts like c++20 with specialized overload resolution.

2023-05-27 Thread vushu via Digitalmars-d-learn
On Saturday, 27 May 2023 at 13:42:29 UTC, Basile B. wrote: On Saturday, 27 May 2023 at 13:23:38 UTC, vushu wrote: [...] Is there something equivalent in dlang, doesn't seem like d support specialized overload? D solution is called [template constraints](https://dlang.org/spec/template

Re: Concepts like c++20 with specialized overload resolution.

2023-05-27 Thread Basile B. via Digitalmars-d-learn
On Saturday, 27 May 2023 at 13:23:38 UTC, vushu wrote: [...] Is there something equivalent in dlang, doesn't seem like d support specialized overload? D solution is called [template constraints](https://dlang.org/spec/template.html#template_constraints).

Concepts like c++20 with specialized overload resolution.

2023-05-27 Thread vushu via Digitalmars-d-learn
ing LAVA ``` Is there something equivalent in dlang, doesn't seem like d support specialized overload?

Wrong compiler error message regarding overload sets

2023-02-07 Thread Andre Pany via Digitalmars-d-learn
Hi, for the source code below, the compiler says: app.d(26): constructor `app.TObject.this` hides base class function `app.DelphiObject.this` app.d(26): add `alias this = app.DelphiObject.this` to `app.TObject`'s body to merge the overload sets But if I add `alias

Re: Is it valid in D to write an opSlice overload that takes no arguments?

2022-09-11 Thread Adam D Ruppe via Digitalmars-d-learn
On Sunday, 11 September 2022 at 09:47:34 UTC, solidstate1991 wrote: Here's this code: This should be allowed, but also the way you're supposed to write it now is a no-argument opIndex.

Is it valid in D to write an opSlice overload that takes no arguments?

2022-09-11 Thread solidstate1991 via Digitalmars-d-learn
Here's this code: ```d auto opSlice() { struct Range { Attr currentAttr; auto front() { return currentAttr; } void popFront() { currentAttr = currentAttr._nextAttr; } bool empty() { return currentAttr is null; } } return Range(firstAttr); } ``` Th

Re: How to check if a class has a specific overload for member x?

2022-08-01 Thread pascal111 via Digitalmars-d-learn
On Monday, 1 August 2022 at 00:23:21 UTC, Hipreme wrote: On Sunday, 31 July 2022 at 19:25:51 UTC, Hipreme wrote: [...] Seems that is how it should be checked: ```d enum hasOverload(T, string member, FuncType)() { bool ret; static foreach(overload; __traits

Re: How to check if a class has a specific overload for member x?

2022-07-31 Thread Hipreme via Digitalmars-d-learn
On Sunday, 31 July 2022 at 19:25:51 UTC, Hipreme wrote: I wish to generate some functions using mixin template and I wish to check if the current class already has an specific overload with parameters (A, B). What I'm currently trying is doing `__traits(getOverloads, T, "foo&qu

How to check if a class has a specific overload for member x?

2022-07-31 Thread Hipreme via Digitalmars-d-learn
I wish to generate some functions using mixin template and I wish to check if the current class already has an specific overload with parameters (A, B). What I'm currently trying is doing `__traits(getOverloads, T, "foo")`, and then checking if its type are equal. But it seems

Re: split Error - no overload matches

2022-02-16 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Feb 15, 2022 at 06:37:44PM +, meta via Digitalmars-d-learn wrote: > A trick i use often: > > ```D > import std; > > void main() > { > import uni = std.uni; > writeln("Learning D is fun".split!(uni.isWhite)); > } > > ``` > > Under-rated way of importing things, you don't blo

Re: split Error - no overload matches

2022-02-15 Thread meta via Digitalmars-d-learn
A trick i use often: ```D import std; void main() { import uni = std.uni; writeln("Learning D is fun".split!(uni.isWhite)); } ``` Under-rated way of importing things, you don't bloat your scope anymore

Re: split Error - no overload matches

2022-02-15 Thread Steven Schveighoffer via Digitalmars-d-learn
ront ``` That second check is testing to see if it compiles, and if it doesn't for any reason, then it doesn't match. Now, the symbol `isWhite` is an overload set, which is in error (ambiguous). So I'm not sure how to test this better. Note, if I copy the entire definition of

Re: split Error - no overload matches

2022-02-14 Thread forkit via Digitalmars-d-learn
On Monday, 14 February 2022 at 11:37:38 UTC, ag0aep6g wrote: On 14.02.22 12:14, forkit wrote: However, if I uncomment the //import std.uni : isWhite; then it will compile. I don't understand. I thought 'import std;' would be sufficient here?? "isWhite" is ambiguous. There's std.uni.isWhite

Re: split Error - no overload matches

2022-02-14 Thread ag0aep6g via Digitalmars-d-learn
On 14.02.22 12:14, forkit wrote: However, if I uncomment the //import std.uni : isWhite; then it will compile. I don't understand. I thought 'import std;' would be sufficient here?? "isWhite" is ambiguous. There's std.uni.isWhite and std.ascii.isWhite. `import std;` can't know which one you

split Error - no overload matches

2022-02-14 Thread forkit via Digitalmars-d-learn
This code will not compile. Error: no overload matches for `split` However, if I uncomment the //import std.uni : isWhite; then it will compile. I don't understand. I thought 'import std;' would be sufficient here?? // module test; @safe: import std; void main()

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: 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 fault

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 Foo!

Re: template ctor overload Segmentation fault

2021-12-14 Thread Tejas via Digitalmars-d-learn
templated it is impossible for the compiler to issue this error before actually instantiating the constructor. I see 2 possible fixes for this: (1) either we rename the copy constructor symbol to __cpCtor so that it does not clash with the normal constructor overload set or (2) when a templated

Re: template ctor overload Segmentation fault

2021-12-14 Thread RazvanN via Digitalmars-d-learn
2 possible fixes for this: (1) either we rename the copy constructor symbol to __cpCtor so that it does not clash with the normal constructor overload set or (2) when a templated constructor is instantiated, we can check whether it is an rvalue constructor and issue an error if a copy

Re: template ctor overload Segmentation fault

2021-12-13 Thread Tejas via Digitalmars-d-learn
On Sunday, 12 December 2021 at 19:17:53 UTC, vit wrote: On Sunday, 12 December 2021 at 18:32:28 UTC, Imperatorn 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

Re: template ctor overload Segmentation fault

2021-12-13 Thread user1234 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 fault

Re: template ctor overload Segmentation fault

2021-12-12 Thread vit via Digitalmars-d-learn
On Sunday, 12 December 2021 at 18:32:28 UTC, Imperatorn 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: template ctor overload Segmentation fault

2021-12-12 Thread Imperatorn 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 fault

template ctor overload Segmentation fault

2021-12-12 Thread vit via Digitalmars-d-learn
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 fault (core dumped)

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: Possible to overload assignment of struct field ??

2021-08-23 Thread james.p.leblanc via Digitalmars-d-learn
On Tuesday, 24 August 2021 at 05:34:08 UTC, Ali Çehreli wrote: On 8/23/21 10:25 PM, james.p.leblanc wrote: So, you need a "property". Easy... :) 1) Rename the member e.g. as a_. 2) Write setter and getter functions named 'a'. struct Foo{ int a_; int a() const { return a_; } void

Re: Possible to overload assignment of struct field ??

2021-08-23 Thread Ali Çehreli via Digitalmars-d-learn
On 8/23/21 10:25 PM, james.p.leblanc wrote: So, you need a "property". Easy... :) 1) Rename the member e.g. as a_. 2) Write setter and getter functions named 'a'. struct Foo{ int a_; int a() const { return a_; } void a(int value) { a_ = value; } } void main(){ auto x = F

Possible to overload assignment of struct field ??

2021-08-23 Thread james.p.leblanc via Digitalmars-d-learn
Greetings, With a struct, there are many overload possibilities available. However, I haven't been able to find how to overload assignment of **selected fields** of a struct. For example, suppose: struct Foo{ int a; int b; ... } void main(){ auto x

Re: Not allowed to globally overload operators?

2021-07-20 Thread Tejas via Digitalmars-d-learn
On Tuesday, 20 July 2021 at 18:32:26 UTC, Ali Çehreli wrote: On 7/19/21 11:20 PM, Tejas wrote: > trying to create the spaceship operator of C++ Just to make sure, D's opCmp returns an int. That new C++ operator was added to provide the same semantics. Ali I know. As I already mentioned, I

Re: Not allowed to globally overload operators?

2021-07-20 Thread Ali Çehreli via Digitalmars-d-learn
On 7/20/21 11:49 AM, H. S. Teoh wrote: > On Tue, Jul 20, 2021 at 11:32:26AM -0700, Ali Çehreli via Digitalmars-d-learn wrote: >> On 7/19/21 11:20 PM, Tejas wrote: >> >>> trying to create the spaceship operator of C++ >> >> Just to make sure, D's opCmp returns an int. That new C++ operator was >

Re: Not allowed to globally overload operators?

2021-07-20 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 20 July 2021 at 18:49:07 UTC, H. S. Teoh wrote: On Tue, Jul 20, 2021 at 11:32:26AM -0700, Ali Çehreli via Digitalmars-d-learn wrote: On 7/19/21 11:20 PM, Tejas wrote: > trying to create the spaceship operator of C++ Just to make sure, D's opCmp returns an int. That new C++ operato

Re: Not allowed to globally overload operators?

2021-07-20 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Jul 20, 2021 at 11:32:26AM -0700, Ali Çehreli via Digitalmars-d-learn wrote: > On 7/19/21 11:20 PM, Tejas wrote: > > > trying to create the spaceship operator of C++ > > Just to make sure, D's opCmp returns an int. That new C++ operator was > added to provide the same semantics. [...] F

Re: Not allowed to globally overload operators?

2021-07-20 Thread Ali Çehreli via Digitalmars-d-learn
On 7/19/21 11:20 PM, Tejas wrote: > trying to create the spaceship operator of C++ Just to make sure, D's opCmp returns an int. That new C++ operator was added to provide the same semantics. Ali

Re: Not allowed to globally overload operators?

2021-07-19 Thread Tejas via Digitalmars-d-learn
On Tuesday, 20 July 2021 at 06:34:45 UTC, vit wrote: On Tuesday, 20 July 2021 at 06:20:34 UTC, Tejas wrote: ... Initially, I was trying to create the spaceship operator of C++, but we aren't allowed to create new operators ... D has spaceship operator: opCmp (https://dlang.org/spec/operator

Re: Not allowed to globally overload operators?

2021-07-19 Thread Tejas via Digitalmars-d-learn
wanted to verify whether we can even overload an operator globally, but even that seems to be failing, atleast for me. From the docs: Operator overloading is accomplished by rewriting operators whose operands are class or struct objects into calls to specially named members. https://dlan

Re: Not allowed to globally overload operators?

2021-07-19 Thread Mike Parker via Digitalmars-d-learn
On Tuesday, 20 July 2021 at 06:20:34 UTC, Tejas wrote: Why isn't it working by default? Initially, I was trying to create the spaceship operator of C++, but we aren't allowed to create new operators, it seems. Then I just wanted to verify whether we can even overload an operato

Re: Not allowed to globally overload operators?

2021-07-19 Thread vit via Digitalmars-d-learn
On Tuesday, 20 July 2021 at 06:20:34 UTC, Tejas wrote: ... Initially, I was trying to create the spaceship operator of C++, but we aren't allowed to create new operators ... D has spaceship operator: opCmp (https://dlang.org/spec/operatoroverloading.html#compare).

Not allowed to globally overload operators?

2021-07-19 Thread Tejas via Digitalmars-d-learn
quot;+"(1,5); ``` It works as expected. Why isn't it working by default? Initially, I was trying to create the spaceship operator of C++, but we aren't allowed to create new operators, it seems. Then I just wanted to verify whether we can even overload an operator globally

Re: templated overload of opAssign

2021-04-05 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 3 April 2021 at 13:46:17 UTC, kdevel wrote: Why does this code [...] ```d ec.opAssign (bar (1)); // okay // ec = bar (1); // Error: expression bar(1) is void and has no value ``` [...] compile with the abovementioned error? This is a compiler bug. You're not allowed to hav

Re: templated overload of opAssign

2021-04-05 Thread kdevel via Digitalmars-d-learn
On Monday, 5 April 2021 at 20:59:34 UTC, tsbockman wrote: However, `=` and `~=` should not treat `lazy void` parameters differently. They should either both work, or neither. I checked and this is actually a very old regression; both worked way back in DMD 2.061. So, I've filed a front-end bug

Re: templated overload of opAssign

2021-04-05 Thread tsbockman via Digitalmars-d-learn
On Monday, 5 April 2021 at 05:22:22 UTC, frame wrote: On Sunday, 4 April 2021 at 18:05:04 UTC, tsbockman wrote: Thus, the solution is to use an explicit `delegate` instead of `lazy`: Yes, I forgot to mention that. Could you please explain why you set 'scope' here? Isn't it wanted to keep

Re: templated overload of opAssign

2021-04-05 Thread tsbockman via Digitalmars-d-learn
On Monday, 5 April 2021 at 15:05:24 UTC, kdevel wrote: You changed the definition of ``bar`` while the exception collector (``EC``) is meant to catch and collect an exception thrown from the *unmodified* function. My point was that the code will work if you do explicitly what `lazy` does impl

Re: templated overload of opAssign

2021-04-05 Thread Imperatorn via Digitalmars-d-learn
On Monday, 5 April 2021 at 15:05:24 UTC, kdevel wrote: On Sunday, 4 April 2021 at 18:05:04 UTC, tsbockman wrote: ``` [...] [...] [...] [...] ``` [...] Nice, is this documented somewhere? 🤔 Maybe we could add a better error message or smth.

Re: templated overload of opAssign

2021-04-05 Thread kdevel via Digitalmars-d-learn
On Sunday, 4 April 2021 at 18:05:04 UTC, tsbockman wrote: ``` [...] You cannot assign void returned from bar() as parameter to opAssign(). The lazy keyword creates some internal delegate, thus opAssign() works instead. [...] auto bar (int i) { return () { if (i == 1) th

Re: templated overload of opAssign

2021-04-04 Thread frame via Digitalmars-d-learn
On Sunday, 4 April 2021 at 18:05:04 UTC, tsbockman wrote: Thus, the solution is to use an explicit `delegate` instead of `lazy`: Yes, I forgot to mention that. Could you please explain why you set 'scope' here? Isn't it wanted to keep references here?

Re: templated overload of opAssign

2021-04-04 Thread tsbockman via Digitalmars-d-learn
On Sunday, 4 April 2021 at 16:38:10 UTC, frame wrote: On Saturday, 3 April 2021 at 13:46:17 UTC, kdevel wrote: Why does this code ec.opAssign (bar (1)); // okay // ec = bar (1); // Error: expression bar(1) is void and has no value compile with the abovementioned error? You cannot a

Re: templated overload of opAssign

2021-04-04 Thread frame via Digitalmars-d-learn
On Saturday, 3 April 2021 at 13:46:17 UTC, kdevel wrote: Why does this code ec.opAssign (bar (1)); // okay // ec = bar (1); // Error: expression bar(1) is void and has no value compile with the abovementioned error? You cannot assign void returned from bar() as parameter to opAssig

templated overload of opAssign

2021-04-03 Thread kdevel via Digitalmars-d-learn
Why does this code ```d import std.stdio,std.typecons; struct EC { Exception [] ex; auto opAssign (X: void) (lazy X f) { writeln (__PRETTY_FUNCTION__); try return f (); catch (Exception e) ex ~= e; } } class E : Exception { this (string s) { super (s); } } void bar (int

Re: How do I overload += operator?

2021-01-26 Thread bachmeier via Digitalmars-d-learn
On Monday, 25 January 2021 at 17:09:22 UTC, Jack wrote: I'd like to make this work s += 10 where s is a struct. How can I do that? You have your answer, but someone else might come upon this in the future, so here's a link to the clearest explanation of operator overloading for someone new to

Re: How do I overload += operator?

2021-01-25 Thread Q. Schroll via Digitalmars-d-learn
On Monday, 25 January 2021 at 21:53:15 UTC, Jack wrote: That naming is confusing op: it is an operator method Op: it takes an operator as a parameter Assign: kinda obvious. As an example, there are opIndex, opIndexAssign and opIndexOpAssign. opIndex overloads obj[i]. opIndexAssign overloads

Re: How do I overload += operator?

2021-01-25 Thread Jack via Digitalmars-d-learn
On Monday, 25 January 2021 at 17:12:47 UTC, Paul Backus wrote: On Monday, 25 January 2021 at 17:09:22 UTC, Jack wrote: I'd like to make this work s += 10 where s is a struct. How can I do that? +=, -=, *=, and other compound assignment operators can be overloaded by defining `opOpAssign`: h

Re: How do I overload += operator?

2021-01-25 Thread Jack via Digitalmars-d-learn
On Monday, 25 January 2021 at 17:11:41 UTC, Adam D. Ruppe wrote: On Monday, 25 January 2021 at 17:09:22 UTC, Jack wrote: auto ref opAssign(string op, T)(T value) try opOpAssign instead opAssign is for = opOpAssign is for +=, -=, etc. It might be some variation but I think it works if y

Re: How do I overload += operator?

2021-01-25 Thread Paul Backus via Digitalmars-d-learn
On Monday, 25 January 2021 at 17:09:22 UTC, Jack wrote: I'd like to make this work s += 10 where s is a struct. How can I do that? +=, -=, *=, and other compound assignment operators can be overloaded by defining `opOpAssign`: https://dlang.org/spec/operatoroverloading.html#op-assign

Re: How do I overload += operator?

2021-01-25 Thread Adam D. Ruppe via Digitalmars-d-learn
On Monday, 25 January 2021 at 17:09:22 UTC, Jack wrote: auto ref opAssign(string op, T)(T value) try opOpAssign instead opAssign is for = opOpAssign is for +=, -=, etc. It might be some variation but I think it works if you just rename it.

How do I overload += operator?

2021-01-25 Thread Jack via Digitalmars-d-learn
I'd like to make this work s += 10 where s is a struct. How can I do that? this isn't working: auto ref opAssign(string op, T)(T value) if(op == "+") { m += value; return this; } the compiler didn't consider that overload and return: d

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: How make Optional pre determined parameter type without overload function?

2020-12-02 Thread Marcone via Digitalmars-d-learn
Now my slice works fine. // Tipo Nulo. class None {} // Função slice() auto slice(T1, T2, T3 = None)(T1 conteudo, T2 inicio, T3 fim = T3.init) { int start, end, startlen; static if (is(T2 == int)) {inicio = inicio < 0 ? conteudo.length + inicio : inicio;} static if (is(T3 == int))

Re: How make Optional pre determined parameter type without overload function?

2020-11-28 Thread Marcone via Digitalmars-d-learn
On Sunday, 29 November 2020 at 02:55:02 UTC, Ali Çehreli wrote: On 11/28/20 6:40 PM, Marcone wrote: void a(T1, T2)(T1 b, T2 c){ // I need parameter "c" optional, but only (String or int). How can I make it without overload function? } Since it's optional, T2 must have a

Re: How make Optional pre determined parameter type without overload function?

2020-11-28 Thread Ali Çehreli via Digitalmars-d-learn
On 11/28/20 6:40 PM, Marcone wrote: void a(T1, T2)(T1 b, T2 c){ // I need parameter "c" optional, but only (String or int). How can I make it without overload function? } Since it's optional, T2 must have a default type. I made it 'int' below. void a(T1, T2 =

How make Optional pre determined parameter type without overload function?

2020-11-28 Thread Marcone via Digitalmars-d-learn
void a(T1, T2)(T1 b, T2 c){ // I need parameter "c" optional, but only (String or int). How can I make it without overload function? }

Re: Value based overload resolution?

2020-11-09 Thread Paul Backus via Digitalmars-d-learn
On Monday, 9 November 2020 at 22:04:55 UTC, kdevel wrote: It appears to me that the overload resolution may depend on the /value/ of the function argument. According to [1] the type of 1 is int and that of 1L is long. Thus I would have expected foo!int and foo!long being called in those cases

Re: Value based overload resolution?

2020-11-09 Thread Adam D. Ruppe via Digitalmars-d-learn
On Monday, 9 November 2020 at 22:04:55 UTC, kdevel wrote: It appears to me that the overload resolution may depend on the /value/ of the function argument. According to [1] the type of 1 is int and that of 1L is long. That's not exactly true because of value range propagation. It is wei

Value based overload resolution?

2020-11-09 Thread kdevel via Digitalmars-d-learn
(id (1L)); foo (0x1_); foo (0x1_L); return 0; } ~~~ Output: $ ./id short id.foo(short s) short id.foo(short s) int id.foo!int.foo(int s) long id.foo!long.foo(long s) int id.foo!int.foo(int s) long id.foo!long.foo(long s) It appears to me that the overload resolution may depend on

Re: Is it possible to "overload" based on visibility?

2020-09-26 Thread 60rntogo via Digitalmars-d-learn
On Friday, 25 September 2020 at 18:58:54 UTC, H. S. Teoh wrote: You probably need to use the long-form of templates, with separate function declarations, to accomplish this. E.g.: ... Alright, but your example still contains shouldReturnByRef which presumably I need to implement myself. But

  1   2   3   4   >