Skip to content

Add Pedantic warnings for explicit downcasting (type refinement) #2430

@MaxGraey

Description

@MaxGraey
Member

Downcasting is not cheap and may produce app termination. Much better to use explicit type narrowing + implicit downcasting like:

class Animal {}
class Tiger extends Animal {}
function foo(base: Animal): void {
  if (base instanceof Tiger) {
    let tiger = base; // implicitly infer as base: Tiger
    // use tiger
  }
}

instead of

function foo(base: Animal): void {
   let tiger = <Tiger>base; // may throw "unexpected downcast"
}

Pedantic mode (enabled by --pedantic) should disallow last explicit variant and propose first one.

Depends on #2423 and #2352

Activity

georg-getz

georg-getz commented on Aug 12, 2022

@georg-getz

Hi, I randomly got here from following #2423. Wouldn't Child be a better name than Parent for the extending class :D

MaxGraey

MaxGraey commented on Aug 12, 2022

@MaxGraey
MemberAuthor

@georg-getz good point. Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @MaxGraey@georg-getz

        Issue actions

          Add Pedantic warnings for explicit downcasting (type refinement) · Issue #2430 · AssemblyScript/assemblyscript