Re: [Vala] Invalid cast Warnings
- From: Michael Gratton <mike vee net>
- To: Al Thomas <astavale yahoo co uk>
- Cc: Luca Dionisi <luca dionisi gmail com>, vala-list <vala-list gnome org>, Daniel Brendle <grindhold skarphed org>
- Subject: Re: [Vala] Invalid cast Warnings
- Date: Mon, 06 Feb 2017 11:04:06 +1100
On Mon, Feb 6, 2017 at 4:57 AM, Al Thomas via vala-list
<vala-list gnome org> wrote:
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
}
In that case, yeah, but there definitely are cases as Daniel suggests
where it is needed, including his example of integrating with 3rd party
code.
Eg: If some method defines that parameter with a specific type, but it
is likely that the actual object will be a subtype, then it's valid to
check and then cast:
interface Printer {
void do_print(PrintJob job);
}
class UnixPrinter : Printer {
void do_print(PrintJob job) {
UnixPrintJob unix = job as UnixPrintJob;
...
}
}
--
⊨ Michael Gratton, Percept Wrangler.
⚙ <http://mjog.vee.net/>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]