Need help using a case class constructor with infix notation

498 views
Skip to first unread message

Joe Barnes

unread,
Jun 8, 2013, 10:42:33 PM6/8/13
to scala...@googlegroups.com
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

Som Snytt

unread,
Jun 8, 2013, 11:21:59 PM6/8/13
to Joe Barnes, scala-user
AFAIK, infix notation means there is an operator i.e. method.

For $colon$colon, there is a method for making and a case class for unmaking:



--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Som Snytt

unread,
Jun 8, 2013, 11:51:27 PM6/8/13
to Joe Barnes, scala-user
BTW, and sorry for the spam and AIA if I'm missing something,


> case classes with two argument constructors

on the pattern side you're ok with either ctor pattern or extractor pattern.  Maybe from now on, I'll use xtor for extractor.

apm@mara:~$ scala
Welcome to Scala version 2.10.1 (OpenJDK 64-Bit Server VM, Java 1.7.0_21).
Type in expressions to have them evaluated.
Type :help for more information.

scala> case class Foo(i: Int, j: Int, k: Int)
defined class Foo

scala> Foo(1,2,3)
res0: Foo = Foo(1,2,3)

scala> res0 match { case 1 Foo (2,3) => true }
res1: Boolean = true

scala> object Bar {
     | def unapply(f: Foo): Option[(Int,Int,Int)] = Some(f.i,f.j,f.k)
     | }
defined module Bar

scala> res0 match { case 1 Bar (2,3) => true }
res2: Boolean = true



Joe Barnes

unread,
Jun 9, 2013, 8:01:09 AM6/9/13
to scala...@googlegroups.com
Ah, you're right. Thanks for the links to the List code. That's exactly what I was attempting. I'll try it out when I get back to my laptop.

FYI, this is the blog that I misunderstood (but its clear now): http://blog.lunatech.com/2011/11/25/scala-list-extractor-demystified

Thanks!
Joe

Reply all
Reply to author
Forward
0 new messages