| 1 | ---
|
| 2 | default_highlighter: oils-sh
|
| 3 | ---
|
| 4 |
|
| 5 | Types in the Oils Runtime - OSH and YSH
|
| 6 | ===========
|
| 7 |
|
| 8 | Here are all types of values in the Oils runtime, organized for understanding.
|
| 9 |
|
| 10 | <div id="toc">
|
| 11 | </div>
|
| 12 |
|
| 13 | ## Nine Atoms
|
| 14 |
|
| 15 | Values of these types are immutable:
|
| 16 |
|
| 17 | ### Serialiable
|
| 18 |
|
| 19 | - `Null Bool`
|
| 20 | - `Int Float`
|
| 21 | - `Str`
|
| 22 | - The only type shared between OSH and YSH.
|
| 23 |
|
| 24 | ### More
|
| 25 |
|
| 26 | - `Eggex Match` - to match patterns
|
| 27 | - `Range` - to iterate over `3 .. 5`
|
| 28 | - `Stdin` - to iterate over lines (has a singleton value)
|
| 29 |
|
| 30 | <!--
|
| 31 | It seems like stdin could be a file descriptor, but that doesn't fit with the
|
| 32 | shell I/O model. You always REDIRECT first, then read from stdin. And you
|
| 33 | don't read incrementally from multiple files at once.
|
| 34 | -->
|
| 35 |
|
| 36 | <!--
|
| 37 |
|
| 38 | These are variants of VALIDATED strings, with lazily materialized views?
|
| 39 |
|
| 40 | - value.{Htm8,Tsv8,Json8} ?
|
| 41 |
|
| 42 | -->
|
| 43 |
|
| 44 | ## Six Containers
|
| 45 |
|
| 46 | - `List Dict` - YSH containers are arbitrarily recursive
|
| 47 | - `Place` is for "out parmas"
|
| 48 | - created with `&myvar`, mutated with `call place->setValue(42)`
|
| 49 | - `BashArray BashAssoc` are for bash compatibility in OSH:
|
| 50 |
|
| 51 | ### `Obj` is for User-defined Types
|
| 52 |
|
| 53 | - `Obj` - has a prototype chain
|
| 54 |
|
| 55 | Objects allow **polymorphism**. See [YSH Objects](objects.html).
|
| 56 |
|
| 57 | #### Examples of Objects
|
| 58 |
|
| 59 | Modules and types are represented by `Obj` instances of a certain shape, not by
|
| 60 | primitive types.
|
| 61 |
|
| 62 | 1. Modules are `Obj` instances with attributes, and an `__invoke__` method.
|
| 63 | 1. Types are `Obj` instances with an `__index__` method, and are often compared
|
| 64 | for identity.
|
| 65 |
|
| 66 | In general, Objects are mutable. Do not mutate modules or types!
|
| 67 |
|
| 68 | ## Five Units of Code
|
| 69 |
|
| 70 | Values of these types are immutable:
|
| 71 |
|
| 72 | - `BoundFunc` (for methods)
|
| 73 | - `BuiltinFunc Func`
|
| 74 | - `BuiltinProc Proc`
|
| 75 |
|
| 76 | ## Four Types for Reflection
|
| 77 |
|
| 78 | - `Command Expr` - custom evaluation of commands and expressions <!-- no CommandFrag, ExprFrag) -->
|
| 79 | - `Frame` - a frame on the call stack (`proc` and `func`)
|
| 80 | - `DebugFrame` - to trace `eval()` calls, and more
|
| 81 |
|
| 82 | ## Appendix
|
| 83 |
|
| 84 | ### The JSON Data Model
|
| 85 |
|
| 86 | These types can be serialized to and from JSON:
|
| 87 |
|
| 88 | - `Null Str Int Float List Dict`
|
| 89 |
|
| 90 | ### Why Isn't Everything an Object?
|
| 91 |
|
| 92 | In YSH, the `Obj` type is used for **polymorphism** and reflection.
|
| 93 |
|
| 94 | Polymorphism is when you hide **different** kinds of data behind the **same**
|
| 95 | interface.
|
| 96 |
|
| 97 | But most shell scripts deal with **concrete** textual data, which may be
|
| 98 | JSON-like or TSV-like. The data is **not** hidden or encapsulated, and
|
| 99 | shouldn't be.
|
| 100 |
|
| 101 | ### Implementation Details
|
| 102 |
|
| 103 | These types used internally:
|
| 104 |
|
| 105 | - `value.Undef` - used when looking up a variable
|
| 106 | - `value.Interrupted` - for SIGINT
|
| 107 | - `value.Slice` - for a[1:2]
|
| 108 |
|
| 109 | ### Related
|
| 110 |
|
| 111 | - [Types and Methods](ref/chap-type-method.html) in the [Oils
|
| 112 | Reference](ref/index.html)
|
| 113 | - [core/value.asdl]($oils-src)
|
| 114 |
|