-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcasting.spd
More file actions
executable file
·39 lines (29 loc) · 885 Bytes
/
casting.spd
File metadata and controls
executable file
·39 lines (29 loc) · 885 Bytes
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
#!/usr/local/bin/speedie
main
"Type-casting example!"
|| s = "hello(world)".parse
|| losing_type = s|object| // losing type-info is ok
if s isa Message
"Approach 1: '$s' was tested using 'isa'"
if s.mustbe(message, s)
"Approach 2: '$s' was tested using 'mustbe'. Creates errors if s is not a message"
|| msg = s as Message
"Approach 3: '$s' was tested using 'as'"
target !ForBatchTest {
|| ThisLineCompilesOK = 1
|| blindly = losing_type|message| // comment this out, to get the code working! :)
}
testCasting()
class Vehicle
|String| name
class Car (Vehicle)
|string| brand
constructor (|String| name, |String| brand)
super.constructor(name)
.brand = brand
function TestCasting
|Vehicle| myVehicle = Car("My Car", "Tesla")
|| myCar = myVehicle as Car
"My car's brand is ${myCar.brand}"
else
"myVehicle is not a Car"