-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstructs_ex
More file actions
executable file
·41 lines (33 loc) · 1.23 KB
/
structs_ex
File metadata and controls
executable file
·41 lines (33 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/bash
#
# An example that illustrates `structs` and `constructors`.
readonly DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
. ${DIR}/../gobash
# `struct` and `constructor` are synonyms. In future examples, we will
# only use `struct` in text (but we really mean both at once).
function Request() {
# Normal function arguments in bash.
local -r url="${1}"
shift 1
# `make_` invocation differentiates a struct from other
# functions. `make_` allocates an instance. The first
# argument is the struct name (think of it as a type). struct
# name can be anything, but we will always use $FUNCNAME
# (which is `Request` in this example). Name is important when
# associating methods with a struct (and we love
# $FUNCNAME). The arguments that follow are the name and value
# of a field.
make_ $FUNCNAME \
"url" "${url}"
}
# Creating a `Request` instance is trivial.
req=$(Request "https://www.google.com")
# We can print the value of a field.
$req url
# We can also update the value.
$req url "https://www.google.com/maps"
# Print again.
$req url
# Output of this script will be:
# https://www.google.com
# https://www.google.com/maps