Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In Dart</p> <ol> <li><p>upcasts are implicit. If <code>B</code> is a subclass of <code>A</code>, then <code>B b = a</code> (where <code>a</code> is an instance of class <code>A</code>) is warning free and silently casts <code>a</code> to <code>B</code>. In Java you would have needed to write <code>B b = (B) a</code>.</p></li> <li><p>the dynamic type is an always-present escape hatch. If <code>B</code> and <code>C</code> are not in the same hierarchy, then temporarily assigning to the dynamic type will make the cast warning-free. </p> <pre><code>B b = someB; var tmp = b; C c = tmp; </code></pre></li> <li><p>One can do an explicit check using <code>is</code>. The <code>is</code> check returns true if the object is of the right type. There are also some very simple rules that propagate is-check types. For example, if <code>is</code> is used inside an <code>if</code> condition, then the corresponding branch uses this information for type-warnings. </p> <pre><code>Object o; o.foo(); // warning: foo does not exist on Object. if (o is A) { // assuming that A contains 'foo'. o.foo(); // warning-free. } </code></pre></li> <li><p>One can explicitly check and throw if a type is not the expected one with <code>as</code>. This operator does <em>not</em> throw when the left-hand side is <code>null</code>.</p></li> </ol> <p>For your example this boils down to:</p> <p>No explicit check (1):</p> <pre class="lang-dart prettyprint-override"><code>void onEvent(Event event) { // Implicit upcast. PlaceChangeEvent is subclass of Event. PlaceChangeEvent pce = event; Place place = pce.place; presenterProvider.display(place); } </code></pre> <p>With an is-check (3):</p> <pre class="lang-dart prettyprint-override"><code>void onEvent(Event event) { if (event is PlaceChangeEvent) { // Static type system propagates type. No need for variable. Place place = event.place; presenterProvider.display(place); } else { // Note: this doesn't look like an exception, but an error. throw new Exception("Event is not a PlaceChangeEvent."); } } </code></pre> <p>Using <code>as</code> (4):</p> <pre class="lang-dart prettyprint-override"><code>void onEvent(Event event) { Place place = (event as PlaceChangeEvent).place; presenterProvider.display(place); } </code></pre> <p>Alternatively, if you expect to receive a PlaceChangeEvent, you should simply change the type of the argument:</p> <pre class="lang-dart prettyprint-override"><code>void onEvent(PlaceChangeEvent event) { Place place = event.place; presenterProvider.display(place); } </code></pre> <p>In checked mode this will catch bad types, and in unchecked mode it will throw when accessing <code>event.place</code>. This is generally the preferred way.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload