The Maybe package defines a set of commands for dealing with unknown/missing data (see null) in situations where you cannot simply omit that information or use some suitable default value (such as ""). The package provides two data constructors, which are simply commands that return specially formatted strings that exactly resemble tagged lists:
package require maybe 1.0 set a [maybe Nothing] ;# returns Nothing set b [maybe Just 12] ;# returns {Just 12}
In addition, the package also provides an exists subcommand that checks if a value is not Nothing and optionally extracts the value from a Just constructor in one go:
proc show a { if {[maybe exists $a val]} { puts "a = $val" } else { puts "a is unknown" } } show [maybe Nothing] ;# prints "a is unknown" show [maybe Just "Hello, World!"] ;# prints "a = Hello, World!"
The advantage of the package is that any string is valid within a Just constructor, including the strings "Just" and "Nothing", and you can reliably distinguish between a value being empty "", and not being present at all. The datatype can also be nested, so e.g. the following are valid:
maybe Just [maybe Nothing] maybe Just [maybe Just 42]
The package is available with both pure-Tcl and efficient C implementations in source-code form from [L1 ] and is licensed under the same liberal terms as Tcl itself. It was written by NEM based on the Maybe datatype in Haskell and other functional programming languages.