Preconditions.checkArgument and Scala

1,221 views
Skip to first unread message

Blair Zajac

unread,
Sep 14, 2010, 12:16:10 AM9/14/10
to guava-...@googlegroups.com
With both Scala 2.7.7 and 2.8.0, the following fails to compile:


scala> import com.google.common.base.Preconditions.checkArgument
import com.google.common.base.Preconditions.checkArgument

scala> checkArgument(! Nil.isEmpty, "failure")
<console>:6: error: ambiguous reference to overloaded definition,
both method checkArgument in object Preconditions of type
(Boolean,java.lang.String,java.lang.Object*)Unit
and method checkArgument in object Preconditions of type (Boolean,Any)Unit
match argument types (Boolean,java.lang.String)
checkArgument(! Nil.isEmpty, "scdc")


It would be nice to have this cleanly work with Scala. Any suggestions besides
adding an empty array as a third argument?

BTW, I would put this up on StackOverflow, but this appears to have a change to
Guava to make this work, more than having it work in my code.

What about adding Preconditions.checkArgument() that takes a boolean and a
String, not an Object?

Blair

Gary Pampara

unread,
Sep 14, 2010, 1:22:04 AM9/14/10
to Blair Zajac, guava-...@googlegroups.com
Until this is sorted in some way (although the error the scala
compiler provides is completely valid, so I don't really know if it
would be worth the effort for Guava to change to support this),
perhaps you could consider using Scala's require() instead, which is a
language feature.

Have a look here:
http://daily-scala.blogspot.com/2010/03/assert-require-assume.html

> --
> guava-...@googlegroups.com.
> http://groups.google.com/group/guava-discuss?hl=en
> unsubscribe: guava-discus...@googlegroups.com
>
> This list is for discussion; for help, post to Stack Overflow instead:
> http://stackoverflow.com/questions/ask
> Use the tag "guava".
>

Kevin Bourrillion

unread,
Sep 14, 2010, 9:57:23 AM9/14/10
to Blair Zajac, guava-...@googlegroups.com
Shouldn't scala match Java's overload-resolution rules?


--
guava-...@googlegroups.com.
http://groups.google.com/group/guava-discuss?hl=en
unsubscribe: guava-discus...@googlegroups.com

This list is for discussion; for help, post to Stack Overflow instead:
http://stackoverflow.com/questions/ask
Use the tag "guava".



--
Kevin Bourrillion @ Google
http://guava-libraries.googlecode.com

Blair Zajac

unread,
Sep 14, 2010, 12:58:48 PM9/14/10
to Gary Pampara, guava-...@googlegroups.com
On 09/13/2010 10:22 PM, Gary Pampara wrote:
> Until this is sorted in some way (although the error the scala
> compiler provides is completely valid, so I don't really know if it
> would be worth the effort for Guava to change to support this),
> perhaps you could consider using Scala's require() instead, which is a
> language feature.

The reason that I don't use require is that it prepends the some text to
the exception message text that I don't want:

def require(requirement: Boolean) {
if (!requirement)
throw new IllegalArgumentException("requirement failed")
}

def require(requirement: Boolean, message: Any) {
if (!requirement)
throw new IllegalArgumentException("requirement failed: "+ message)
}

Kinda minor thing, but it bugs me if I want to customize an exception
message that is thrown to client code.

Blair

ray.j.greenwell

unread,
Sep 14, 2010, 9:12:38 PM9/14/10
to guava-discuss
On Sep 13, 9:16 pm, Blair Zajac <bl...@orcaware.com> wrote:

> It would be nice to have this cleanly work with Scala.  Any suggestions besides
> adding an empty array as a third argument?


checkArgument(! Nil.isEmpty, (Object) "scdc");
Reply all
Reply to author
Forward
0 new messages