Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #85515
| From | Lynn McGuire <lynnmcguire5@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Function overloading |
| Date | 2022-07-20 19:22 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <tba68s$1uiit$1@dont-email.me> (permalink) |
| References | <tb9ssj$s32$1@gioia.aioe.org> |
On 7/20/2022 4:42 PM, Cholo Lennon wrote:
> I am learning Rust. One missing C++/Java feature is function
> overloading. One of the explanations of this decision is that "function
> overloading is an anti-pattern", because, among other things, "it leads
> to less readable and understandable code".
>
> This surprised me because I consider it very useful. I really hate
> having to code multiple functions with different names and almost the
> same functionality. I suffered this in Python (its solution based in
> decorators is not the best), and also in Typescript (its solution based
> in several prototypes for a single function is really awful).
>
> What is your opinion about the function overloading as an
> "anti-pattern"? is it?
>
> Disclaimer: "Overloading" where only types differ (but the number of
> parameters are the same) is possible in Rust using generic traits,
> something like the following:
>
> struct MyPrinter;
>
> trait Printer<T> {
> fn print(&self, value: T);
> }
>
> impl Printer<i32> for MyPrinter {
> fn print(&self, value: i32) {
> println!("Value: {}", value);
> }
> }
>
> impl Printer<&str> for MyPrinter {
> fn print(&self, value: &str) {
> println!("Value: {}", value);
> }
> }
>
> fn main() {
> let foo = MyPrinter;
>
> foo.print(10);
> foo.print("Hello");
> }
>
>
> --
> Cholo Lennon
> Bs.As.
> ARG
I use function overloading extensively in my C++ code code. Works
great and it is useful to have the same name with different arguments in
many classes. Here is one of my helper functions, tuple:
std::vector <int> tuple ();
std::vector <int> tuple (int int1);
std::vector <int> tuple (int int1, int int2);
std::vector <int> tuple (int int1, int int2, int int3);
std::vector <int> tuple (int int1, int int2, int int3, int int4);
std::vector <int> tuple (int int1, int int2, int int3, int int4, int int5);
std::vector <int> tuple (int int1, int int2, int int3, int int4, int
int5, int int6);
std::vector <int> tuple (int int1, int int2, int int3, int int4, int
int5, int int6, int int7);
std::vector <int> tuple (int int1, int int2, int int3, int int4, int
int5, int int6, int int7, int int8);
std::vector <int> tuple (int int1, int int2, int int3, int int4, int
int5, int int6, int int7, int int8, int int9);
std::vector <int> tuple (int int1, int int2, int int3, int int4, int
int5, int int6, int int7, int int8, int int9, int int10);
and so on to int60. I also have tupleString and tupleTuple.
I also have the following methods in my DesValue class:
virtual void setValue (int aValue);
virtual void setValue (double aValue);
virtual void setValue (char * aValue);
virtual void setValue (std::string &aValue);
virtual void setValue (std::vector <int> aValue);
virtual void setValue (std::vector <double> aValue);
virtual void setValue (std::vector <std::string> aValue);
virtual void setString ( std::string aString );
virtual void setString ( std::vector <std::string> aString );
Lynn
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Function overloading Cholo Lennon <chololennon@hotmail.com> - 2022-07-20 18:42 -0300
Re: Function overloading Lynn McGuire <lynnmcguire5@gmail.com> - 2022-07-20 19:22 -0500
Re: Function overloading Gawr Gura <gawrgura@mail.hololive.com> - 2022-07-20 22:44 -0700
Re: Function overloading Lynn McGuire <lynnmcguire5@gmail.com> - 2022-07-21 16:17 -0500
Re: Function overloading Cholo Lennon <chololennon@hotmail.com> - 2022-07-22 19:43 -0300
Re: Function overloading Lynn McGuire <lynnmcguire5@gmail.com> - 2022-07-22 18:38 -0500
Re: Function overloading Cholo Lennon <chololennon@hotmail.com> - 2022-07-24 01:24 -0300
Re: Function overloading Lynn McGuire <lynnmcguire5@gmail.com> - 2022-07-25 16:01 -0500
Re: Function overloading "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-07-26 00:05 +0200
Re: Function overloading Cholo Lennon <chololennon@hotmail.com> - 2022-07-30 18:04 -0300
Re: Function overloading Cholo Lennon <chololennon@hotmail.com> - 2022-07-30 18:03 -0300
Re: Function overloading Lynn McGuire <lynnmcguire5@gmail.com> - 2022-08-02 23:11 -0500
Re: Function overloading Bonita Montero <Bonita.Montero@gmail.com> - 2022-07-27 16:10 +0200
Re: Function overloading Bonita Montero <Bonita.Montero@gmail.com> - 2022-07-27 16:11 +0200
Re: Function overloading Bonita Montero <Bonita.Montero@gmail.com> - 2022-07-27 16:35 +0200
Re: Function overloading Bonita Montero <Bonita.Montero@gmail.com> - 2022-07-28 05:51 +0200
Re: Function overloading Lynn McGuire <lynnmcguire5@gmail.com> - 2022-07-27 14:54 -0500
Re: Function overloading Bonita Montero <Bonita.Montero@gmail.com> - 2022-07-28 05:43 +0200
Re: Function overloading Lynn McGuire <lynnmcguire5@gmail.com> - 2022-07-28 13:32 -0500
Re: Function overloading Lynn McGuire <lynnmcguire5@gmail.com> - 2022-07-27 15:01 -0500
Re: Function overloading Juha Nieminen <nospam@thanks.invalid> - 2022-07-21 05:45 +0000
Re: Function overloading Paavo Helde <eesnimi@osa.pri.ee> - 2022-07-21 09:04 +0300
Re: Function overloading Juha Nieminen <nospam@thanks.invalid> - 2022-07-21 07:16 +0000
Re: Function overloading Lynn McGuire <lynnmcguire5@gmail.com> - 2022-07-21 16:22 -0500
csiph-web