-
Notifications
You must be signed in to change notification settings - Fork 6
JSObject Wrapper Types
jbracker edited this page Mar 11, 2013
·
14 revisions
It is sometimes useful to introduce wrapper types (e.g. JSDate
, JSCanvas
) around JSObject
to gain a bit more type safty. When implementing these don't forget to provide the following instances:
-
Show
- A prerequisite forSunroof
. Do not derive this instance! It is used as default implementation forshowVar
inSunroof
. A derived instance will not produce valid Javascript. -
Sunroof
- All these wrapper types should be sunroof values (otherwise they are useless). -
BooleanOf
- Without this binding using the type insideif-then-else
is impossible. -
IfB
- WithoutifB
will not be available for the wrapper. -
EqB
- This should be implemented with reference equality to be consistent with Javascript. This also means it is always possible to implement this class.
Here an example implemention for the type not existing type JSWrapper
. It is advised to provide the comment for EqB
that is given in the example to ensure that readers of the documentation know what is going on.
newtype JSWrapper = JSWrapper JSObject
instance Show JSWrapper where
show (JSWrapper o) = show o
instance Sunroof JSWrapper where
box = JSWrapper . box
unbox (JSWrapper o) = unbox o
type instance BooleanOf JSWrapper = JSBool
instance IfB JSWrapper where
ifB = jsIfB
-- | Reference equality, not value equality.
instance EqB JSWrapper where
(JSWrapper a) ==* (JSWrapper b) = a ==* b
This requires the following imports:
import Data.Boolean ( BooleanOf, IfB(..), EqB(..) )
import Language.Sunroof.Classes ( Sunroof(..) )
import Language.Sunroof.JS.Bool ( JSBool, jsIfB )
import Language.Sunroof.JS.Object ( JSObject )