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

Core Map.get when map has with optional values #7054

Open
5 tasks done
angusjf opened this issue Sep 26, 2024 · 4 comments
Open
5 tasks done

Core Map.get when map has with optional values #7054

angusjf opened this issue Sep 26, 2024 · 4 comments

Comments

@angusjf
Copy link

angusjf commented Sep 26, 2024

Thank you for filing! Check list:

  • Is it a bug? Usage questions should often be asked in the forum instead.
  • Concise, focused, friendly issue title & description.
  • A minimal, reproducible example.
  • OS and browser versions, if relevant.
  • Is it already fixed in master? Instructions
type key = A | B

let dict = Map.fromArray([(A, Some(1)), (B, None)])

assert(dict->Map.get(A) == Some(Some(1))) // as expected

assert(dict->Map.get(B) == None)          // expected to fail, passes

assert(dict->Map.get(B) == Some(None))    // expected to pass, fails

JS's Map returns undefined when the key is not present, and also when the value is undefined.

https://github.com/rescript-lang/experimental-rescript-stdlib-build/blob/aa8001a778a8975f83ece0a5266b3049b9d20d42/core/src/Core__Map.res#L14

Apologies if issue is in the wrong place, I am new to rescript

@cometkim
Copy link
Member

Good catch. I think it's an issue of the Core binding

It actually need runtime to handle nested option correctly

module Map = {
  include Map

  let get = (t, key) => {
    if t->has(key) {
      Some(t->get(key)->Option.getUnsafe)
    } else {
      None
    }
  }
}

@zth
Copy link
Collaborator

zth commented Oct 1, 2024

You should be able to work around this by using Null.t instead of option.

@bloodyowl
Copy link
Collaborator

yeah, otherwise the get would need a runtime wrapper (like map.has(x) ? Some(map.get(x)) : None)

@cknitt
Copy link
Member

cknitt commented Oct 16, 2024

See also rescript-lang/rescript-core#217

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

No branches or pull requests

5 participants