Standard Specialization Either where Left and Right are the same

Is there a standard specialization Eitherin Haskell or Scala's, which makes the types contained in Left, and Rightthe same type?

In Haskell, I want something like this:

data SpecializedEither a = Left a | Right a

It can also be considered a small generalization Maybethat forces one to Nothinghold a value.

edit: Ganesh raises a very good point that a Monad instance cannot be defined for this type. Is there a better way to do what I'm trying to do?

+4
source share
2 answers

There's a standard Monadinstance on((,) e) until eaMonoid

instance Monoid e => Monad ((,) e) where
  return a = (mempty, a)
  (e1, a) >>= f = let (e2, b) = f a in (e1 <> e2, b)

Either a a (Bool, a) ( ), Monad, a Monoid Bool. ( , . ) Monoid s, "" "". , , Left Right " ". Right (, , Left ),

data Either1 a = Left1 a | Right1 a

get1 :: Either1 a -> a
get1 (Left1 a) = a
get1 (Right1 a) = a

instance Monad Either1 where
  return = Right1
  x >>= f = case (x, f (get1 x)) of
    (Right1 _, Right1 b) -> Right1 b
    (Right1 _, Left1  b) -> Left1  b
    (Left1  _, y       ) -> Left1 (get1 y)
+11

:

type Foo[T] = Either[T, T]
val x: Foo[String] = Right("")
// Foo[String] = Right()
+1

Source: https://habr.com/ru/post/1544694/


All Articles