From 5e8e7ae97975c7fdba45daa764e6de8a7f94d02e Mon Sep 17 00:00:00 2001 From: Olivier NOUGUIER Date: Wed, 28 Aug 2024 17:41:12 +0200 Subject: [PATCH] feat: Add sealed classes example to client code The code changes in this commit add a new file `Sealed.scala` to the `examples/client/src/main/scala/samples` directory. This file contains an example of using sealed classes in Scala. It defines a sealed trait `Animal` and two case classes `Horse` and `Lama` that extend the `Animal` trait. It also includes a case class `Owner` that has a `name` and a `pet` field of type `Animal`. The commit also updates the `HelloWorld.scala` file to include the new `sealedClasses` sample component. Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> --- .../client/src/main/scala/HelloWorld.scala | 1 + .../src/main/scala/samples/Sealed.scala | 29 +++++++++++++++++++ .../client/src/main/scala/samples/Tree.scala | 3 +- 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 examples/client/src/main/scala/samples/Sealed.scala diff --git a/examples/client/src/main/scala/HelloWorld.scala b/examples/client/src/main/scala/HelloWorld.scala index 59e3c67..a0d285d 100644 --- a/examples/client/src/main/scala/HelloWorld.scala +++ b/examples/client/src/main/scala/HelloWorld.scala @@ -17,6 +17,7 @@ object App extends App { private val demos = Seq( samples.simple, + samples.sealedClasses, samples.either, samples.enums, samples.person, diff --git a/examples/client/src/main/scala/samples/Sealed.scala b/examples/client/src/main/scala/samples/Sealed.scala new file mode 100644 index 0000000..21f9d23 --- /dev/null +++ b/examples/client/src/main/scala/samples/Sealed.scala @@ -0,0 +1,29 @@ +package samples + +import dev.cheleb.scalamigen.{*, given} + +import com.raquo.laminar.api.L.* + +import com.raquo.airstream.state.Var + +sealed trait Animal +case class Horse(name: String, age: Int) extends Animal +case class Lama(name: String, age: Int, splitDistance: Int) extends Animal + +case class Owner(name: String, pet: Animal) + +val sealedClasses = Sample( + "Sealed", { + + val eitherVar = Var(Owner("Agnes", Horse("Scala le chat", 6))) + + div( + child <-- eitherVar.signal.map { item => + div( + s"$item" + ) + }, + eitherVar.asForm + ) + } +) diff --git a/examples/client/src/main/scala/samples/Tree.scala b/examples/client/src/main/scala/samples/Tree.scala index 53a70a8..830bec6 100644 --- a/examples/client/src/main/scala/samples/Tree.scala +++ b/examples/client/src/main/scala/samples/Tree.scala @@ -17,7 +17,8 @@ object Tree: case (Empty, Node(_, _, _)) => false case (Node(_, left1, right1), Node(_, left2, right2)) => isSameStructure(left1, left2) && isSameStructure(right1, right2) -implicit def treeInstance[A](using + +given treeInstance[A](using default: Defaultable[A] )(using Form[A]): Form[Tree[A]] = new Form[Tree[A]] { self =>