upcasting

Why is upcasting necessary in this Scala code?

懵懂的女人 提交于 2019-12-05 16:35:56
问题 This compiles: import scala.collection._ trait Foo[A, +This <: SortedSet[A] with SortedSetLike[A,This]] extends SortedSetLike[A, This] { this: This => def bar: This = (this: SortedSetLike[A,This]).empty } But if the upcast is removed it fails to compile: import scala.collection._ trait Foo[A, +This <: SortedSet[A] with SortedSetLike[A,This]] extends SortedSetLike[A, This] { this: This => def bar: This = this.empty } Why? From the extends clause we know that Foo is a SortedSetLike[A, This] ,

How to implement a compile-time check that a downcast is valid in a CRTP?

时光怂恿深爱的人放手 提交于 2019-12-05 04:40:05
I have a plain old CRPT (please don't get distracted by access restrictions - the question is not about them): template<class Derived> class Base { void MethodToOverride() { // generic stuff here } void ProblematicMethod() { static_cast<Derived*>(this)->MethodToOverride(); } }; that is as usual intended to be used like this: class ConcreteDerived : public Base<ConcreteDerived> { void MethodToOverride() { //custom stuff here, then maybe Base::MethodToOverride(); } }; Now that static_cast bothers me. I need a downcast (not an upcast), so I have to use an explicit cast. In all reasonable cases

implicit upcasting and explicit downcasting in java

和自甴很熟 提交于 2019-12-04 22:01:13
问题 When java can implicitly do up casting , why does not it implicitly do down casting ?Please explain with some simple example? 回答1: The point is that upcasting will always succeed, so it's safe - whereas downcasting can fail: String x = getStringFromSomewhere(); Object y = x; // This will *always* work But: Object x = getObjectFromSomewhere(); String y = (String) x; // This might fail with an exception Because it's a "dangerous" operation, the language forces you to do it explicitly - you're

Java - Upcasting and Downcasting

♀尐吖头ヾ 提交于 2019-12-04 18:12:06
I Knew there are plenty of articles/questions in stackoverflow describing about upcasting and downcasting in Java. And I knew what is upcasting and downcasting. But my question is not specfic to that. Upcasting - Conversion from child to parent - Compiler takes care. No cast is required Downcasting - Conversion from parent to child - Explicit cast is required public class Animal { public void getAnimalName(){ System.out.println("Parent Animal"); } } public class Dog extends Animal{ public void getDogName(){ System.out.println("Dog"); } } public static void main(String[] args) { Dog d = new Dog

Swift - upcasting array of protocol to array of super protocol causes error

纵饮孤独 提交于 2019-12-04 04:02:12
问题 In Swift, I notice that I can upcast an object that conforms to a protocol called, let's say SubProtocol to another protocol called SuperProtocol which is a super protocol of SubProtocol . But I can't do the same with an array of the protocol. Here's the example code that I ran in Playground: protocol SuperProtocol { } protocol SubProtocol: SuperProtocol { } class MyObject: SubProtocol { } let value1: SubProtocol = MyObject() let value2: SuperProtocol = value1 // No error here. Upcasting

Use methods declared in implementation that are not defined in interface

五迷三道 提交于 2019-11-30 08:50:27
问题 I have a class defined by an interface public interface Test { void testMethod(); } Test test = new TestImpl(); public class TestImpl implements Test { @Override public void testMethod() { //Nothing to do here } public void anotherMethod() { //I am adding this method in the implementation only. } } How can I call anotherMethod? test.anotherMethod(); //Does not work. I want to be able to define a few methods in the implementation only because in my production code, the Test interface covers a

Does SFINAE apply to function bodies?

本秂侑毒 提交于 2019-11-29 15:20:35
I have the following sample code: class Serializable {}; class MyData : public Serializable {}; void GetData( Serializable& ) {} template<typename T> void GetData( T& data ) { std::istringstream s{"test"}; s >> data; } int main() { MyData d; GetData(d); } ( Live Sample ) Based on overload resolution rules, the non-template version should be preferred because the base class is of type Serializable . However, I expect SFINAE to kick in when there are errors in the template version when it is instantiated for overload resolution (because if the >> operator is not defined for a type, it should not

How expensive is downcasting in Java 6? [closed]

帅比萌擦擦* 提交于 2019-11-29 12:01:39
There is a method which receives an argument of type Collection and it needs to use some of the methods that are found in the List class when it does work with that argument. Is up-casting expensive in terms of speed? List<Stuff> list = (List<Stuff>) collection; I would also like to note that the collection object is never used after this, only the list , and that this will be compiled and run on Oracle Java 1.6. Serious answers are given by actual benchmarks. For example, I used this jmh -targeting code: public class Benchmark1 { static final List<Integer>[] lists = new List[10000]; static {

Use methods declared in implementation that are not defined in interface

只谈情不闲聊 提交于 2019-11-29 08:04:42
I have a class defined by an interface public interface Test { void testMethod(); } Test test = new TestImpl(); public class TestImpl implements Test { @Override public void testMethod() { //Nothing to do here } public void anotherMethod() { //I am adding this method in the implementation only. } } How can I call anotherMethod? test.anotherMethod(); //Does not work. I want to be able to define a few methods in the implementation only because in my production code, the Test interface covers a pretty broad spectrum of classes and is implemented by multiple classes. I use methods defined in the

Why does C style cast allow you to convert to a private base class? [duplicate]

筅森魡賤 提交于 2019-11-29 04:08:25
This question already has an answer here: Can I cast a derived class to a private base class, using C-style cast? 3 answers Say we have this code class A { public: A() : x(1) {} virtual ~A() {} int x; }; class B { public: B() : y(2) {} virtual ~B() {} void g() { cout << "B::" << y << endl; } int y; }; class C : private A, private B { public: void f() { B* p = static_cast<B*>( this ); p->g(); } }; int main() { C c; ((B*)&c)->g(); return 0; } The C style cast in the main function cannot be correctly expressed in terms of the C++ casts ( static_cast , dynamic_cast , reinterpret_cast ). But what