Debian Codegraph @

docs/src/why/syntax-typing-bad.md

raw
1 ---
2 title: What is syntax typing?
3 ---
4
5 Explicit syntax typing is the use of syntax to designate the type of some data.
6 It is a feature of the design of most programming languages.
7
8 ```python
9 x = "11" # this is a string
10 y = 11 # this isn't
11 ```
12
13 It it isn't limited to programming languages though. It's a common feature of
14 serialization formats like JSON:
15
16 ```json
17 {"x": "11", "y": 42}
18 ```
19
20 But not others, like INI:
21
22 ```ini
23 server=192.0.2.62
24 port=143
25 ```
26
27 Or CSV:
28
29 ```ini
30 server,port
31 192.0.2.62,143
32 ```
33
34 Or StrictYAML:
35
36 ```yaml
37 server: 192.0.2.62
38 port: 143
39 ```
40
41 In those cases, it's up to the program - in another file - to decide what part
42 of that data is and what part is an integer.
43
44
45 ## What does it mean to attach syntax typing to data?
46
47 Syntax typing the explicit prioritization of cohesion over terseness. It
48 puts type information right next to the data, but in the process this
49 means that if the data appears a *lot*
50
51
52 ## When would you sacrifice terseness?
53
54 Let's return to Python for a second and imagine that
55
56 ```python
57 x = "11" # this is a string
58 y = 11 # this isn't
59 ```
60
61 This helps distinguish the types for the parser, which is useful for JSON, but it also comes with two disadvantages:
62
63 - The distinction is subtle and not particularly clear to *non-programmers*, who will not necessarily understand that a directive needs to be given to the parser to avoid it being misinterpreted.
64 - It's not necessary if the type structure is maintained outside of the markup.
65 - Verbosity - two extra characters per string makes the markup longer and noisier.
66
67 In JSON when being used as a REST API, syntax typing is often an *advantage* - it is explicit to the machine reading the JSON that "string" and "age" is an integer and it can convert accordingly *in the absence of a schema*.
68
69 StrictYAML assumes all values are strings unless the schema explicitly indicates otherwise (e.g. Map(Int(), Int())).
70
71 StrictYAML does not require quotation marks for strings that are implicitly converted to other types (e.g. yes or 1.5), but it does require quotation marks for strings that are syntactically confusing (e.g. "{ text in curly brackets }")
72
73 Standard YAML has explicit syntax typing to explicitly declare strings, although it's confusing as hell to know when it's required and when it is not. For example:
74
75 ```yaml
76 a: text # not necessary
77 b: "yes" # necessary
78 c: "0" # necessary
79 d: "3.5" # necessary
80 e: in # not necessary
81 f: out # not necessary
82 g: shake it all about # not necessary
83 h: "on" # necessary
84 ```
85
86 Most other configuration language formats also make use of syntax typing. For example:
87
88 - [TOML](../../why-not/toml)
89 - [JSON5](../../why-not/json5)
90 - [HJSON](../../why-not/hjson)
91 - [SDLang](../../why-not/sdlang)
92 - [HOCON](../../why-not/hocon)
93
94 [INI](../../why-not/ini) does not have explicit syntax typing however.