creativescala / doodle

Compositional vector graphics in Scala / Scala.JS
https://creativescala.org/doodle/
Apache License 2.0
327 stars 75 forks source link

Implementing Clipping with Reified #159

Open VOSID8 opened 4 months ago

VOSID8 commented 4 months ago

I'm working keeping in mind the other Java2D algebras as reference utilizing reified data structure and package. I'm facing bit of a trouble with expressing relationships between types with ReifiedClip and Reified.

  val ClipApi = new ClipApi {
    def clip[A](
        tx: Tx,
        img: Drawing[A],
        clipPath: ClosedPath
    ): Reification[A] = {
      WriterT.tell(List(Reified.clip(tx, img, clipPath)))
    }
  }
[error] -- [E007] Type Mismatch Error: C:\Projects\doodle\java2d\src\main\scala\doodle\java2d\algebra\reified\ReifiedClip.scala:44:6 
[error] 44 |      WriterT.tell(List(Reified.clip(tx,img, clipPath)))
[error]    |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[error]    |      Found:    cats.data.WriterT[[A] =>> doodle.java2d.Drawing[A],
[error]    |        List[doodle.java2d.algebra.reified.Reified], Unit]
[error]    |      Required: doodle.java2d.algebra.reified.Reification[A┬▓]
[error]    |
[error]    |      where:    A  is a type variable with constraint
[error]    |                A┬▓ is a type in method clip
noelwelsh commented 4 months ago

The source of the error you are seeing comes from the type parameter C you added to the Clip reified instruction (and you also added a type parameter to render). This approach won't work with Reified: it is intended to be a simple list of instructions (a "linear IR") without any nesting. The way you have represented Clip has nesting, which introduces the type parameter that is causing you problems. You need to get rid of the nesting, and then things will work.

I've added some additional documentation to Reified that explains how the instructions should be represented (a linear IR) and describes how you can solve the problem you'll encounter when you try to implement clipping in this representation.