Skip to content

Optimization applied for polymorphic variants, but not for regular variants #6741

Open
@cknitt

Description

@cknitt

The following ReScript code

type status1 = OPEN | CLOSED

let isOpen1 = (status: status1) =>
  switch status {
  | OPEN => true
  | CLOSED => false
  }

type status2 = [#OPEN | #CLOSED]

let isOpen2 = (status: status2) =>
  switch status {
  | #OPEN => true
  | #CLOSED => false
  }

creates the following JS output:

function isOpen1(status) {
  if (status === "OPEN") {
    return true;
  } else {
    return false;
  }
}

function isOpen2(status) {
  return status === "OPEN";
}

So there is some optimization applied for polymorphic variants that is not applied for normal variants.

https://rescript-lang.org/try?version=v11.1.0&code=C4TwDgpgBAzsCGwCuMByB7ATgW3gGwDV5MBLeAO2CgF4oB5ABQFFUoAfKAYQBk6BlJgBEAUMLwQqJGHUjkMOfEVIUqtABRxEKAFywEyNFlyFiZSgEoaAPmFRYAdxLAAxgAs9WmFADet9vWZWaisoYEwkCD8OHn4haygAM3wYSLsAX1FQSA8DBnQ8EGwsMFcSZyUzVSgAbQBiRhZ-WpiBQQBdUXFJaVk8gqLMErKKlRooDX0dHJQ+wuLS8tMVS2C-GEcXd00DHyioesD4sIi95t5W+KS8FL8MoA

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