Covariant trees in Scala

Looking at scalaz.Tree[A], it is invariant in A. I am looking for a multi-level tree that I can reset hierarchy values ​​to

eg. if i have ADT

trait MyThing
case object Thing1 extends MyThing
case object Thing2 extends MyThing

And I want the tree MyThings, I cannot use it without casting to MyThingin scalaz;

import scalaz.{Scalaz, Tree}
import Scalaz._
val tree = Thing1.asInstanceOf[MyThing].
               node(Thing2.asInstanceOf[MyThing].leaf)

This is a bit of a pain.

  • Is there a covariant [+ A] version of the tree?
  • Why is the first tree invariant?
+4
source share
1 answer

, Huw , downcasting asInstanceOf. , , , - , . , asInstanceOf . asInstanceOf , downcasting .

: , Scalaz , pull request by Huw . , Scalaz - ADT, .. .

ADT (, Haskell OCaml, ), ADT ADT, Scala messy, :

scala> List(1, 2, 3).foldLeft(None)((_, i) => Some(i))
<console>:14: error: type mismatch;
 found   : Some[Int]
 required: None.type
              List(1, 2, 3).foldLeft(None)((_, i) => Some(i))
                                                     ^

foldLeft, None.type, . ( foldLeft), .

Scalaz , ADT, ADT. , none[A] some[A](a: A) Option, Option[A].

( . ).

, :

val thing1: MyThing = Thing1
val thing2: MyThing = Thing2

thing1.node(thing2.leaf). , Argonaut Json ADT ADT-, .

+2

All Articles