Translate Haskell Class-based ad-hoc polymorphism example to Scala

Hi @LPTK, right, let me see, I switched from bundling the two givens in an object to just having them on their own because I thought the importing ergonomics were reasonable. I was exploring the expression problem [1], and so I didn’t want to be forced to reopen an object in order to add a new given to it, I wanted additivity, the ability to add a new given in a new file, i.e. without having to touch existing files.

I just had a go at putting each program element in its own package, just to see what that forces me to do in terms of imports, and here is what I am seeing:

package explore.traits

trait Shape[A]:
  extension (shape: A)
    def area: Double

======================================================

package explore.data.circle

case class Circle(radius: Float)

======================================================

package explore.data.rect

case class Rect(length: Float, width: Float)

======================================================

package explore.givens.circle

import explore.traits.Shape
import explore.data.circle.Circle

given Shape[Circle] with
  extension (c: Circle)
    def area: Double = math.Pi * c.radius * c.radius

======================================================

package explore.givens.rect

import explore.traits.Shape
import explore.data.rect.Rect

given Shape[Rect] with
  extension (r: Rect)
    def area: Double = r.length * r.width

======================================================

package explore.main

import explore.data.circle.Circle
import explore.data.rect.Rect
import explore.givens.rect.given
import explore.givens.circle.given

@main def main: Unit =
  assert(Rect(2,3).area == 6)
  assert(Circle(1).area == math.Pi)

======================================================

Does the above showcase the ‘import inconvenience’ that you are keen to avoid, or possibly dispel your concerns?

[1]
https://homepages.inf.ed.ac.uk/wadler/papers/expression/expression.txt

http://channel9.msdn.com/shows/Going+Deep/C9-Lectures-Dr-Ralf-Lmmel-Advanced-Functional-Programming-Type-Classes/
http://ecn.channel9.msdn.com/o9/ch9/3672/563672/C9LecturesRalfLaemmelExpressionProblem_ch9.mp4
http://ecn.channel9.msdn.com/o9/ch9/7020/567020/C9LecturesRalfLaemmelAFPTypeClasses_ch9.mp4