Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cknitt opened this issue Apr 25, 2024 · 0 comments
Open

Comments

@cknitt
Copy link
Member

cknitt commented Apr 25, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant