-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1044 from hylo-lang/collection
Add a bare bone 'Collection' trait to the standard library
- Loading branch information
Showing
3 changed files
with
65 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/// A collection of elements accessible via an indexed subscript. | ||
public trait Collection { | ||
|
||
/// The type of the elements contained in `Self`. | ||
type Element | ||
|
||
/// The type of the positions in `Self`. | ||
type Index: SemiRegular | ||
|
||
/// Returns the position of the first element in `self`, or `end_index()` if `self` is empty. | ||
fun start_index() -> Index | ||
|
||
/// Returns the "past the end" position in `self`, that is, the position immediately after the | ||
/// last element in `self`. | ||
fun end_index() -> Index | ||
|
||
/// Returns the position immediately after `i`. | ||
/// | ||
/// - Requires: `i != end_index()`. | ||
fun index(after i: Index) -> Index | ||
|
||
/// Accesses the elment at position `i`. | ||
subscript(_ i: Index): Element { let } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
Tests/HyloTests/TestCases/TypeChecking/ConformanceWithSubscript.hylo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//- typeCheck expecting: success | ||
|
||
trait P { | ||
type X | ||
subscript(_ i: Int): X { let } | ||
} | ||
|
||
type B: P { | ||
typealias X = Bool | ||
subscript(_ i: Int): Bool { true } | ||
} |