simulacrum icon indicating copy to clipboard operation
simulacrum copied to clipboard

Hygiene problems when ops-eligible methods refer to type members of type class

Open mpilquist opened this issue 10 years ago • 1 comments

Some examples:

// Fails to compile
@typeclass trait F[A] { self =>
  type B = A
  def f(a: A): Option[B]
}

// Fails to compile
@typeclass trait G[A] { self =>
  type B = A
  def f(a: A): Option[self.B]
}

// Compiles but doesn't generate an op for f
@typeclass trait H[A] { self =>
  type B = A
  def f(b: B): Option[A]
}

mpilquist avatar Sep 08 '15 22:09 mpilquist

I looked at this, and it's pretty well hopeless for the same reason as #55.

// Method should only be adapted if the first parameter in the first parameter list
// matches `tparamName`
val TargetTypeName = tparamName

It's not possible to generate correct code when correctness depends on knowing how names are bound but ad hoc string matching is being used to attempt to approximate that.

Your best bet is to severely restrict how people can write their type classes so the necessary information can be obtained before typechecking.

paulp avatar Mar 08 '17 23:03 paulp