Re: [Vala] Invalid cast Warnings



----- Original Message -----
From: Luca Dionisi <luca dionisi gmail com>
Sent: Sunday, 5 February 2017, 17:43
Subject: Re: [Vala] Invalid cast Warnings

Obviously you can't cast an instance of a base class to a superclass.
You can do:
var a = new TestSubType();
TestBase b = (TestBase)a;

Not viceversa.


I've never used sub-types much. Always found casting to an
interface for polymorhism much easier to understand.
Sub-type tutorials always end up using animals or car parts :-)

Maybe something like this would make it clearer:

void main () {
  var a = new Salmon ();
  Mackerel b = (Mackerel)a; // Invalid cast
  Fish c = (Fish)a; // Valid cast
}

class Fish {
}

class Salmon:Fish {
}

class Mackerel:Fish {
}

I guess it makes sense that a salmon can't be converted to a mackerel, 

but it is a fish.

The Vala compiler, however, should reject these casts rather than have
low level C code do it at run time.


Al


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]