Module

Foreign

This module defines types and functions for working with foreign data.

#Foreign

data Foreign :: Type

A type for foreign data.

Foreign data is data from any external unknown or unreliable source, for which it cannot be guaranteed that the runtime representation conforms to that of any particular type.

Suitable applications of Foreign are

  • To represent responses from web services
  • To integrate with external JavaScript libraries.

#MultipleErrors

type MultipleErrors = NonEmptyList ForeignError

A type for accumulating multiple ForeignErrors.

#F

type F = Except MultipleErrors

An error monad, used in this library to encode possible failures when dealing with foreign data.

The Alt instance for Except allows us to accumulate errors, unlike Either, which preserves only the last error.

#renderForeignError

#unsafeToForeign

unsafeToForeign :: forall a. a -> Foreign

Coerce any value to the a Foreign value.

This is considered unsafe as it's only intended to be used on primitive JavaScript types, rather than PureScript types. Exporting PureScript values via the FFI can be dangerous as they can be mutated by code outside the PureScript program, resulting in difficult to diagnose problems elsewhere.

#unsafeFromForeign

unsafeFromForeign :: forall a. Foreign -> a

Unsafely coerce a Foreign value.

#unsafeReadTagged

unsafeReadTagged :: forall m a. Monad m => String -> Foreign -> FT m a

Unsafely coerce a Foreign value when the value has a particular tagOf value.

#typeOf

typeOf :: Foreign -> String

Read the Javascript type of a value

#tagOf

tagOf :: Foreign -> String

Read the Javascript tag of a value.

This function wraps the Object.toString method.

#isNull

isNull :: Foreign -> Boolean

Test whether a foreign value is null

#isUndefined

isUndefined :: Foreign -> Boolean

Test whether a foreign value is undefined

#isArray

isArray :: Foreign -> Boolean

Test whether a foreign value is an array

#readString

readString :: forall m. Monad m => Foreign -> FT m String

Attempt to coerce a foreign value to a String.

#readChar

readChar :: forall m. Monad m => Foreign -> FT m Char

Attempt to coerce a foreign value to a Char.

#readBoolean

readBoolean :: forall m. Monad m => Foreign -> FT m Boolean

Attempt to coerce a foreign value to a Boolean.

#readNumber

readNumber :: forall m. Monad m => Foreign -> FT m Number

Attempt to coerce a foreign value to a Number.

#readInt

readInt :: forall m. Monad m => Foreign -> FT m Int

Attempt to coerce a foreign value to an Int.

#readArray

readArray :: forall m. Monad m => Foreign -> FT m (Array Foreign)

Attempt to coerce a foreign value to an array.

#readNull

readNull :: forall m. Monad m => Foreign -> FT m (Maybe Foreign)

#readUndefined

readUndefined :: forall m. Monad m => Foreign -> FT m (Maybe Foreign)

#readNullOrUndefined

readNullOrUndefined :: forall m. Monad m => Foreign -> FT m (Maybe Foreign)

#fail

fail :: forall m a. Monad m => ForeignError -> FT m a

Throws a failure error in FT.

Modules