OILS / doc / novelties.md View on Github | oilshell.org

122 lines, 70 significant
1---
2default_highlighter: oils-sh
3---
4
5Novelties in OSH and YSH
6========================
7
8Oils usually cleans up existing practice, rather than inventing brand new
9things. But here are a few features that may be unfamiliar.
10
11Related: [Known Differences](known-differences.html).
12
13<div id="toc">
14</div>
15
16## Global Options in the Interpreter
17
18The interpreter has a big list of global settings! Print them with `shopt -p`.
19
20This 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
28We use options to upgrade OSH to YSH:
29
30 shopt --set ysh:upgrade
31
32
33It's a bit like `use strict;` in Perl and JavaScript, or `from __future__` in
34Python.
35
36
37## The First Word of a Command
38
39The 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
44In YSH, mutation looks like this:
45
46 setvar x = 42
47 setvar x += 3
48
49Not 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
56In YSH, you use the `call` keyword to throw away the result of an expression.
57It's most often used with functions and methods:
58
59 call myFunc(x)
60
61 call mylist->pop()
62
63The `=` 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
71See [Command vs. Expression Mode](command-vs-expression-mode.html) for more.
72
73### Hay Case Sensitivity
74
75Attribute nodes start with capital letters, and this changes the parsing mode
76to 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
87These use `[]` instead of `()`:
88
89 assert [42 === x] # assert can pretty-print the expression
90
91 ls8 | where [size > 10] # not implemented
92
93It's motivated by idioms from Awk and R.
94
95## Three Quotation Types
96
97YSH is Lisp-y! These **unevaluated** quotation types don't appear in Python
98and 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
107TODO: Explain more.
108
109
110
111<!--
112
113Other: 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