Skip to content

Enums Implicitly Converts to Int when Compared to Null #862

Description

@frederikaalund

Let's use the following enum as an example:

public enum AreaType
	WOODLAND
	SNOWY

The compiler will associate 0 with WOODLAND and 1 with SNOWY.

The compiler complains if we try to compare an enum with an integer. This is a good thing. Take for instance:

let isWoodland = AreaType.WOODLAND == 0

The above gives the error Cannot compare types AreaType with integer-literal. Exactly what we want.

Unfortunately, comparison between enum and null works. This produces unexpected results:

let isWoodland = AreaType.WOODLAND == null

isWoodland will be assigned true since null is coalesced into 0 (which coincides with the integer representation of WOODLAND). This leads to subtle bugs. Note that you can also do meaningless stuff like AreaType areaType = null.

I think comparison between enum and null should give a compiler error instead.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions