feat: tipos core Option[T], Pair[A,B] y Error base
Tres tipos nuevos en core: - Option[T]: tipo suma Some/None para valores opcionales - Pair[A,B]: tipo producto genérico para agrupar dos valores - Error: tipo de error base referenciado como error_type por impuras Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -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"
|
||||
---
|
||||
@@ -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
|
||||
}
|
||||
@@ -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.
|
||||
@@ -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
|
||||
}
|
||||
@@ -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"
|
||||
---
|
||||
Reference in New Issue
Block a user