Function Overloading

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

Local function overloading

2014-04-12 Thread Philpax
While trying to overload a function in local/function scope, I ran into this behaviour: http://dpaste.dzfl.pl/b4e8b9ddf78a and I was wondering what the cause was. As far as I can tell, this should be fine in global scope (and it is), but I'm curious as to why it doesn't work inside a function.

Local function overloading

2014-04-13 Thread Philpax
Thanks! I used the static struct solution. I did some quick research, and I think that the reason why the original code doesn't work is because the two functions have the same identifier, which results in Dsymboltable::insert rejecting the second function. I haven't tested this hypothesis, but

interface function overloading

2011-01-08 Thread %u
Isn't it possible to have a hierarchy in interface definitions such that it is possible to overload according to best interface match? This now won't compile due to multiple matches. module main; interface I1{} interface I2 : I1{} class C : I2{ this(){} } void func(I1 i){} void func(I2

nested function overloading

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

Re: Function Overloading

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

Re: Function Overloading

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

Re: Function Overloading

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

Re: Function Overloading

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

Re: Local function overloading

2014-04-12 Thread monarch_dodra
On Saturday, 12 April 2014 at 16:45:02 UTC, Philpax wrote: While trying to overload a function in local/function scope, I ran into this behaviour: http://dpaste.dzfl.pl/b4e8b9ddf78a and I was wondering what the cause was. As far as I can tell, this should be fine in global scope (and it is),

Re: Local function overloading

2014-04-12 Thread Andrej Mitrovic
On 4/12/14, monarch_dodra wrote: > I know you can workaround it by putting > your functions a static members of a dummy struct You can also use a mixin template: class A {} class B {} mixin template M() { void func2(A a) { } void func2(B b) { } } void main() { mixin M!(); func

Re: Local function overloading

2014-04-14 Thread Jonathan M Davis
On Sunday, April 13, 2014 08:40:17 Philpax wrote: > Is not being able to overload functions in local scope intended > behaviour? Yes. IIRC, I complained about it at one point, and Walter didn't like the idea of having overloaded nested functions. I don't remember what his reasoning was, but I do

Re: Local function overloading

2014-04-14 Thread monarch_dodra
On Monday, 14 April 2014 at 10:35:20 UTC, Jonathan M Davis wrote: On Sunday, April 13, 2014 08:40:17 Philpax wrote: Is not being able to overload functions in local scope intended behaviour? Yes. IIRC, I complained about it at one point, and Walter didn't like the idea of having overloaded ne

Function overloading between modules

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

Re: interface function overloading

2011-01-08 Thread Jonathan M Davis
On Saturday 08 January 2011 22:01:11 %u wrote: > Isn't it possible to have a hierarchy in interface definitions such that it > is possible to overload according to best interface match? > > This now won't compile due to multiple matches. > > > module main; > > interface I1{} > interface I2

Re: interface function overloading

2011-01-08 Thread %u
== Quote from Jonathan M Davis (jmdavisp...@gmx.com)'s article > On Saturday 08 January 2011 22:01:11 %u wrote: > > Isn't it possible to have a hierarchy in interface definitions such that it > > is possible to overload according to best interface match? > > > > This now won't compile due to multip

Re: interface function overloading

2011-01-09 Thread bearophile
%u: > func(cast(I2)(new C())); That code smells a bit (http://en.wikipedia.org/wiki/Code_smell ). Bye, bearophile

Re: interface function overloading

2011-01-09 Thread %u
== Quote from bearophile (bearophileh...@lycos.com)'s article > %u: > > func(cast(I2)(new C())); > That code smells a bit (http://en.wikipedia.org/wiki/Code_smell ). > Bye, > bearophile Extract the construction and you get: module main; interface I1{} interface I2 : I1{} class C : I2{

Re: interface function overloading

2011-01-09 Thread bearophile
%u: > What is the deeper problem in this little snippet? > Or do you mean there is something wrong if you encounter this pattern. You are right, sorry :-) Bye, bearophile

Re: interface function overloading

2011-01-12 Thread Stanislav Blinov
09.01.2011 15:22, %u пишет: == Quote from bearophile (bearophileh...@lycos.com)'s article %u: func(cast(I2)(new C())); That code smells a bit (http://en.wikipedia.org/wiki/Code_smell ). Bye, bearophile Extract the construction and you get: module main; interface I1{} interface I2 :

Re: interface function overloading

2011-01-12 Thread %u
== Quote from Stanislav Blinov (bli...@loniir.ru)'s article > In C++ I sometimes have similar problems, especially with multiple > inheritance. Though, as Jonathan mentioned, those problems are even more > annoying because of hijacking: you don't always immediately notice them. > Long ago I've deci

Re: nested function overloading

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

Re: nested function overloading

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

Re: nested function overloading

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

Re: nested function overloading

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

Re: nested function overloading

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

Re: nested function overloading

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

Re: nested function overloading

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

Another bug in function overloading?

2014-04-26 Thread Domain via Digitalmars-d-learn
module test; public interface I { void foo(); void foo(int); } public abstract class A : I { public void bar() { foo(); } public void foo(int i) { } } public class C : A { public void foo() { } public void bar2() { foo(1);

Re: Function overloading between modules

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

Re: Function overloading between modules

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

Re: Function overloading between modules

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

Re: Function overloading between modules

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

Re: Another bug in function overloading?

2014-04-26 Thread Jonathan M Davis via Digitalmars-d-learn
On Sat, 26 Apr 2014 06:55:38 + Domain via Digitalmars-d-learn wrote: > module test; > > public interface I > { > void foo(); > void foo(int); > } > > public abstract class A : I > { > public void bar() > { > foo(); > } > > public void foo(int i) >

Re: Another bug in function overloading?

2014-04-26 Thread Andrej Mitrovic via Digitalmars-d-learn
On 4/26/14, Jonathan M Davis via Digitalmars-d-learn wrote: > No. That's expected. I wonder whether a better diagnostic could help. But then again, maybe the hiding would be intentional and the diagnostic would be spurious/invalid. Not sure..

cross_module function overloading & alias & template: how to ?

2016-11-10 Thread Picaud Vincent via Digitalmars-d-learn
Hi All, In my adventure to learn a little bit of D coming from C++ I am now faced with the following problem: I have read about "cross-module overloading", §5.5.2 page 146 of Andrei Alexandrescu book. That makes sense to me and this is interesting. As a concrete example here the scenario: I

Re: cross_module function overloading & alias & template: how to ?

2016-11-10 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, November 10, 2016 15:46:11 Picaud Vincent via Digitalmars-d- learn wrote: > ---> What am I missing? What is the right way to do that? Honestly, I'm surprised that the compiler let you alias std.algorithm.comparison.min, because it's a templated function, and in the case of templat

Re: cross_module function overloading & alias & template: how to ?

2016-11-10 Thread Picaud Vincent via Digitalmars-d-learn
On Thursday, 10 November 2016 at 17:12:32 UTC, Jonathan M Davis wrote: On Thursday, November 10, 2016 15:46:11 Picaud Vincent via Digitalmars-d- learn wrote: [...] Honestly, I'm surprised that the compiler let you alias std.algorithm.comparison.min, because it's a templated function, and in t

Re: cross_module function overloading & alias & template: how to ?

2016-11-10 Thread Steven Schveighoffer via Digitalmars-d-learn
On 11/10/16 12:12 PM, Jonathan M Davis via Digitalmars-d-learn wrote: On Thursday, November 10, 2016 15:46:11 Picaud Vincent via Digitalmars-d- learn wrote: ---> What am I missing? What is the right way to do that? Honestly, I'm surprised that the compiler let you alias std.algorithm.compa

Re: cross_module function overloading & alias & template: how to ?

2016-11-10 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, November 10, 2016 14:32:28 Steven Schveighoffer via Digitalmars-d-learn wrote: > On 11/10/16 12:12 PM, Jonathan M Davis via Digitalmars-d-learn wrote: > > On Thursday, November 10, 2016 15:46:11 Picaud Vincent via > > Digitalmars-d- > > > > learn wrote: > >> ---> What am I missing

Re: cross_module function overloading & alias & template: how to ?

2016-11-10 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, November 10, 2016 17:41:02 Picaud Vincent via Digitalmars-d- learn wrote: > It is certainly a compiler problem: I used gdc -> compile error, > but with dmd it compiles and runs fine. Full details in the git > repo. Don't bother with gdc at this point. Unless there's a development vers

Re: cross_module function overloading & alias & template: how to ?

2016-11-10 Thread Picaud Vincent via Digitalmars-d-learn
On Thursday, 10 November 2016 at 20:12:10 UTC, Jonathan M Davis wrote: On Thursday, November 10, 2016 17:41:02 Picaud Vincent via Digitalmars-d- learn wrote: It is certainly a compiler problem: I used gdc -> compile error, but with dmd it compiles and runs fine. Full details in the git repo.

Virtual value types during compile-time for static type safety, static optimizations and function overloading.

2015-07-17 Thread Tamas via Digitalmars-d-learn
I got inspired by Andrei's "Generic Programming Must Go" talk: https://www.youtube.com/watch?v=mCrVYYlFTrA I.e. writing functions with static if-s, based on what we know about the input. So the question is how to annotate the input variables? Adam showed an excellent solution for this, by wrap

Re: Virtual value types during compile-time for static type safety, static optimizations and function overloading.

2015-07-17 Thread ZombineDev via Digitalmars-d-learn
On Friday, 17 July 2015 at 21:20:41 UTC, Tamas wrote: Is there a solution that results the same static optimizations, but has no runtime penalty, i.e. the functions just operates with ints? (At least when compiled) Did you try looking at assembly generated by GDC or LDC with full optimization

Re: Virtual value types during compile-time for static type safety, static optimizations and function overloading.

2015-07-17 Thread ZombineDev via Digitalmars-d-learn
On Friday, 17 July 2015 at 23:15:31 UTC, ZombineDev wrote: On Friday, 17 July 2015 at 21:20:41 UTC, Tamas wrote: Is there a solution that results the same static optimizations, but has no runtime penalty, i.e. the functions just operates with ints? (At least when compiled) Did you try looking

Re: Virtual value types during compile-time for static type safety, static optimizations and function overloading.

2015-07-17 Thread Tamas via Digitalmars-d-learn
On Friday, 17 July 2015 at 23:16:51 UTC, ZombineDev wrote: On Friday, 17 July 2015 at 23:15:31 UTC, ZombineDev wrote: On Friday, 17 July 2015 at 21:20:41 UTC, Tamas wrote: Is there a solution that results the same static optimizations, but has no runtime penalty, i.e. the functions just operat

Re: Virtual value types during compile-time for static type safety, static optimizations and function overloading.

2015-07-17 Thread Baz via Digitalmars-d-learn
On Friday, 17 July 2015 at 21:20:41 UTC, Tamas wrote: Although this code is fully operational, presents nice api and compile-time optimizations, the extra Struct wrapper is not without runtime penalty. Is there a solution that results the same static optimizations, but has no runtime penalty,

Re: Virtual value types during compile-time for static type safety, static optimizations and function overloading.

2015-07-17 Thread Adam D. Ruppe via Digitalmars-d-learn
On Friday, 17 July 2015 at 23:44:25 UTC, Tamas wrote: I used DMD, BTW. what compile flags?

Re: Virtual value types during compile-time for static type safety, static optimizations and function overloading.

2015-07-18 Thread Tamas via Digitalmars-d-learn
I made a thorough comparison using multiple compilers and a summary of the findings. In short, there is a runtime overhead. I reduced the code to cut out the imports and made two versions with equivalent semantic content. positive0.d contains the hand written specializations of the abs functio

Re: Virtual value types during compile-time for static type safety, static optimizations and function overloading.

2015-07-18 Thread Tamas via Digitalmars-d-learn
Sorry, the main function of positive0.d correctly looks like this: int main() { return !((abs(-16) == 16) && (abs(3) == 3) && (square(5).absPositive == 25) && (square(-4).absPositive == 16)); } But this does not affect the results, the asm file sizs or the asm abs function bodies.

Re: Virtual value types during compile-time for static type safety, static optimizations and function overloading.

2015-07-18 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 18 July 2015 at 10:06:07 UTC, Tamas wrote: Compile & execute: $ dmd positive0.d; ./positive0; echo $? $ ldc2 positive0.d; ./positive0; echo $? Try adding the automatic optimize flags in all your cases. For dmd, `-O -inline`. Not sure about ldc but I think it is `-O` as well.

Re: Virtual value types during compile-time for static type safety, static optimizations and function overloading.

2015-07-18 Thread Tamas via Digitalmars-d-learn
On Saturday, 18 July 2015 at 13:16:26 UTC, Adam D. Ruppe wrote: On Saturday, 18 July 2015 at 10:06:07 UTC, Tamas wrote: Compile & execute: $ dmd positive0.d; ./positive0; echo $? $ ldc2 positive0.d; ./positive0; echo $? Try adding the automatic optimize flags in all your cases. For dmd, `-O -