Warning: Work in progress! Leave feedback on Zulip or Github if you'd like this doc to be updated.

Oil Keywords

Related:

Table of Contents
Two Styles of Variable Declaration and Assignment
Legacy Style: readonly, local, name=val
Oil's Style: const, var, setvar, setglobal, and setref
Expressions Go on the Right
= Pretty Prints an Expression
_ Ignores an Expression
Other Kinds of Assignment
auto for Autovivification (future, not implemented)
Notes and Examples
Mutating Arrays
Mutating Associative Arrays
proc Disables Dynamic Scope

Two Styles of Variable Declaration and Assignment

Legacy Style: readonly, local, name=val

They don't allow expressions on the right.

Oil's Style: const, var, setvar, setglobal, and setref

See the doc on variables for details.

Expressions Go on the Right

Just like with assignments.

= Pretty Prints an Expression

Useful interactively.

$ = 'foo'
(Str)   'foo'

$ = :| one two |
(StrArray)   ['one', 'two']

_ Ignores an Expression

Think of this:

_ f(x)

as a shortcut for:

_ = f(x)  # assign to "meh" variable

Other Kinds of Assignment

auto for Autovivification (future, not implemented)

auto count += 1

auto hist['key'] += 1

auto total += 3.5
auto hist['key'] += 4.6

Notes and Examples

Mutating Arrays

Use setvar:

Shell:

a=(one two three)
a[0]=zz

Oil:

var a = :| one two three |
setvar a[0] = 'zz'  # also acceptable

Mutating Associative Arrays

Shell:

declare -A A=(['name']=foo ['type']='dir')
A['type']=file

Oil:

var A = {name: 'foo', type: 'dir'}
setvar A['type'] = 'file'  # also acceptable

proc Disables Dynamic Scope

Recall that procs are the way to declare shell-like functions in Oil.

proc p {
  echo one
  echo two
}

p > file.txt

They mostly look like and work like shell functions, but they change scoping rules.

Generated on Sat, 27 Jul 2024 17:23:15 +0000