2010-06-12 19:05:13 -04:00
|
|
|
ok(if mySpecialVariable? then false else true)
|
2010-01-01 12:41:55 -05:00
|
|
|
|
2010-06-12 19:05:13 -04:00
|
|
|
mySpecialVariable: false
|
2010-01-01 12:41:55 -05:00
|
|
|
|
2010-06-12 19:05:13 -04:00
|
|
|
ok(if mySpecialVariable? then true else false)
|
2010-01-16 23:03:54 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Existential assignment.
|
|
|
|
a: 5
|
|
|
|
a: null
|
|
|
|
a ?= 10
|
|
|
|
b ?= 10
|
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok a is 10 and b is 10
|
2010-01-16 23:03:54 -05:00
|
|
|
|
|
|
|
|
|
|
|
# The existential operator.
|
|
|
|
z: null
|
|
|
|
x: z ? "EX"
|
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok z is null and x is "EX"
|
2010-01-17 14:23:41 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Only evaluate once.
|
|
|
|
counter: 0
|
2010-06-12 19:05:13 -04:00
|
|
|
getNextNode: ->
|
2010-01-17 14:23:41 -05:00
|
|
|
throw "up" if counter
|
|
|
|
counter++
|
|
|
|
|
2010-06-12 19:05:13 -04:00
|
|
|
ok(if getNextNode()? then true else false)
|
2010-01-23 23:30:55 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Existence chains, soaking up undefined properties:
|
|
|
|
obj: {
|
|
|
|
prop: "hello"
|
|
|
|
}
|
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok obj?.prop is "hello"
|
2010-01-23 23:30:55 -05:00
|
|
|
|
2010-02-24 00:06:01 -05:00
|
|
|
ok obj?['prop'] is "hello"
|
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok obj.prop?.length is 5
|
2010-02-02 10:39:44 -05:00
|
|
|
|
2010-02-24 00:06:01 -05:00
|
|
|
ok obj?['prop']?['length'] is 5
|
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok obj?.prop?.non?.existent?.property is undefined
|
2010-01-24 12:52:15 -05:00
|
|
|
|
2010-02-24 00:06:01 -05:00
|
|
|
ok obj?['non']?['existent'].property is undefined
|
|
|
|
|
2010-01-24 12:52:15 -05:00
|
|
|
|
|
|
|
# Soaks and caches method calls as well.
|
|
|
|
arr: ["--", "----"]
|
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok arr.pop()?.length is 4
|
|
|
|
ok arr.pop()?.length is 2
|
|
|
|
ok arr.pop()?.length is undefined
|
|
|
|
ok arr[0]?.length is undefined
|
|
|
|
ok arr.pop()?.length?.non?.existent()?.property is undefined
|
2010-02-20 20:00:07 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Soaks method calls safely.
|
|
|
|
value: undefined
|
|
|
|
result: value?.toString().toLowerCase()
|
|
|
|
|
|
|
|
ok result is undefined
|
|
|
|
|
|
|
|
value: 10
|
|
|
|
result: value?.toString().toLowerCase()
|
|
|
|
|
|
|
|
ok result is '10'
|
|
|
|
|
2010-02-22 22:11:47 -05:00
|
|
|
|
2010-07-15 09:08:51 -04:00
|
|
|
# Soaks constructor invocations.
|
|
|
|
a: 0
|
|
|
|
class Foo
|
|
|
|
constructor: -> a += 1
|
|
|
|
bar: "bat"
|
|
|
|
|
|
|
|
ok (new Foo())?.bar is 'bat'
|
|
|
|
ok a is 1
|
|
|
|
|
|
|
|
|
2010-02-22 22:11:47 -05:00
|
|
|
# Safely existence test on soaks.
|
|
|
|
result: not value?.property?
|
|
|
|
ok result
|
|
|
|
|
2010-05-05 05:58:48 -04:00
|
|
|
|
|
|
|
# Safely calls values off of non-existent variables.
|
|
|
|
result: nothing?.value
|
|
|
|
ok result is undefined
|
2010-07-10 11:27:43 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Assign to the result of an exsitential operation with a minus.
|
|
|
|
x: null ? - 1
|
|
|
|
ok x is - 1
|