overloading

Why Typescript doesn't support function overloading in a right way?

做~自己de王妃 提交于 2020-07-06 11:05:39
问题 There are a lot of questions about how function overloading works in Typescript, (for instance, TypeScript function overloading). But there are no questions like 'why does it work in that way?' Now function overloading looks like that: function foo(param1: number): void; function foo(param1: number, param2: string): void; function foo(...args: any[]): void { if (args.length === 1 && typeof args[0] === 'number') { // implementation 1 } else if (args.length === 2 && typeof args[0] === 'number'

Function overloading in Dart

十年热恋 提交于 2020-07-04 20:25:06
问题 The following code: class Tools { static int roll(int min, int max) { // IMPLEMENTATION } static int roll(List<int> pair) { // IMPLEMENTATION } } renders a The name 'roll' is already defined error on the second roll function. How come? Since the parameters of the functions are distinct, shouldn't polymorphism apply? Edit. Corrected title in order to better reflect the subject. 回答1: What your code demonstrates is function overloading and not related to polymorphism. Function overloading is not

Method oveloading - Object vs Object vararg

给你一囗甜甜゛ 提交于 2020-07-03 07:03:09
问题 See the code below: // 1st method private static void method(Object o){ System.out.println("object method"); } // 2nd method private static void method(Object... o){ System.out.println("object vararg method"); } public static void main(String [] ar){ method(null); // 1st call Integer value=null; method(value); // 2nd call } I expected 1st call and 2nd call both should invoke 1st method , thought null will prefer to match Object than Object... vararg. But I am wrong. 1st call invoked 2nd

Method oveloading - Object vs Object vararg

北城余情 提交于 2020-07-03 06:59:45
问题 See the code below: // 1st method private static void method(Object o){ System.out.println("object method"); } // 2nd method private static void method(Object... o){ System.out.println("object vararg method"); } public static void main(String [] ar){ method(null); // 1st call Integer value=null; method(value); // 2nd call } I expected 1st call and 2nd call both should invoke 1st method , thought null will prefer to match Object than Object... vararg. But I am wrong. 1st call invoked 2nd

C++ function template overload on template parameter

本小妞迷上赌 提交于 2020-06-14 07:14:03
问题 Should it be possible to overload function template like this (only on template parameter using enable_if): template <class T, class = std::enable_if_t<std::is_arithmetic<T>::value>> void fn(T t) { } template <class T, class = std::enable_if_t<!std::is_arithmetic<T>::value>> void fn(T t) { } if conditions in enable_if don't overlap? My MSVS compiler complains, that 'void fn(T)' : function template has already been defined . If not, what is the alternative (ideally not putting enable_if

C++ function template overload on template parameter

回眸只為那壹抹淺笑 提交于 2020-06-14 07:13:11
问题 Should it be possible to overload function template like this (only on template parameter using enable_if): template <class T, class = std::enable_if_t<std::is_arithmetic<T>::value>> void fn(T t) { } template <class T, class = std::enable_if_t<!std::is_arithmetic<T>::value>> void fn(T t) { } if conditions in enable_if don't overlap? My MSVS compiler complains, that 'void fn(T)' : function template has already been defined . If not, what is the alternative (ideally not putting enable_if

C++ Constructor: Perfect forwarding and overload

浪尽此生 提交于 2020-06-10 04:39:14
问题 I have two classes, A and B, and B is derived from A. A has multiple constructors (2 in the example below). B has one additional member to initialize (which has a default initializer). How can I achieve that B can be construced using one of the constructors of A without having to manually rewrite all the constructor overloads from A in B? (In the example below, I would otherwise have to provide four constructors for B: B():A(){} , B(string s):A(s){} , B(int b):A(),p(b){} , B(string s, int b)

C++ Constructor: Perfect forwarding and overload

六眼飞鱼酱① 提交于 2020-06-10 04:39:07
问题 I have two classes, A and B, and B is derived from A. A has multiple constructors (2 in the example below). B has one additional member to initialize (which has a default initializer). How can I achieve that B can be construced using one of the constructors of A without having to manually rewrite all the constructor overloads from A in B? (In the example below, I would otherwise have to provide four constructors for B: B():A(){} , B(string s):A(s){} , B(int b):A(),p(b){} , B(string s, int b)

overloading methods in Java [duplicate]

对着背影说爱祢 提交于 2020-05-27 05:11:31
问题 This question already has answers here : The relationship of overload and method return type in Java? (4 answers) Closed 3 years ago . What conditions must be met so that two methods correctly qualify as overloaded methods? Is it that two methods must at least differ in their list of arguments such as public void A() { //... } public void A(int val) { //.. } Hence, a mere change in return-type and/or access modifier wont make two overloaded methods? 回答1: You are right. Return types and access

overloading methods in Java [duplicate]

自古美人都是妖i 提交于 2020-05-27 05:11:08
问题 This question already has answers here : The relationship of overload and method return type in Java? (4 answers) Closed 3 years ago . What conditions must be met so that two methods correctly qualify as overloaded methods? Is it that two methods must at least differ in their list of arguments such as public void A() { //... } public void A(int val) { //.. } Hence, a mere change in return-type and/or access modifier wont make two overloaded methods? 回答1: You are right. Return types and access