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/>


_______________________________________________
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list

Reply via email to