No description
Find a file
2015-02-09 22:20:19 -05:00
src No need for automatic boxing 2015-01-31 17:57:48 -05:00
.gitignore Move things to src directory 2015-01-31 15:44:11 -05:00
optional_t.nimble Bump version number 2015-02-09 22:20:19 -05:00
readme.asciidoc Fix typo in readme 2015-01-31 18:51:37 -05:00

= optional_t

== What is optional_t
A simple library for an `Option[T]`. The goal is to have clear syntax and be
easy to use.

== Usage

=== Construction

- `Some("some value")` creates an `Option[string]` that has a value.
- `None[string]()` creates a value of the same type, but without a value.
- `var foo: Option[string]` has a value of `None[String]()`. In other words,
  `foo == None[String]()`.
- `Some[string](nil)` is invalid: the value inside a `Some[ref T]` is required
  to never be nil. This is only validated in debug mode, the check is disabled
  in release mode unless assertions are enabled.

=== Operations

`isNone(Option[T]): bool` ::
  returns `true` if the `Option[T]` is `None`, otherwise returns `false`.
`isSome(Option[T]): bool` ::
  returns `not isNone(...)`
`get(Option[T], [default: T]): T` ::
  Gets the value from the `Option[T]`. If the `Option[T]` is `None`, it raises
  an exception unless `default` is passed, in which case it returns `default`.
`unsafeGet(Option[T]): T` ::
  Gets the value inside the `Option[T]`, behavior is undefined if it is `None`.
  You probably don't need this, the C optimizer is pretty good. Worry about the
  humans, not the computer.
`map(Option[T], proc(T): R): R` ::
  If the `Option[T]` is not `None`, the passed procedure is executed with it's
  value as a parameter, otherwise a `None` is returned without executing the
  passed procedure.
`if myOptionT: ...` ::
  The `if` condition evaluates to `true` if the value is `Some`, otherwise it
  evaluates to `false`.

`$` and `==` work as expected.

*Note*: If `optional_t.nonstrict` is imported, then all `Option[T]` s are
automatically unpacked where necessary. This uses a converter, so the laziness
might lead to odd errors.