2 ms·
Let me offer you an example: proc maybe {onno onjust val} { if {[llength $val] == 2 && [string equal [lindex $val 0] Just]} { lap
by thesz 5y ago
Let me offer you an example:
proc maybe {onno onjust val} {
if {[llength $val] == 2 && [string equal [lindex $val 0] Just]} {
lappend onjust [lrange $val 1 end]
return [uplevel $onjust]
}
# I cannot extract value
return [uplevel $onno]
}
maybe {puts "---"} {puts } {Just "huh?"}
maybe {puts "---"} {puts } {}
This is an example of custom control statement, modeled after Haskell's maybe [1].
[1] https://hackage.haskell.org/package/base-4.16.1.0/docs/Data-Maybe.html#v:maybe https://hackage.haskell.org/package/base-4.16.1.0/docs/Data-...
It is easy to use and write. You can follow algebraic data types convention and prefix values with tags and use something like control structure drafted above. I did that, it is relatively comfortable.
Note that special case of empty list also handled as if it is Nothing. So it is more useful than would appear. ;)