/* ----------------------------------------------------------------------------- Copyright 2019-2022 Kevin P. Barry Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ----------------------------------------------------------------------------- */ // Author: Kevin P. Barry [ta0kira@gmail.com] define Void { @category Void singleton <- Void{ } default () { return singleton } duplicate () { return singleton } formatted () { return "Void" } equals (_, _) { return true } lessThan (_, _) { return false } hashed () { return 123456789 } } define AlwaysEmpty { @category AlwaysEmpty singleton <- AlwaysEmpty{ } default () { return singleton } size () { return 0 } duplicate () { return singleton } defaultOrder () { return empty } readAt (_) { fail("container is always empty") } writeAt (_, _) { fail("container is always empty") } } define ErrorOr { @value optional #x maybeValue @value optional Formatted maybeError value (x) { return ErrorOr<#x>{ x, empty } } error (message) { return ErrorOr{ empty, message } } isError () { return !present(maybeValue) && present(maybeError) } getValue () { if (present(maybeValue)) { return require(maybeValue) } else { fail(require(maybeError)) } } tryValue () { return maybeValue } getError () { return require(maybeError) } convertError () { scoped { optional ErrorOr error <- reduce<#self, ErrorOr>(self) } in if (present(error)) { return require(error) } else { fail("no error present to convert") } } }