overloading

How do you override the ruby case equality operator? (===)

眉间皱痕 提交于 2019-12-07 02:23:30
问题 I have a class that I want to compare to both strings and symbols in a case statement, so I thought that I just override the ===() method for my class and all would be gold. However my ===() method never gets called during the case statement. Any ideas? Here is some example code, and what happens in a irb session: class A def initialize(x) @x=x #note this isn't even required for this example end def ===(other) puts "in ===" return true end end irb(main):010:0> a=A.new("hi") => # irb(main):011

operator overloading for __truediv__ in python

♀尐吖头ヾ 提交于 2019-12-07 02:11:44
问题 I am trying to implement overloading for division operator in python. class Fraction: def __init__(self,top,bottom): def gcd(m, n): while m % n != 0: old_m = m old_n = n m = old_n n = old_m % old_n return n common = gcd(top,bottom) self.num = top/common self.den = bottom/common def __str__ (self): return str(self.num) + "/" + str(self.den) def get_num(self): return self.num def get_den(self): return self.den def __add__(self, other_fraction): new_num = self.num * other_fraction.den + self.den

c# generic method overload not consistent with abstract Visitor pattern

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 01:56:55
问题 experimenting with Visitor pattern and generic method I found a kind of discrepancy in C#.NET. AFAIK C# compiler prefers an explicit overload to a generic method, therefore the following code: public abstract class A { public abstract void Accept(Visitor v); } public class B : A { public override void Accept(Visitor v) { v.Visit(this); } } public class C : A { public override void Accept(Visitor v) { v.Visit(this); } } public class D : A { public override void Accept(Visitor v) { v.Visit(this

Typescript child class function overloading

只愿长相守 提交于 2019-12-07 01:38:43
问题 How can I achieve something similar to this pattern in typescript? class A { Init(param1: number) { // some code } } class B extends A { Init(param1: number, param2: string) { // some more code } } The code snipped above appears like it should work, however on close inspection of How Typescript function overloading works it makes sense that an error is thrown: TS2415: 'Class 'B' incorrectly extends base class 'A'. Types of property 'Init' are incompatible. I know that constructor functions

Overloaded virtual function call resolution

时光毁灭记忆、已成空白 提交于 2019-12-07 01:35:42
问题 Please consider the following code: class Abase{}; class A1:public Abase{}; class A2:public A1{}; //etc class Bbase{ public: virtual void f(Abase* a); virtual void f(A1* a); virtual void f(A2* a); }; class B1:public Bbase{ public: void f(A1* a); }; class B2:public Bbase{ public: void f(A2* a); }; int main(){ A1* a1=new A1(); A2* a2=new A2(); Bbase* b1=new B1(); Bbase* b2=new B2(); b1->f(a1); // calls B1::f(A1*), ok b2->f(a2); // calls B2::f(A2*), ok b2->f(a1); // calls Bbase::f(A1*), ok b1->f

<?= operator C++ greater less question mark equals sign

て烟熏妆下的殇ゞ 提交于 2019-12-07 00:50:10
问题 I saw <?= and >?= used in a code: http://community.topcoder.com/stat?c=problem_solution&rm=151152&rd=5854&pm=2923&cr=310333 I tried to compile without the includes to test if it's standard, but it didn't work. I then added the includes, but it still gives the same error: question-mark.cpp:15:5: error: expected primary-expression before ‘?’ token question-mark.cpp:15:6: error: expected primary-expression before ‘=’ token question-mark.cpp:15:9: error: expected ‘:’ before ‘;’ token question

C# overloading with generics: bug or feature?

天涯浪子 提交于 2019-12-06 22:12:39
问题 Let's have a following simplified example: void Foo<T>(IEnumerable<T> collection, params T[] items) { // ... } void Foo<C, T>(C collection, T item) where C : ICollection<T> { // ... } void Main() { Foo((IEnumerable<int>)new[] { 1 }, 2); } Compiler says: The type 'System.Collections.Generic.IEnumerable' cannot be used as type parameter 'C' in the generic type or method 'UserQuery.Foo(C, T)'. There is no implicit reference conversion from 'System.Collections.Generic.IEnumerable' to 'System

Why is Scala's behavior in case of overloading with by-name parameters different from the case with by-value parameters?

寵の児 提交于 2019-12-06 21:18:23
问题 Given this Scala code: object test { def byval(a: Int) = println("Int") def byval(a: Long) = println("Long") def byname(a: => Int) = println("=> Int") def byname(a: => Long) = println("=> Long") def main(args: Array[String]) { byval(5) byname(5) } } the call byval(5) compiles correctly, but byname fails to compile: ambiguous reference to overloaded definition Why? I would expect to observe the same behavior for by-value and by-name parameters with respect to overloading… How can it be fixed?

Overloading a super class's function

自作多情 提交于 2019-12-06 20:59:37
问题 Is there something in the C++ standard that prevents me from overloading a super class's function? Starting with this pair of classes: class A { // super class int x; public: void foo (int y) {x = y;} // original definition }; class B : public A { // derived class int x2; public: void foo (int y, int z) {x2 = y + z;} // overloaded }; I can call B::foo() easily: B b; b.foo (1, 2); // [1] But if I try to call A::foo() ... B b; b.foo (12); // [2] ... I get a compiler error: test.cpp: In function

Is there any guarantee on the order of substitution in a function template after type deduction?

喜你入骨 提交于 2019-12-06 20:46:09
问题 Consider this function template: template<typename T> typename soft_error<T>::type foo(T, typename hard_error<T>::type) { } After deducing type T from the type of the first argument in the call to foo() , the compiler will proceed to substitute T and instantiate the function signature. If substitution for the return type gets executed first, causing a simple substitution failure, the compiler will discard this function template when computing the overload set and search for other viable