-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ox
More file actions
87 lines (68 loc) · 1.87 KB
/
main.ox
File metadata and controls
87 lines (68 loc) · 1.87 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Null Coalescing Operator Example in Oxide
# The ?? operator provides default values for null/empty values
print "=== Null Coalescing Operator (??) ==="
print ""
# Basic usage with empty strings
name = ""
displayName = name ?? "Anonymous"
print "Empty name ?? 'Anonymous' = " + displayName
# With actual value
actualName = "John"
result = actualName ?? "Anonymous"
print "'John' ?? 'Anonymous' = " + result
# Chaining null coalescing
print ""
print "=== Chaining ?? Operators ==="
first = ""
second = ""
third = "Fallback"
final = first ?? second ?? third
print "'' ?? '' ?? 'Fallback' = " + final
# Practical example: User configuration
print ""
print "=== Practical: User Configuration ==="
# Simulating optional configuration values
userTheme = ""
systemTheme = ""
defaultTheme = "light"
theme = userTheme ?? systemTheme ?? defaultTheme
print "Selected theme: " + theme
# With user preference set
userTheme = "dark"
theme = userTheme ?? systemTheme ?? defaultTheme
print "With user preference: " + theme
# Working with function returns
print ""
print "=== With Function Returns ==="
func getUsername()
return ""
endfunc
func getGuestName()
return "Guest_123"
endfunc
username = getUsername() ?? getGuestName()
print "Username: " + username
# Using with dictionaries
print ""
print "=== With Dictionary Values ==="
config = {"host": "localhost", "port": "", "debug": "true"}
host = config["host"] ?? "127.0.0.1"
port = config["port"] ?? "8080"
timeout = config["timeout"] ?? "30"
print "Host: " + host
print "Port: " + port
print "Timeout: " + timeout + "s"
# Comparison with traditional approach
print ""
print "=== Traditional vs ?? Operator ==="
value = ""
# Traditional way
if value == ""
traditional = "default"
else
traditional = value
endif
# With ?? operator
modern = value ?? "default"
print "Traditional if/else: " + traditional
print "Using ?? operator: " + modern