| 1 | ---
|
| 2 | default_highlighter: oils-sh
|
| 3 | ---
|
| 4 |
|
| 5 | Novelties in OSH and YSH
|
| 6 | ========================
|
| 7 |
|
| 8 | Oils usually cleans up existing practice, rather than inventing brand new
|
| 9 | things. But here are a few features that may be unfamiliar.
|
| 10 |
|
| 11 | Related: [Known Differences](known-differences.html).
|
| 12 |
|
| 13 | <div id="toc">
|
| 14 | </div>
|
| 15 |
|
| 16 | ## Global Options in the Interpreter
|
| 17 |
|
| 18 | The interpreter has a big list of global settings! Print them with `shopt -p`.
|
| 19 |
|
| 20 | This idea comes from shell, and they can be tamed with blocks to `shopt`:
|
| 21 |
|
| 22 | shopt --unset errexit {
|
| 23 | touch /let
|
| 24 | touch /it
|
| 25 | touch /fail
|
| 26 | }
|
| 27 |
|
| 28 | We use options to upgrade OSH to YSH:
|
| 29 |
|
| 30 | shopt --set ysh:upgrade
|
| 31 |
|
| 32 |
|
| 33 | It's a bit like `use strict;` in Perl and JavaScript, or `from __future__` in
|
| 34 | Python.
|
| 35 |
|
| 36 |
|
| 37 | ## The First Word of a Command
|
| 38 |
|
| 39 | The Python-like features in YSH have to co-exist with shell like `echo "hello
|
| 40 | $name"`, so there are more "first words".
|
| 41 |
|
| 42 | ### Mutation Uses the `setvar` Keyword
|
| 43 |
|
| 44 | In YSH, mutation looks like this:
|
| 45 |
|
| 46 | setvar x = 42
|
| 47 | setvar x += 3
|
| 48 |
|
| 49 | Not like JavaScript or Python
|
| 50 |
|
| 51 | x = 42 # doesn't work
|
| 52 | x += 3 # nope
|
| 53 |
|
| 54 | ### Evaluate Expressions with the `call` and `=` Keywords
|
| 55 |
|
| 56 | In YSH, you use the `call` keyword to throw away the result of an expression.
|
| 57 | It's most often used with functions and methods:
|
| 58 |
|
| 59 | call myFunc(x)
|
| 60 |
|
| 61 | call mylist->pop()
|
| 62 |
|
| 63 | The `=` operator works the same way, but prints the return value:
|
| 64 |
|
| 65 | $ = mylist->pop() # pretty-print result with = operator
|
| 66 | (Str) "x"
|
| 67 |
|
| 68 | $ = 42 + a[i]
|
| 69 | (Int) 43
|
| 70 |
|
| 71 | See [Command vs. Expression Mode](command-vs-expression-mode.html) for more.
|
| 72 |
|
| 73 | ### Hay Case Sensitivity
|
| 74 |
|
| 75 | Attribute nodes start with capital letters, and this changes the parsing mode
|
| 76 | to allow "bare" assignment:
|
| 77 |
|
| 78 | hay define Package
|
| 79 |
|
| 80 | Package {
|
| 81 | name = 'cpython' # assignment without var/setvar keyword
|
| 82 | version = '3.12'
|
| 83 | }
|
| 84 |
|
| 85 | ## Lazy Arg Lists
|
| 86 |
|
| 87 | These use `[]` instead of `()`:
|
| 88 |
|
| 89 | assert [42 === x] # assert can pretty-print the expression
|
| 90 |
|
| 91 | ls8 | where [size > 10] # not implemented
|
| 92 |
|
| 93 | It's motivated by idioms from Awk and R.
|
| 94 |
|
| 95 | ## Three Quotation Types
|
| 96 |
|
| 97 | YSH is Lisp-y! These **unevaluated** quotation types don't appear in Python
|
| 98 | and JS:
|
| 99 |
|
| 100 | var myblock = ^(ls /tmp | wc -l)
|
| 101 |
|
| 102 | var myexpr = ^[age > 10] # use evalExpr()
|
| 103 |
|
| 104 | var mytemplate = ^"$name is $age years old" # not implemented
|
| 105 |
|
| 106 |
|
| 107 | TODO: Explain more.
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 | <!--
|
| 112 |
|
| 113 | Other: value.Place could be unfamliar to Python/JS users. It's based on C/C++
|
| 114 | (but safe), and Rust also uses a similar syntax.
|
| 115 |
|
| 116 | -->
|
| 117 |
|
| 118 | ## Related
|
| 119 |
|
| 120 | - [Quirks](quirks.html) is about OSH.
|
| 121 | - [Warts](warts.html) is about YSH.
|
| 122 |
|