[scala] Parenthesis inference

View: New views
4 Messages — Rating Filter:   Alert me  

[scala] Parenthesis inference

by Florian Hars-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I am not exactly sure that this conforms to the principle of least surprise:

scala> Some(1,2) match { case Some(1,2) => true case _ => false }
<console>:5: error: wrong number of arguments for <none>: ((Int, Int))Some[(Int, Int)]
       Some(1,2) match { case Some(1,2) => true case _ => false }
                              ^

- Florian.

Re: [scala] Parenthesis inference

by Anders Bach Nielsen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Florian Hars wrote:
> I am not exactly sure that this conforms to the principle of least surprise:
>
> scala> Some(1,2) match { case Some(1,2) => true case _ => false }
> <console>:5: error: wrong number of arguments for <none>: ((Int, Int))Some[(Int, Int)]
>        Some(1,2) match { case Some(1,2) => true case _ => false }
>                               ^

the correct version of this should be

scala> Some((1,2)) match { case Some((1,2)) => true case _ => false }
res0: Boolean = true

I presume you want (1,2) to be a tuple and with the Some(1,2) is not a
tuple (except if there is an implicit conversion?).

/Anders

--
Anders Bach Nielsen            |   http://www.daimi.au.dk/~abachn/
University of Aarhus           |   andersbach.nielsen@...
-
  Time to be aggressive. Go after a tattooed Virgo.

Re: [scala] Parenthesis inference

by Paul Phillips-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Sep 17, 2008 at 07:23:23PM +0200, Anders Bach Nielsen wrote:
> I presume you want (1,2) to be a tuple and with the Some(1,2) is not a
> tuple (except if there is an implicit conversion?).

But it is a tuple.  Some only takes one argument, and all that argument
can be is the tuple (1,2).

scala> Some((1,2)) == Some(1,2)
res3: Boolean = true

This is generally true:

scala> def id(x: AnyRef) = x.getClass.getName
id: (AnyRef)java.lang.String

scala> id ("x")
res12: java.lang.String = java.lang.String

scala> id ("x", "y")
res13: java.lang.String = scala.Tuple2

Notice also this doesn't work:

scala> id (("x"))  
res14: java.lang.String = java.lang.String

scala> id (Tuple1("x"))
res15: java.lang.String = scala.Tuple1

The spec says you can use a trailing comma to denote a 1-tuple, but the
interpreter says it's deprecated, plus it doesn't work:

scala> id (("x",))
<console>:1: warning: Trailing commas have been deprecated
res2: java.lang.String = java.lang.String

I think there are quite a few corner cases around parentheses inference
and it shouldn't be seen as anything but a convenience.

--
Paul Phillips      | Giving every man a vote has no more made men wise
Analgesic          | and free than Christianity has made them good.
Empiricist         |     -- H. L. Mencken
up hill, pi pals!  |----------* http://www.improving.org/paulp/ *----------

Re: [scala] Parenthesis inference

by Alex Boisvert-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Sep 17, 2008 at 9:22 AM, Florian Hars <hars@...> wrote:
I am not exactly sure that this conforms to the principle of least surprise:

scala> Some(1,2) match { case Some(1,2) => true case _ => false }
<console>:5: error: wrong number of arguments for <none>: ((Int, Int))Some[(Int, Int)]
      Some(1,2) match { case Some(1,2) => true case _ => false }

Agreed but it makes sense once you understand the design tradeoffs to keep the pattern matching syntax simple and powerful.

A language with no surprises is a language with no good surprises :)

alex