| Portability | portable |
|---|---|
| Stability | experimental |
| Maintainer | golubovsky@gmail.com |
Language.JSMW.Cond
Contents
Description
Encoding of Javascript conditionals.
- switch :: (Switchable s r, CElement c) => Expression r -> Switch s r c e a -> JSMW c (Expression e)
- (-->) :: (Switchable s r, CElement c) => s -> JSMW c (Expression e) -> Switch s r c e ()
- none :: (Switchable s r, CElement c) => JSMW c (Expression e) -> Switch s r c e ()
Switch
The following functions help encode the Javascript switch and case statements.
The Switchable class seen in the type signatures, and its necessary instances are
defined internally by this module. The switch statement can be encoded for numeric,
boolean, and string scrutinees.
Unlike Javascript switch, a value can be returned from JSMW switch (see
the second example). All expressions matching case labels must return values of the same
type.
Below are examples of switch statements encoded in JSMW.
let p =number5 - (number1 -number4)switchp $ do 5-->alert(string"This is Five") 8-->alert(string"This is Eight")none$toStringp >>=alert... n2 <-switchctrl $ do True-->return (n -number1) False-->return (n +number1)
switch :: (Switchable s r, CElement c) => Expression r -> Switch s r c e a -> JSMW c (Expression e)Source
Encode a switch statement.
(-->) :: (Switchable s r, CElement c) => s -> JSMW c (Expression e) -> Switch s r c e ()Source
Encode a case label. The first (left) argument is a literal describing
the value of the label. Note that the left argument must be a Haskell
literal, not a Javascript expression. In other words, for boolean labels,
use True rather than true. The second (right) argument is a JSMW monadic
expression matching the label. Break statements are inserted automatically
(that is, fall-through case labels are not permitted).
none :: (Switchable s r, CElement c) => JSMW c (Expression e) -> Switch s r c e ()Source
Encode a default: case label, that is, what action should be taken if none
of the case labels matches the scrutinee.
In both none and -->, JSMW monadic expression should be of the same type.
Also note that if no case label matches the scrutinee value, and no default
label has been defined, an exception will be thrown showing the scrutinee
name that did not match.