method-overloading

C# derived classes, overload resolution

Ok, I have an some different objects that are derived from a base class and I've put a bunch of them in a list. I want to loop through the list and push each to a method. I have separate methods with each one's type signature, but the compiler is complaining. Can someone explain why? Is this an opportunity to use Generics, and if so,...

Python method overload based on argument count?

If I call QApplication's init without arguments i get TypeError: arguments did not match any overloaded call: QApplication(list-of-str): not enough arguments QApplication(list-of-str, bool): not enough arguments QApplication(list-of-str, QApplication.Type): not enough arguments QApplication(Display, int visual=0, int colormap=0...

How can I differentiate between def foo[A](xs: A*) and def foo[A, B](xs: (A, B)*)?

I know that type erasure makes them look equal, type-wise, at runtime, so that: class Bar { def foo[A](xs: A*) { xs.foreach(println) } def foo[A, B](xs: (A, B)*) { xs.foreach(x => println(x._1 + " - " + x._2)) } } gives the following compiler error: <console>:7: error: double definition: method foo:[A,B](xs: (A, B)*)Unit a...

C# Action and Func parameter overloads

I need a method that takes an Action (or a Func), but the Action has a mixed number of parameters. What is the most straight forward and compact way to implement these overloads: public void Execute<T>(Action<T> action, T param) { // TODO: Implement something like: // Execute(action, param, null); } public void Execute<T1,T2>(A...

Best way to emulate Ruby "splat" operator in PHP function signatures [Method overloading]

In Ruby def my_func(foo,bar,*zim) [foo, bar, zim].collect(&:inspect) end puts my_func(1,2,3,4,5) # 1 # 2 # [3, 4, 5] In PHP (5.3) function my_func($foo, $bar, ... ){ #... } What's the best way to to do this in PHP? ...

What is the correct syntax for calling an overloaded method using 'this()' in C#?

I may have this wrong, but I've seen the way of creating an overloaded method that calls itself in the definition. It's something like: public void myFunction(int a, int b) { //Some code here } public void myFunction(int a) : this (a, 10) { } This is not the correct syntax, I know, but I can't find the correct syntax anywhere for...

Method overloading standard protocol?

If you add a third signature for a method, do you make the second and third variations directly call the first (the implemented variation), or do you make the third call the second and the second call the first. It would seem to me that the extra method call would be overhead you could live without, so you'd want all the methods to call...

Can I pass a primitive type by reference in Java?

I would like to call a method which could potentially take on different versions, i.e. the same method for input parameters that are of type: boolean byte short int long The way I would like to do this is by "overloading" the method (I think that is the correct term?): public void getValue(byte theByte) {...} public void getValue(sh...

How to make a method take 2 different types

I know this is probably a really simple question but I'm having a brain fart at the moment. I am trying to create a method that can take one of 2 custom types. Basically the body of this method will be identical for both the types as they both have a Name property (I'm doing a comparison on the Name property to use in sorting). How shoul...

Why does Guava's ImmutableList have so many overloaded of() methods?

I was just looking at Guava's ImmutableList and I noticed that the of() method was overloaded 12 times. It looks to me that all they needed was: static <E> ImmutableList<E> of(); static <E> ImmutableList<E> of(E element); // not even necessary static <E> ImmutableList<E> of(E... elements); What's the reason for having so many similar...

How to implement optional parentheses during function call? (function overloading)

My guess is that this is not possible, but I'd like f and f() to do the same thing. var f = function(str){ console.log(str||'foo'); }(); f; // wanted output: 'foo' f(); // wanted output: 'foo' f('bar'); // wanted output: 'bar' Because f is no longer a function definit...

Generic type of a child class doesn't allow parents

Given a structure like this: class Parent { } class Child : Parent { } I have a method that takes a generic type with a constraint that the object is of type Child static void doSomething<T>() where T : Child { if (typeof(T) == typeof(Parent)) { /* ... */ } else if (typeof(T) == typeof(Child)) { /...

node.js - overloading functions

Is there a way to overload functions in node.js similar to _noSuchMethod_ https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/noSuchMethod? Thanks ...

C# Generic overloading of List<T> : How would this be done?

The StringBuilder class allows you, in what I consider to be a very intuitive way, to chain method calls to .Append(), .AppendFormat() and some others like so: StringBuilder sb = new StringBuilder(); sb.Append("first string") .Append("second string); The List class' .Add() method, on the other hand, returns void - so chaining calls ...

Overloading virtual functions in two different interfaces

I have an issue where I have an interface that has parts that make sense as templated, and parts that make sense as not-templated. I'm in the middle of a refactor where I'm splitting that out into two different interfaces where the more specific (the templated one) inherits from the other one. E.g., say I have an interface IArray, this...

Sort list by property/anonymous function?

I've got a list defined like this... var sets = new List<HashSet<int>>(numSets); Why isn't there an overload so I can sort it like this? sets.Sort(s => s.Count); I want the largest set first. What's the easiest way to do that? ...

Why overload a function with fewer / greater arguments?

So I am a java programmer and I know what overloading a function means. Moreover, I have overloaded a function with different type of arguments, and can overload with, fewer and greater arguments. I was asked this on an interview. I really don't know if this has any benefits or what the interviewer was getting at here. Does it have any...

Unable to resolve overloaded class methods in template delegate.

Background: I'm using a delegation technique to abstract access to arbitrary object methods, but I'm having some issues where the linker is concerned. Consider the following class, ContextNode. template <class ObjectType, class GetType, class SetType> class ContextNode: public ContextNodeBase { public: ContextNode(ObjectType* tar...

In Java, How do I return a String or a double depending on the circumstance of the method? Is it possible?

For example, I have a method that looks through a string for data separated by a specified deliminator, but some items might be a names, and other items might be numbers. If a user calls my method to return item number X from the deliminated list, i want it to return a string if item X is a name, or a double if item X is a number. For ...

Cost of using params in C#

Does anyone have advice for using the params in C# for method argument passing. I'm contemplating making overloads for the first 6 arguments and then a 7th using the params feature. My reasoning is to avoid the extra array allocation the params feature require. This is for some high performant utility methods. Any advice? Is it a waste o...