I'm trying to implement some small quality-of-life improvements around an XML parser, basically trying to ape the way Circe looks like for Json parsing in Scala. The idea then would be to come up with a type class definition along these lines
type ParseResult[T] = ???
trait XMLCodec[T]:
extension(t: T)
def toXML: Elem
extension(xml: Elem)
def as[T]: ParseResult[T] // <- this fails to compile
Except that, as stated in the title, I get a Suspicious Shadowing by a Type Parameter
.
I could of course remove the [T]
from that line as in
trait XMLCodec[T]:
//...
extension(xml: Elem)
def asObject: ParseResult[T] // <- works fine
The above is fine, but I have concerns about name collision when more than one implementation of the same type class is present in any given context. Is there an easy way out of this that I'm missing?
CodePudding user response:
If as[T]
has its own type parameter independent of the XMLCodec[T]
type parameter then what is the reason to put the extension method as[T]
inside the type class XMLCodec[T]
?
Maybe you can have a type class
trait XMLCodec[T]:
extension (t: T)
def toXML: Elem
def toParseResult(xml: Elem): ParseResult[T]
and a separate extension method
extension (xml: Elem)
def as[T](using codec: XMLCodec[T]): ParseResult[T] = codec.toParseResult(xml)