puffnfresh / roy

Small functional language that compiles to JavaScript.
http://roy.brianmckenna.org/
MIT License
834 stars 74 forks source link

Handling null/undefined in JS interop #191

Open rtfeldman opened 10 years ago

rtfeldman commented 10 years ago

The canonical example of JS interop is console.log, which returns nothing. We have a test showing that we can coerce console to Number if need be.

But what about handling return values from calls to JS functions? Since Roy doesn't have null or undefined, how do we handle the case where we get one of those values back?

One approach would be to include something along the lines of JSOption in the core (or stdlib?), and wrap all JS return values in it:

data JSOption value = Some value | Null | Undefined

match strFromJsFunction
  case (Some (Str str)) = console.log str
  case Null             = console.log "Got back null"
  case Undefined        = console.log "Got back undefined"

On the flip side, there's also the question of how to interact with JS APIs that require passing null or undefined under certain circumstances. One approach would be to give JSOption special treatment when making calls out, having Null compile to just the JS literal null, Some foo compile out to just foo, etc. I'm not a fan of special treatment in general, but it's at least a starting point for discussion.

Thoughts?