Skip to content

Optional

IsaacShelton edited this page Nov 14, 2022 · 7 revisions

Optional

Optional represents the type of a value that might exist.

Specifications

Type Size Memory Management Model File
Optional ? bytes Scoped 2.7/Optional.adept

Definition

struct <$T> Optional (value $T, has bool)

where

$T is any valid type

Fields

Name Type Description
has bool Whether a value exists
value $T Wrapped value, only exists if has is true

Value Management Definitions

  • verbatim func __defer__(this *<$T> Optional)
  • func __assign__(this *<$T> Optional, other <$T> Optional)

Memory Management

optional.value is freed at the end of the scope if optional.has is true.

Functions

  • func none() <$T> Optional

    Returns an empty optional. Should be used with ~> operator like none() ~> <int> Optional.

    [view src]

  • func some(contained POD $T) <$T> Optional

    Returns an optional with a contained value. The contained value is copied byte for byte, so some values may need modifications before being given to this function.

    [view src]

Methods

  • func set(this *<$T> Optional, value POD $T) void

    Sets an optional to a value. The value is copied byte for byte, so some values may need modifications before being given to this function.

    [view src]

  • func get(this *<$T> Optional) $T

    Gets the contained value of an optional.

    [view src]

  • func getPointer(this *<$T> Optional) *$T

    Gets a pointer to the contained value of an optional.

    [view src]

  • func rid(this *<$T> Optional) void

    Sets an optional to not contain anything.

    [view src]

  • func assign(this *<$T> Optional, value POD $T) void

    Destroys and clears the internal value of an optional, and then stores the new value using standard assignment semantics

    this.value.__defer__()
    memset(&this.value, 0, sizeof(this.value))
    this.value = value
    this.has = true
    

    [view src]

  • func assignPOD(this *<$T> Optional, value POD $T) void

    Destroys and clears the internal value of an optional, and then stores the new value using POD assignment semantics

    this.value.__defer__()
    memset(&this.value, 0, sizeof(this.value))
    this.value = POD value
    this.has = true
    

    [view src]

Undocumented Functions

These functions are undocumented as they are likely to change in the future.

  • func isSome(this *<$T> Optional, out captured *$T) bool
  • func map(this *<$T> Optional, function func($~T) $S) <$S> Optional
  • func mapPOD(this *<$T> Optional, function func($~T) $S) <$S> Optional
  • func pass(this *<$T> Optional) <$T> Optional

Disallowed functions

  • func toString(_ <$T> Optional) String = delete {}

    Since it cannot be known whether we are responsible for freeing _.value, this call makes no sense

    [view src]

Settings

#default Optional_error_no_value true
Clone this wiki locally