Skip to content

Null Safety requires an explicit cast when casting a num to an int #971

Closed
@a-siva

Description

@a-siva

When migrating the below code to NNBD using pkg/dev_compiler/tool/check_nnbd_sdk.dart tool, the analyzer produces a warning (requires an explicit cast)

const int DAYS_IN_4_YEARS = 4 * 365 + 1;	
const int DAYS_IN_100_YEARS = 25 * DAYS_IN_4_YEARS - 1;
const int DAYS_IN_100_YEARS = 25 * DAYS_IN_4_YEARS - 1;
const int DAYS_IN_400_YEARS = 4 * DAYS_IN_100_YEARS + 1;
const int daysSince1970 = 200;
const in DAYS_IN_400_YEARS
int days = daysSince1970;
days = days.remainder(DAYS_IN_400_YEARS);

ERROR|STATIC_TYPE_WARNING|INVALID_ASSIGNMENT|lib/core/core.dart|2092|12|33|A value of type 'num' can't be assigned to a variable of type 'int'.

It requires the user to do an explicit cast which can be inefficient.

We need a mechanism that would not require the user to make an explicit cast here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions