fn: breaking - fix type variables for better inference

This commit is contained in:
Keagan McClelland
2024-08-12 12:11:32 -07:00
parent 9bbd327a10
commit a026d64c1b
4 changed files with 8 additions and 10 deletions

View File

@ -100,16 +100,14 @@ func TestPropToOptionIdentities(t *testing.T) {
e = NewRight[int, string](s) e = NewRight[int, string](s)
r2O := e.RightToOption() == Some(s) r2O := e.RightToOption() == Some(s)
o2R := e == OptionToRight[string, int, string]( o2R := e == OptionToRight(Some(s), i)
Some(s), i,
)
l2O := e.LeftToOption() == None[int]() l2O := e.LeftToOption() == None[int]()
return r2O && o2R && l2O return r2O && o2R && l2O
} else { } else {
e = NewLeft[int, string](i) e = NewLeft[int, string](i)
l2O := e.LeftToOption() == Some(i) l2O := e.LeftToOption() == Some(i)
o2L := e == OptionToLeft[int, int](Some(i), s) o2L := e == OptionToLeft(Some(i), s)
r2O := e.RightToOption() == None[string]() r2O := e.RightToOption() == None[string]()
return l2O && o2L && r2O return l2O && o2L && r2O

View File

@ -23,7 +23,7 @@ func Iden[A any](a A) A {
// Const is a function that accepts an argument and returns a function that // Const is a function that accepts an argument and returns a function that
// always returns that value irrespective of the returned function's argument. // always returns that value irrespective of the returned function's argument.
// This is also quite useful in conjunction with higher order functions. // This is also quite useful in conjunction with higher order functions.
func Const[A, B any](a A) func(B) A { func Const[B, A any](a A) func(B) A {
return func(_ B) A { return func(_ B) A {
return a return a
} }

View File

@ -216,7 +216,7 @@ func (o Option[A]) UnsafeFromSome() A {
// OptionToLeft can be used to convert an Option value into an Either, by // OptionToLeft can be used to convert an Option value into an Either, by
// providing the Right value that should be used if the Option value is None. // providing the Right value that should be used if the Option value is None.
func OptionToLeft[O, L, R any](o Option[O], r R) Either[O, R] { func OptionToLeft[O, R any](o Option[O], r R) Either[O, R] {
if o.IsSome() { if o.IsSome() {
return NewLeft[O, R](o.some) return NewLeft[O, R](o.some)
} }
@ -226,7 +226,7 @@ func OptionToLeft[O, L, R any](o Option[O], r R) Either[O, R] {
// OptionToRight can be used to convert an Option value into an Either, by // OptionToRight can be used to convert an Option value into an Either, by
// providing the Left value that should be used if the Option value is None. // providing the Left value that should be used if the Option value is None.
func OptionToRight[O, L, R any](o Option[O], l L) Either[L, O] { func OptionToRight[O, L any](o Option[O], l L) Either[L, O] {
if o.IsSome() { if o.IsSome() {
return NewRight[L, O](o.some) return NewRight[L, O](o.some)
} }
@ -239,7 +239,7 @@ func OptionToRight[O, L, R any](o Option[O], l L) Either[L, O] {
// and Some is converted to Ok. // and Some is converted to Ok.
func (o Option[A]) SomeToOk(err error) Result[A] { func (o Option[A]) SomeToOk(err error) Result[A] {
return Result[A]{ return Result[A]{
OptionToLeft[A, A, error](o, err), OptionToLeft(o, err),
} }
} }
@ -248,7 +248,7 @@ func (o Option[A]) SomeToOk(err error) Result[A] {
// ignored and Some is converted to Ok. // ignored and Some is converted to Ok.
func (o Option[A]) SomeToOkf(errString string, args ...interface{}) Result[A] { func (o Option[A]) SomeToOkf(errString string, args ...interface{}) Result[A] {
return Result[A]{ return Result[A]{
OptionToLeft[A, A, error](o, fmt.Errorf(errString, args...)), OptionToLeft(o, fmt.Errorf(errString, args...)),
} }
} }

View File

@ -427,7 +427,7 @@ func TestSingletonInitIsEmpty(t *testing.T) {
// empty slice. // empty slice.
func TestPropAlwaysNoneEmptyFilterMap(t *testing.T) { func TestPropAlwaysNoneEmptyFilterMap(t *testing.T) {
f := func(s []int) bool { f := func(s []int) bool {
filtered := FilterMap(s, Const[Option[int], int](None[int]())) filtered := FilterMap(s, Const[int](None[int]()))
return len(filtered) == 0 return len(filtered) == 0
} }