Skip to content

Commit b5eff57

Browse files
authored
Clarify runtime representation of Unit (#211)
* Clarify the representation of `Unit` * Fix typo * Clarify explanation more * Clarify explanation more, yet again
1 parent e79cd54 commit b5eff57

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

text/chapter10.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,8 @@ For simple types, the correspondence is almost trivial. For example, if an expre
11361136

11371137
A similar law holds for expressions of type `Int` `Number` and `String` - expressions of type `Int` or `Number` evaluate to non-null JavaScript numbers, and expressions of type `String` evaluate to non-null JavaScript strings. Expressions of type `Int` will evaluate to integers at runtime, even though they cannot not be distinguished from values of type `Number` by using `typeof`.
11381138

1139+
What about `Unit`? Well, since `Unit` has only one inhabitant (`unit`) and its value is not observable, it doesn't actually matter what it's represented with at runtime. Old code tends to represent it using `{}`. Newer code, however, tends to use `undefined`. So, although it doesn't really matter what you use to represent `Unit`, it is recommended to use `undefined` (not returning anything from a function also returns `undefined`).
1140+
11391141
What about some more complex types?
11401142

11411143
As we have already seen, PureScript functions correspond to JavaScript functions of a single argument. More precisely, if an expression `f` has type `a -> b` for some types `a` and `b`, and an expression `x` evaluates to a value with the correct runtime representation for type `a`, then `f` evaluates to a JavaScript function, which when applied to the result of evaluating `x`, has the correct runtime representation for type `b`. As a simple example, an expression of type `String -> String` evaluates to a function which takes non-null JavaScript strings to non-null JavaScript strings.

0 commit comments

Comments
 (0)