Re: Operator Overloading with multiple return types

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

Re: Operator Overloading with multiple return types

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

Re: Operator Overloading with multiple return types

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

Re: Operator Overloading with multiple return types

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

Re: Operator Overloading with multiple return types

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

Re: Operator Overloading with multiple return types

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

Re: Operator Overloading with multiple return types

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

Re: Operator Overloading with multiple return types

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

Operator Overloading with multiple return types

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

Re: Multiple return type or callback function

2017-01-23 Thread Basile B. via Digitalmars-d-learn
On Monday, 23 January 2017 at 15:15:35 UTC, aberba wrote: I'm creating a function to authenticate user login. I want to determine login failure (Boolean) and error message (will be sent to frontend) but D does have multiple return type [...] Yes, MRV can be done with a tuple auto foo

Re: Multiple return type or callback function

2017-01-23 Thread pineapple via Digitalmars-d-learn
On Monday, 23 January 2017 at 15:15:35 UTC, aberba wrote: I'm creating a function to authenticate user login. I want to determine login failure (Boolean) and error message (will be sent to frontend) but D does have multiple return type (IMO could use struct but will make code dirty with too

Multiple return type or callback function

2017-01-23 Thread aberba via Digitalmars-d-learn
I'm creating a function to authenticate user login. I want to determine login failure (Boolean) and error message (will be sent to frontend) but D does have multiple return type (IMO could use struct but will make code dirty with too much custom types). struct Result { bool success

Multiple return type from object factory possible?

2013-06-22 Thread deed
class A { ... } class NonContainer : A { ... } class Container : A { A[] container; } class NC1 : NonContainer {} ... class C1 : Container {} ... A getO(string info) { switch (info) { default : return new NonContainer(); case info1: return new

Re: Multiple return type from object factory possible?

2013-06-22 Thread Simen Kjaeraas
On Sat, 22 Jun 2013 18:58:22 +0200, deed n...@none.none wrote: class A { ... } class NonContainer : A { ... } class Container : A { A[] container; } class NC1 : NonContainer {} ... class C1 : Container {} ... A getO(string info) { switch (info) {

Re: Multiple return type from object factory possible?

2013-06-22 Thread deed
auto o = getO(info3); if (cast(Container)o != null) { ... } Thanks Simen. (Compiler requires !is instead of !=)

Re: Optional extra return value? Multiple return values with auto?

2012-08-15 Thread ReneSac
On Wednesday, 15 August 2012 at 01:22:41 UTC, Era Scarecrow wrote: On Wednesday, 15 August 2012 at 00:37:32 UTC, ReneSac wrote: And my last question of my first post: I can't use auto for the out values right? An enhancement proposal like this would be compatible with D? I would say No.

Re: Optional extra return value? Multiple return values with auto?

2012-08-14 Thread ReneSac
Thanks, this indeed works. One obvious (when your program starts to behave weirdly...) down side of this solution: it needs a different dummy for each optional out value of a function, or else multiple variables will be modifying the same dummy. And, of course, a different dummy for each type

Re: Optional extra return value? Multiple return values with auto?

2012-08-14 Thread Era Scarecrow
On Wednesday, 15 August 2012 at 00:37:32 UTC, ReneSac wrote: And my last question of my first post: I can't use auto for the out values right? An enhancement proposal like this would be compatible with D? I would say No. Maybe if it was a union, but I don't think so;.It still needs to

Re: Optional extra return value? Multiple return values with auto?

2012-08-14 Thread Ali Çehreli
On 08/14/2012 06:22 PM, Era Scarecrow wrote: On Wednesday, 15 August 2012 at 00:37:32 UTC, ReneSac wrote: And my last question of my first post: I can't use auto for the out values right? An enhancement proposal like this would be compatible with D? I would say No. Maybe if it was a

Re: Optional extra return value? Multiple return values with auto?

2012-08-14 Thread Era Scarecrow
On Wednesday, 15 August 2012 at 01:42:11 UTC, Ali Çehreli wrote: Agreed. The function code must be compiled to use certain amount of data from the program stack for that particular parameter. That size of that parameter must be known at compile time. The compiler could in theory examine the

Re: Optional extra return value? Multiple return values with auto?

2012-08-11 Thread ReneSac
On Tuesday, 24 July 2012 at 05:30:49 UTC, Ali Çehreli wrote: The options that I can think of: - Return a struct (or a class) where one of the members is not filled-in - Similarly, return a tuple This is awkward, and doesn't look good for performance. - Use an out parameter, which can

Re: Optional extra return value? Multiple return values with auto?

2012-08-11 Thread Ali Çehreli
On 08/11/2012 03:48 PM, ReneSac wrote: On Tuesday, 24 July 2012 at 05:30:49 UTC, Ali Çehreli wrote: - Use an out parameter, which can have a default lvalue: int g_default_param; void foo(ref int i = g_default_param) { if (i == g_param) { // The caller is not interested in 'i' } else {

Re: Optional extra return value? Multiple return values with auto?

2012-08-11 Thread Timon Gehr
There is no compiler bug. You cannot pass immutable/rvalue by reference to mutable.

Re: Optional extra return value? Multiple return values with auto?

2012-07-24 Thread Chris NS
On Tuesday, 24 July 2012 at 03:25:55 UTC, ReneSac wrote: Do I really have to duplicate the function, in order to achieve this? In a nutshell, yes. Or else resort to bizarre sorcery such as may rot the very heart from one's chest (or template ninjitsu, whatever). But is it really so

Re: Optional extra return value? Multiple return values with auto?

2012-07-24 Thread David
Am 24.07.2012 05:25, schrieb ReneSac: I whish there was: auto foo() { return Tuple!(foo, bar, 1, new Custum()); } void main() { auto (s1, s2, i, c) = foo(); }

Re: Optional extra return value? Multiple return values with auto?

2012-07-24 Thread bearophile
On Tuesday, 24 July 2012 at 03:25:55 UTC, ReneSac wrote: How I can return multiple values in D, but one of them being optional? One of the ways to to it is to return a tuple with your arguments, where the last item of the tuple is a Nullable of the optional element: import std.stdio,

Re: Optional extra return value? Multiple return values with auto?

2012-07-24 Thread Chris NS
On Tuesday, 24 July 2012 at 08:56:21 UTC, David wrote: Am 24.07.2012 05:25, schrieb ReneSac: I whish there was: auto foo() { return Tuple!(foo, bar, 1, new Custum()); } void main() { auto (s1, s2, i, c) = foo(); } I think the main blocker to something like that right now is the

Re: Optional extra return value? Multiple return values with auto?

2012-07-24 Thread bearophile
It seems forum.dlang.org hides my first answer, I don't know why: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learnarticle_id=37708 Bye, bearophile

Optional extra return value? Multiple return values with auto?

2012-07-23 Thread ReneSac
How I can return multiple values in D, but one of them being optional? I tried the 'out' hack to achieve multiple return values, but it didn't accepted a default argument: it needed a lvalue in the calling function. In Lua, for example, one can do: function foo(input) -- calculations

Re: Optional extra return value? Multiple return values with auto?

2012-07-23 Thread Ali Çehreli
On 07/23/2012 08:25 PM, ReneSac wrote: How I can return multiple values in D, but one of them being optional? I tried the 'out' hack to achieve multiple return values, but it didn't accepted a default argument: it needed a lvalue in the calling function. Like in C and C++, functions in D can

Re: multiple return

2011-04-20 Thread %u
thanks you all, it works. last thing, I have this Tuple!(int,int,int)(1, 2, 3) how can I use the return values individual? to be more clear if I rturn tuple(a, b, c) can I write in the code void main() { //call the function here writeln(a); }

Re: multiple return

2011-04-20 Thread bearophile
%u: how can I use the return values individual? One of the simpler ways is to think of a tuple as an array, and use sometuple[0] to take its first item, etc. But your tuple indexes need to be compile-time constants. Bye, bearophile

multiple return

2011-04-19 Thread %u
I have function which have more than one return, and the code compile and run but it gives rong result -I guess-, so i use tuple but the compiler can't return tuple. how can I return values? why I can't return tuple?

Re: multiple return

2011-04-19 Thread bearophile
%u: I have function which have more than one return, and the code compile and run but it gives rong result -I guess-, so i use tuple but the compiler can't return tuple. how can I return values? why I can't return tuple? Currently in D there are two ways to return multiple values from a

Re: multiple return

2011-04-19 Thread Max Klyga
On 2011-04-20 01:35:46 +0300, %u said: I have function which have more than one return, and the code compile and run but it gives rong result -I guess-, so i use tuple but the compiler can't return tuple. how can I return values? why I can't return tuple? In D, tuple is not built in type, it