-
-
Notifications
You must be signed in to change notification settings - Fork 419
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add constrained_types package to the standard library
Implements RFC 79. https://github.com/ponylang/rfcs/blob/main/text/0079-constrained-types.md Closes #4492
- Loading branch information
1 parent
0da5bd4
commit 662335e
Showing
3 changed files
with
47 additions
and
0 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,8 @@ | ||
use "pony_test" | ||
|
||
actor \nodoc\ Main is TestList | ||
new create(env: Env) => PonyTest(env, this) | ||
new make() => None | ||
|
||
fun tag tests(test: PonyTest) => | ||
None |
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,37 @@ | ||
type ValidationResult is (ValidationSuccess | ValidationFailure) | ||
|
||
primitive ValidationSuccess | ||
|
||
class val ValidationFailure | ||
let _errors: Array[String val] = _errors.create() | ||
|
||
new create(e: (String val | None) = None) => | ||
match e | ||
| let s: String val => _errors.push(s) | ||
end | ||
|
||
fun ref apply(e: String val) => | ||
_errors.push(e) | ||
|
||
fun errors(): this->Array[String val] => | ||
_errors | ||
|
||
interface val Validator[T] | ||
new val create() | ||
fun apply(i: T): ValidationResult | ||
|
||
class val Constrained[T: Any val, F: Validator[T]] | ||
let _value: T val | ||
|
||
new val _create(value: T val) => | ||
_value = value | ||
|
||
fun val apply(): T val => | ||
_value | ||
|
||
primitive MakeConstrained[T: Any val, F: Validator[T] val] | ||
fun apply(value: T): (Constrained[T, F] | ValidationFailure) => | ||
match F(value) | ||
| ValidationSuccess => Constrained[T, F]._create(value) | ||
| let e: ValidationFailure => e | ||
end |
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