I'm attempting to utilize Scala's infix notation for case classes with two argument constructors, much in the same way as List uses the cons :: operator. What I'm attempting to implement is more of a heterogeneous list, tho. My list contains objects parameterized with different types, thus a plain ol' list won't do.
Here are the relevant snippets:
sealed trait GetRequest[T]
protected case class SingleGetRequest[A <: Readable, T](val obj:DataObject[A, T]) extends GetRequest[T]
case class &[A <: Readable, T, U](val obj:DataObject[A, T], val next:GetRequest[U]) extends GetRequest[(T, U)]
I expect & to work as an infix operator to construct and pattern match. The latter works fine:
def unpack[U](req:GetRequest[U]) = {
req match {
case SingleGetRequest(obj) => ???
case obj & next => ???
}
}
However, I can't construct with infix notation:
[error] /home/jbarnes/code/snmp4s/core/src/test/scala/org/snmp4s/SnmpIntegrationSuite.scala:265: value & is not a member of org.snmp4s.DataObjectInst[org.snmp4s.ReadOnly,Int]
[error] val get3 = get(IfIndex(1) & SingleGetRequest(IfAlias(1)))
[error] ^
However, there are no problems constructing it the other way:
val get3 = get(&(IfIndex(1), SingleGetRequest(IfAlias(1))))
I have also tried making it a right-associative operator (&:) but no luck there either:
[error] /home/jbarnes/code/snmp4s/core/src/test/scala/org/snmp4s/SnmpIntegrationSuite.scala:265: value &: is not a member of org.snmp4s.SingleGetRequest[org.snmp4s.ReadWrite,String]
[error] val get3 = get(IfIndex(1) &: IfIndex(2) &: SingleGetRequest(IfAlias(1)))
[error] ^
I have played with making & a member function of GetRequest[T], but I don't see how to give it an unapply for the pattern matching.
Full source is available at
github. This code is in the snmp4s-core project.
Thanks,
Joe