Skip to content

Commit

Permalink
Merge pull request #1079 from hylo-lang/optional
Browse files Browse the repository at this point in the history
Add 'Optional' to the standard library
  • Loading branch information
kyouko-taiga authored Oct 11, 2023
2 parents 92f2b69 + 717a508 commit b61116b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Library/Hylo/Core/Optional.hylo
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/// Either an instance of `T` or the absence thereof.
///
/// Use `Optional` when you need to represent a value that may or may not be present.
public typealias Optional<T> = Union<T, None<T>>

/// The absence of an instance of `T`.
public type None<T> {

/// Creates a value denoting the absence of an instance of `T`.
public memberwise init

}

public conformance None: Regular {

// TODO: Remove when #1078 is implemented.
public fun copy() -> Self {
None()
}

// TODO: Remove when #1078 is implemented.
public fun infix==(_ other: Self) -> Bool {
true
}

}
11 changes: 11 additions & 0 deletions Tests/LibraryTests/TestCases/OptionalTests.hylo
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//- compileAndRun expecting: success

public fun main() {
var x: Optional<Int> = 42
let y = if let i: Int = x { i.copy() } else { 0 }
precondition(y == 42)

&x = None()
let z = if let i: Int = x { i.copy() } else { 0 }
precondition(z == 0)
}

0 comments on commit b61116b

Please sign in to comment.