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/raylib

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

2023-05-28 Thread Dom DiSc via Digitalmars-d-learn
and 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

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 func

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

2023-05-27 Thread ryuukk_ via Digitalmars-d-learn
mple :), 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

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 constraints

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.html

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); } ```

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, &quo

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 hack

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

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
f 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 split locally, it explains the problem correctly (though still not as clear as it could be):

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() { //import

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

Re: template ctor overload Segmentation fault

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

Re: template ctor overload Segmentation fault

2021-12-14 Thread Tejas via Digitalmars-d-learn
is 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){ }

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

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

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-24 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_; }

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 =

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

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,

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++

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. [...]

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-20 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

Re: Not allowed to globally overload operators?

2021-07-20 Thread Tejas via Digitalmars-d-learn
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://dlang.org/spec

Re: Not allowed to globally overload operators?

2021-07-20 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 operator globally

Re: Not allowed to globally overload operators?

2021-07-20 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-20 Thread Tejas via Digitalmars-d-learn
ry!"+"(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, but even that

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

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

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)

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

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

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

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

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

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.d(34)

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,

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

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 elelment

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 returns

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
to 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`'s type

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 defau

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 = int)(T1 b, T2 c

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 weird

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

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

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

2020-09-25 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Sep 25, 2020 at 05:58:08PM +, 60rntogo via Digitalmars-d-learn wrote: > On Friday, 25 September 2020 at 15:21:22 UTC, Steven Schveighoffer wrote: > > If the input is not ref, you should not return by ref, because then > > you would be returning a reference to local stack data that is

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

2020-09-25 Thread 60rntogo via Digitalmars-d-learn
On Friday, 25 September 2020 at 15:21:22 UTC, Steven Schveighoffer wrote: If the input is not ref, you should not return by ref, because then you would be returning a reference to local stack data that is about to be destroyed. Yes, I understand that. What I'm really after at this point is

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

2020-09-25 Thread Steven Schveighoffer via Digitalmars-d-learn
On 9/25/20 11:21 AM, Steven Schveighoffer wrote: I wouldn't depend on that mechanism yes. *yet*. -Steve

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

2020-09-25 Thread Steven Schveighoffer via Digitalmars-d-learn
there is a way of saying "just figure out if it's more optimal to return by value or const reference". If the input is not ref, you should not return by ref, because then you would be returning a reference to local stack data that is about to be destroyed. The only way to say "mak

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

2020-09-25 Thread 60rntogo via Digitalmars-d-learn
On Friday, 25 September 2020 at 14:21:59 UTC, Steven Schveighoffer wrote: in does not mean "take by reference", it means "scope const" I'm not sure that I really understand scope, but I read https://dlang.org/spec/function.html#param-storage as saying "in means take by value or reference

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

2020-09-25 Thread Steven Schveighoffer via Digitalmars-d-learn
On 9/25/20 10:12 AM, 60rntogo wrote: On Friday, 25 September 2020 at 13:15:27 UTC, Steven Schveighoffer wrote: ou can use auto ref to alleviate that: int x()(auto ref Foo f) // needs to be a template for auto ref to work That's a good point, thanks. Since we are on that topic, how would

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

2020-09-25 Thread 60rntogo via Digitalmars-d-learn
On Friday, 25 September 2020 at 13:15:27 UTC, Steven Schveighoffer wrote: I don't know, you can search for and report it here: https://issues.dlang.org I find it quite hard to search for anything here, but I couldn't find anything similar so I submitted a bug report. You can use auto ref

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

2020-09-25 Thread Steven Schveighoffer via Digitalmars-d-learn
On 9/25/20 3:43 AM, 60rntogo wrote: On Wednesday, 23 September 2020 at 19:27:13 UTC, Steven Schveighoffer wrote: This is a bug in the language. Is this a known bug? If not, it should be reported. I don't know, you can search for and report it here: https://issues.dlang.org I came up

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

2020-09-25 Thread 60rntogo via Digitalmars-d-learn
On Wednesday, 23 September 2020 at 19:27:13 UTC, Steven Schveighoffer wrote: This is a bug in the language. Is this a known bug? If not, it should be reported. I came up with an answer to my original question that sort of works: --- module foo; struct Foo { private int x; } int x(Foo

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

2020-09-23 Thread 60rntogo via Digitalmars-d-learn
On Wednesday, 23 September 2020 at 19:26:43 UTC, aliak wrote: Yeah, you can make a property setter: private void x(int newValue) { _x = newValue } I'm aware of this, but it does not achieve what I asked for. It only allows me to assign to _x, it doesn't give me a reference to x, so I cannot

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

2020-09-23 Thread Steven Schveighoffer via Digitalmars-d-learn
. Is the behavior that allows me to call the private method intended? This is such a blatant violation of encapsulation that it feels like a bug either in the language or the implementation. This is a bug in the language. Either varying ONLY by visibility of an overload should be disallowed, or you shouldn't

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

2020-09-23 Thread aliak via Digitalmars-d-learn
On Wednesday, 23 September 2020 at 19:27:13 UTC, Steven Schveighoffer wrote: This is a bug in the language. 勞

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

2020-09-23 Thread aliak via Digitalmars-d-learn
On Wednesday, 23 September 2020 at 18:38:53 UTC, 60rntogo wrote: There are really two questions here, one that I intended to ask and one that came out while I was trying to figure out the answer. Consider this simple code: [...] Yeah, you can make a property setter: private void x(int

Is it possible to "overload" based on visibility?

2020-09-23 Thread 60rntogo via Digitalmars-d-learn
; private ref int x() return { writeln("ref int"); return _x; } int x() const { writeln("int"); return _x; } --- At first I didn't even expect this to compile, but it does and I can call these methods. I believe an overload is chosen based on the type qualifier of a Foo ins

Re: Overload comparison operators for "nullable" types

2020-08-01 Thread Oleg B via Digitalmars-d-learn
On Thursday, 30 July 2020 at 14:52:17 UTC, H. S. Teoh wrote: On Thu, Jul 30, 2020 at 01:41:05PM +, Oleg B via Digitalmars-d-learn wrote: [...] Logically we can compare versions, but what must return `opCmp` if one of versions has 'not comparible' state? [...] opCmp is allowed to return

Re: Overload comparison operators for "nullable" types

2020-07-30 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Jul 30, 2020 at 01:41:05PM +, Oleg B via Digitalmars-d-learn wrote: [...] > Logically we can compare versions, but what must return `opCmp` if one of > versions has 'not comparible' state? [...] opCmp is allowed to return float; so you could return float.nan in this case. T --

Overload comparison operators for "nullable" types

2020-07-30 Thread Oleg B via Digitalmars-d-learn
mparible' state before `a <= b` is not intuitive too. `opBinary` can't overload comparison. We have same type of problem if wrap simple `double` into struct and try overload comparison (double.nan has own behavior). Problem can be solved if we add new `opCmpEx` into language spec that ret

Re: Overload function template for rectangular array

2020-05-25 Thread Ali Çehreli via Digitalmars-d-learn
On 5/25/20 1:20 AM, John Chapman wrote: void foo(T)(T[] a) {} void foo(T)(T[][] a) {} auto ra = new int[][](5, 5); ra.foo(); // matches both import std.traits; void foo(T)(T[] a) if (!isArray!T) {} void foo(T)(T[] a) if (isArray!T) {} Or you can take T as parameter and check ElementType!T:

Overload function template for rectangular array

2020-05-25 Thread John Chapman via Digitalmars-d-learn
Is it possible to overload a function template for rectangular arrays? Is there any way to tell them apart from normal ones? void foo(T)(T[] a) {} void foo(T)(T[][] a) {} auto ra = new int[][](5, 5); ra.foo(); // matches both Thanks for any hints.

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.

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

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 }

  1   2   3   4   >