4.2 KiB
title |
---|
Representing Values in Programming Languages |
NOT YET READY
We have given a definition of Value
and its semantics, and proposed
a concrete syntax for communicating and storing Value
s. We now turn
to suggested representations of Value
s as programming-language
values for various programming languages.
When designing a language mapping, an important consideration is roundtripping: serialization after deserialization, and vice versa, should both be identities.
Also, the presence or absence of annotations on a Value
should not
affect comparisons of that Value
to others in any way.
JavaScript.
Boolean
↔Boolean
Float
andDouble
↔ numbersSignedInteger
↔ numbers orBigInt
(see here and here)String
↔ stringsByteString
↔Uint8Array
Symbol
↔Symbol.for(...)
Record
↔{ "_label": theLabel, "_fields": [field0, ..., fieldN] }
, plus convenience accessors(undefined)
↔ the undefined value(rfc3339 F)
↔Date
, ifF
matches thedate-time
RFC 3339 production
Sequence
↔Array
Set
↔{ "_set": M }
whereM
is aMap
from the elements of the set totrue
Dictionary
↔ aMap
Scheme/Racket.
Boolean
↔ booleansFloat
andDouble
↔ inexact numbers (Racket: single- and double-precision floats)SignedInteger
↔ exact numbersString
↔ stringsByteString
↔ byte vector (Racket: "Bytes")Symbol
↔ symbolsRecord
↔ structures (Racket: prefab struct)Sequence
↔ listsSet
↔ Racket: setsDictionary
↔ Racket: hash-table
Java.
Boolean
↔Boolean
Float
andDouble
↔Float
andDouble
SignedInteger
↔Integer
,Long
,BigInteger
String
↔String
ByteString
↔byte[]
Symbol
↔ a simple data class wrapping aString
Record
↔ in a simple implementation, a genericRecord
class; else perhaps a bean mapping?(mime T B)
↔ an implementation ofjavax.activation.DataSource
?
Sequence
↔ an implementation ofjava.util.List
Set
↔ an implementation ofjava.util.Set
Dictionary
↔ an implementation ofjava.util.Map
Erlang.
Boolean
↔true
andfalse
Float
andDouble
↔ floats (unsure how Erlang deals with single-precision)SignedInteger
↔ integersString
↔ pair ofutf8
and a binaryByteString
↔ a binarySymbol
↔ pair ofatom
and a binaryRecord
↔ triple ofobj
, label, and field listSequence
↔ a listSet
↔ asets
setDictionary
↔ a [map][erlang-map] (new in Erlang/OTP R17)
This is a somewhat unsatisfactory mapping because: (a) Erlang doesn't
garbage-collect its atoms, meaning that (a.1) representing Symbol
s
as atoms could lead to denial-of-service and (a.2) representing
Symbol
-labelled Record
s as Erlang records must be rejected for the
same reason; (b) even if it did, Erlang's boolean values are atoms,
which would then clash with the Symbol
s true
and false
; and (c)
Erlang has no distinct string type, making for a trilemma where
String
s are in danger of clashing with ByteString
s, Sequence
s,
or Record
s.
Python.
Boolean
↔True
andFalse
Float
↔ aFloat
wrapper-class for a double-precision valueDouble
↔ floatSignedInteger
↔ int and longString
↔unicode
ByteString
↔bytes
Symbol
↔ a simple data class wrapping aunicode
Record
↔ something likenamedtuple
, but that doesn't care about class identity?Sequence
↔tuple
(but acceptlist
during encoding)Set
↔frozenset
(but acceptset
during encoding)Dictionary
↔ a hashable (immutable) dictionary-like thing (but acceptdict
during encoding)
Squeak Smalltalk.
Boolean
↔true
andfalse
Float
↔ perhaps a subclass ofFloat
?Double
↔Float
SignedInteger
↔Integer
String
↔WideString
ByteString
↔ByteArray
Symbol
↔WideSymbol
Record
↔ a simple data classSequence
↔ArrayedCollection
(usuallyOrderedCollection
)Set
↔Set
Dictionary
↔Dictionary