diff --git a/types/core/error.go b/types/core/error.go new file mode 100644 index 00000000..2098bb57 --- /dev/null +++ b/types/core/error.go @@ -0,0 +1,11 @@ +package core + +// Error is the standard error type for impure functions in the registry. +// Wraps a message string. Used as error_type reference. +type Error struct { + Message string +} + +func (e Error) Error() string { + return e.Message +} diff --git a/types/core/error.md b/types/core/error.md new file mode 100644 index 00000000..52de7883 --- /dev/null +++ b/types/core/error.md @@ -0,0 +1,15 @@ +--- +name: error +lang: go +domain: core +version: "1.0.0" +algebraic: product +definition: | + type Error struct { + Message string + } +description: "Tipo de error base del registry. Referenciado como error_type por funciones impuras." +tags: [error, base, impure] +uses_types: [] +file_path: "types/core/error.go" +--- diff --git a/types/core/option.go b/types/core/option.go new file mode 100644 index 00000000..4b3c0cde --- /dev/null +++ b/types/core/option.go @@ -0,0 +1,43 @@ +package core + +// Option represents a value that may or may not be present. +// A sum type: Some(T) | None. +type Option[T any] struct { + value *T +} + +// Some creates an Option containing a value. +func Some[T any](v T) Option[T] { + return Option[T]{value: &v} +} + +// None creates an empty Option. +func None[T any]() Option[T] { + return Option[T]{} +} + +// IsSome returns true if the Option contains a value. +func (o Option[T]) IsSome() bool { + return o.value != nil +} + +// IsNone returns true if the Option is empty. +func (o Option[T]) IsNone() bool { + return o.value == nil +} + +// Unwrap returns the value or panics if None. +func (o Option[T]) Unwrap() T { + if o.value == nil { + panic("called Unwrap on None") + } + return *o.value +} + +// UnwrapOr returns the value or a default if None. +func (o Option[T]) UnwrapOr(def T) T { + if o.value != nil { + return *o.value + } + return def +} diff --git a/types/core/option.md b/types/core/option.md new file mode 100644 index 00000000..98395988 --- /dev/null +++ b/types/core/option.md @@ -0,0 +1,19 @@ +--- +name: option +lang: go +domain: core +version: "1.0.0" +algebraic: sum +definition: | + type Option[T any] struct { + value *T + } +description: "Tipo suma generico que representa un valor opcional: Some(T) o None. Alternativa a punteros nil para modelar ausencia de valor de forma explicita." +tags: [option, sum, nullable, functional, generic] +uses_types: [] +file_path: "types/core/option.go" +--- + +## Notas + +Tipo suma con dos variantes: Some(T) y None. Inspirado en Option de Rust. diff --git a/types/core/pair.go b/types/core/pair.go new file mode 100644 index 00000000..f490fe4f --- /dev/null +++ b/types/core/pair.go @@ -0,0 +1,7 @@ +package core + +// Pair holds two values of potentially different types. +type Pair[A any, B any] struct { + First A + Second B +} diff --git a/types/core/pair.md b/types/core/pair.md new file mode 100644 index 00000000..d94d2742 --- /dev/null +++ b/types/core/pair.md @@ -0,0 +1,16 @@ +--- +name: pair +lang: go +domain: core +version: "1.0.0" +algebraic: product +definition: | + type Pair[A any, B any] struct { + First A + Second B + } +description: "Tipo producto generico que agrupa dos valores de tipos potencialmente distintos. Util para ZipSlices y operaciones que devuelven dos resultados." +tags: [pair, tuple, product, generic] +uses_types: [] +file_path: "types/core/pair.go" +---