Skip to content

Commit

Permalink
Add constrained_types package to the standard library
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanTAllen committed Feb 6, 2024
1 parent 0da5bd4 commit 662335e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/constrained_types/_test.pony
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
37 changes: 37 additions & 0 deletions packages/constrained_types/constrained.pony
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
2 changes: 2 additions & 0 deletions packages/stdlib/_test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use capsicum = "capsicum"
use cli = "cli"
use collections = "collections"
use collections_persistent = "collections/persistent"
use constrained_types = "constrained_types"
use debug = "debug"
use files = "files"
use format = "format"
Expand Down Expand Up @@ -57,6 +58,7 @@ actor \nodoc\ Main is TestList
cli.Main.make().tests(test)
collections.Main.make().tests(test)
collections_persistent.Main.make().tests(test)
constrained_types.Main.make().tests()
files.Main.make().tests(test)
format.Main.make().tests(test)
ini.Main.make().tests(test)
Expand Down

0 comments on commit 662335e

Please sign in to comment.