JavaScript representations of PureScript values

From foldr
Jump to: navigation, search

PureScript uses ordinary JavaScript values. The JavaScript representations of the more common types are listed on this page. You can use these representations when you are using the FFI.

Keep in mind that PureScript is still unstable, and these representations may change at any time.

Newtypes

Newtypes have the same representation as their underlying type. For example, the following newtype is simply represented by a JavaScript string:

newtype Name = Name String

Unit

These may be represented by any JavaScript value. This allows for tricks such as:

fastVoid :: forall f a. (Functor f) => f a -> f Unit
fastVoid = unsafeCoerce

Function

These are represented by unary JavaScript functions. These JavaScript functions must be pure and parametric. If the functions are not total, consider adding a Partial constraint.

Boolean, Int, Number, String

These are represented by JavaScript Booleans, numbers, and strings.

Array

These are represented by JavaScript arrays.

Record

These are represented by JavaScript objects with corresponding properties.

Eff

These are represented by nilary JavaScript functions. These functions may be effectful. Here is an example:

foreign import log :: forall e. String -> Eff (console :: CONSOLE | e) Unit
exports.log = function(s) {
  return function() { // this is the Eff value
    console.log(s);
  };
};

Aff

These are represented by binary JavaScript functions. These functions may be effectful. They take two arguments: a unary success callback and a unary error callback. Additionally, the function must return a canceler. Here is an example:

foreign import sleep :: forall e. Number -> Aff (time :: TIME | e) Unit
exports.sleep = function(msec) {
  return function(onSuccess, onError) { // this is the Aff value
    if (msec < 0) {
      onError(Error('negative duration'));
    } else {
      setTimeout(onSuccess, msec);
    }
    return Control_Monad_Aff.nonCanceler;
  };
};

IO

Same as Aff.