Using type declarations in a for comprehension

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Using type declarations in a for comprehension

Udas
Even though Scala's type inference is great, sometimes it is useful to add explicit
types for the variables. This works fine, but recently I came across one case where it
I can't make it work, can someone point out what I am doing wrong?


... 
...
val i = for {
  (i, s): Tuple2[Int, String] <- frot(1, "One")
} yield(i)

<console>:2: error: '<-' expected but ':' found.
         (i, s): Tuple2[Int, String] <- frot(1, "One")

Thanks
Udas


-- Copy paste this into scala console to reproduce --

// Function returning option of tuple
def frot(x: Int, y: String): Option[Tuple2[Int, String]] = Some((x, y))

// Function returning option of string
def fros(y: String): Option[String] = Some(y)

// This works fine.
val ix = for {
  s: String <- fros("Two")
  (i, s) <- frot(1, "One")
} yield(i)

// This does not compile
val iy= for {
  (i, s): Tuple2[Int, String] <- frot(1, "One")
} yield(i)

scala> :load fortuple.scala
Loading /fortuple.scala...
frot: (x: Int, y: String)Option[(Int, String)]
fros: (y: String)Option[String]
i: Option[Int] = Some(1)
<console>:2: error: '<-' expected but ':' found.
         (i, s): Tuple2[Int, String] <- frot(1, "One")
               ^
<console>:1: error: eof expected but '}' found.
       } yield(i)
       ^

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Using type declarations in a for comprehension

Lutz Huehnken

I think the error you see is rooted in the way the parser de-sugars the destructuring assignment in the for expression, it seems it just doesn't expect a type annotation in this position. How about going with

val iy= for {
  (i: Int, s: String) <- frot(1, "One")
} yield(i)


Wouldn't that be explicit enough?



On Monday, 23 January 2017 04:06:10 UTC+1, Udas wrote:
Even though Scala's type inference is great, sometimes it is useful to add explicit
types for the variables. This works fine, but recently I came across one case where it
I can't make it work, can someone point out what I am doing wrong?


... 
...
val i = for {
  (i, s): Tuple2[Int, String] <- frot(1, "One")
} yield(i)

<console>:2: error: '<-' expected but ':' found.
         (i, s): Tuple2[Int, String] <- frot(1, "One")

Thanks
Udas


-- Copy paste this into scala console to reproduce --

// Function returning option of tuple
def frot(x: Int, y: String): Option[Tuple2[Int, String]] = Some((x, y))

// Function returning option of string
def fros(y: String): Option[String] = Some(y)

// This works fine.
val ix = for {
  s: String <- fros("Two")
  (i, s) <- frot(1, "One")
} yield(i)

// This does not compile
val iy= for {
  (i, s): Tuple2[Int, String] <- frot(1, "One")
} yield(i)

scala> :load fortuple.scala
Loading /fortuple.scala...
frot: (x: Int, y: String)Option[(Int, String)]
fros: (y: String)Option[String]
i: Option[Int] = Some(1)
<console>:2: error: '<-' expected but ':' found.
         (i, s): Tuple2[Int, String] <- frot(1, "One")
               ^
<console>:1: error: eof expected but '}' found.
       } yield(i)
       ^

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Using type declarations in a for comprehension

Jasper-M
Be careful though. The thing before the `<-` is a pattern, not a value definition. Which means it does not behave the same way:

scala> class Foo
defined class Foo


scala
> val (i, s): (Int,Foo) = (1,"One": Any)
<console>:14: error: type mismatch;
 found  
: Any
 required
: Foo
       val
(i, s): (Int,Foo) = (1,"One": Any)
                                       
^


scala
> val iy= for {
     
|   (i: Int, s: Foo) <- Some(1, "One": Any)
     
| } yield(i)
iy
: Option[Int] = None




Op maandag 23 januari 2017 11:10:52 UTC+1 schreef Lutz Huehnken:

I think the error you see is rooted in the way the parser de-sugars the destructuring assignment in the for expression, it seems it just doesn't expect a type annotation in this position. How about going with

val iy= for {
  (i: Int, s: String) <- frot(1, "One")
} yield(i)


Wouldn't that be explicit enough?



On Monday, 23 January 2017 04:06:10 UTC+1, Udas wrote:
Even though Scala's type inference is great, sometimes it is useful to add explicit
types for the variables. This works fine, but recently I came across one case where it
I can't make it work, can someone point out what I am doing wrong?


... 
...
val i = for {
  (i, s): Tuple2[Int, String] <- frot(1, "One")
} yield(i)

<console>:2: error: '<-' expected but ':' found.
         (i, s): Tuple2[Int, String] <- frot(1, "One")

Thanks
Udas


-- Copy paste this into scala console to reproduce --

// Function returning option of tuple
def frot(x: Int, y: String): Option[Tuple2[Int, String]] = Some((x, y))

// Function returning option of string
def fros(y: String): Option[String] = Some(y)

// This works fine.
val ix = for {
  s: String <- fros("Two")
  (i, s) <- frot(1, "One")
} yield(i)

// This does not compile
val iy= for {
  (i, s): Tuple2[Int, String] <- frot(1, "One")
} yield(i)

scala> :load fortuple.scala
Loading /fortuple.scala...
frot: (x: Int, y: String)Option[(Int, String)]
fros: (y: String)Option[String]
i: Option[Int] = Some(1)
<console>:2: error: '<-' expected but ':' found.
         (i, s): Tuple2[Int, String] <- frot(1, "One")
               ^
<console>:1: error: eof expected but '}' found.
       } yield(i)
       ^

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Using type declarations in a for comprehension

Roland Kuhn
Ah, finally the restriction makes sense: allowing type ascription syntax in this position would be ambiguous, type ascription and type-based pattern match are not at all the same things.

In any case, it usually is best to use a type ascription (or explicit type arguments) on the right-hand side of the arrow within a for-comprehension.

Regards,

Roland

23 jan. 2017 kl. 13:34 skrev Jasper-M <[hidden email]>:

Be careful though. The thing before the `<-` is a pattern, not a value definition. Which means it does not behave the same way:

scala> class Foo
defined class Foo


scala
> val (i, s): (Int,Foo) = (1,"One": Any)
<console>:14: error: type mismatch;
 found  
: Any
 required
: Foo
       val
(i, s): (Int,Foo) = (1,"One": Any)
                                       
^


scala
> val iy= for {
     
|   (i: Int, s: Foo) <- Some(1, "One": Any)
     
| } yield(i)
iy
: Option[Int] = None




Op maandag 23 januari 2017 11:10:52 UTC+1 schreef Lutz Huehnken:

I think the error you see is rooted in the way the parser de-sugars the destructuring assignment in the for expression, it seems it just doesn't expect a type annotation in this position. How about going with

val iy= for {
  (i: Int, s: String) <- frot(1, "One")
} yield(i)


Wouldn't that be explicit enough?



On Monday, 23 January 2017 04:06:10 UTC+1, Udas wrote:
Even though Scala's type inference is great, sometimes it is useful to add explicit
types for the variables. This works fine, but recently I came across one case where it
I can't make it work, can someone point out what I am doing wrong?


... 
...
val i = for {
  (i, s): Tuple2[Int, String] <- frot(1, "One")
} yield(i)

<console>:2: error: '<-' expected but ':' found.
         (i, s): Tuple2[Int, String] <- frot(1, "One")

Thanks
Udas


-- Copy paste this into scala console to reproduce --

// Function returning option of tuple
def frot(x: Int, y: String): Option[Tuple2[Int, String]] = Some((x, y))

// Function returning option of string
def fros(y: String): Option[String] = Some(y)

// This works fine.
val ix = for {
  s: String <- fros("Two")
  (i, s) <- frot(1, "One")
} yield(i)

// This does not compile
val iy= for {
  (i, s): Tuple2[Int, String] <- frot(1, "One")
} yield(i)

scala> :load fortuple.scala
Loading /fortuple.scala...
frot: (x: Int, y: String)Option[(Int, String)]
fros: (y: String)Option[String]
i: Option[Int] = Some(1)
<console>:2: error: '<-' expected but ':' found.
         (i, s): Tuple2[Int, String] <- frot(1, "One")
               ^
<console>:1: error: eof expected but '}' found.
       } yield(i)
       ^


--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Using type declarations in a for comprehension

Udas
Hi Roland,

So:

val iy = for {
  (i, s) <- frot(1, "One"): Option[Tuple2[Int, String]]
} yield(i)

is what you are suggesting, right (sorry, new to Scala) ?


On Monday, January 23, 2017 at 4:46:01 AM UTC-8, rkuhn wrote:
Ah, finally the restriction makes sense: allowing type ascription syntax in this position would be ambiguous, type ascription and type-based pattern match are not at all the same things.

In any case, it usually is best to use a type ascription (or explicit type arguments) on the right-hand side of the arrow within a for-comprehension.

Regards,

Roland

23 jan. 2017 kl. 13:34 skrev Jasper-M <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="JaE4VNtPAwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">jaspe...@...>:

Be careful though. The thing before the `<-` is a pattern, not a value definition. Which means it does not behave the same way:

scala> class Foo
defined class Foo


scala
> val (i, s): (Int,Foo) = (1,"One": Any)
<console>:14: error: type mismatch;
 found  
: Any
 required
: Foo
       val
(i, s): (Int,Foo) = (1,"One": Any)
                                       
^


scala
> val iy= for {
     
|   (i: Int, s: Foo) <- Some(1, "One": Any)
     
| } yield(i)
iy
: Option[Int] = None




Op maandag 23 januari 2017 11:10:52 UTC+1 schreef Lutz Huehnken:

I think the error you see is rooted in the way the parser de-sugars the destructuring assignment in the for expression, it seems it just doesn't expect a type annotation in this position. How about going with

val iy= for {
  (i: Int, s: String) <- frot(1, "One")
} yield(i)


Wouldn't that be explicit enough?



On Monday, 23 January 2017 04:06:10 UTC+1, Udas wrote:
Even though Scala's type inference is great, sometimes it is useful to add explicit
types for the variables. This works fine, but recently I came across one case where it
I can't make it work, can someone point out what I am doing wrong?


... 
...
val i = for {
  (i, s): Tuple2[Int, String] <- frot(1, "One")
} yield(i)

<console>:2: error: '<-' expected but ':' found.
         (i, s): Tuple2[Int, String] <- frot(1, "One")

Thanks
Udas


-- Copy paste this into scala console to reproduce --

// Function returning option of tuple
def frot(x: Int, y: String): Option[Tuple2[Int, String]] = Some((x, y))

// Function returning option of string
def fros(y: String): Option[String] = Some(y)

// This works fine.
val ix = for {
  s: String <- fros("Two")
  (i, s) <- frot(1, "One")
} yield(i)

// This does not compile
val iy= for {
  (i, s): Tuple2[Int, String] <- frot(1, "One")
} yield(i)

scala> :load fortuple.scala
Loading /fortuple.scala...
frot: (x: Int, y: String)Option[(Int, String)]
fros: (y: String)Option[String]
i: Option[Int] = Some(1)
<console>:2: error: '<-' expected but ':' found.
         (i, s): Tuple2[Int, String] <- frot(1, "One")
               ^
<console>:1: error: eof expected but '}' found.
       } yield(i)
       ^


--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="JaE4VNtPAwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">scala-user+...@googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Using type declarations in a for comprehension

Roland Kuhn

24 jan. 2017 kl. 08:45 skrev Udas <[hidden email]>:

Hi Roland,

So:

val iy = for {
  (i, s) <- frot(1, "One"): Option[Tuple2[Int, String]]
} yield(i)

is what you are suggesting, right (sorry, new to Scala) ?

In this contrived example, yes (I’m qualifying this statement since in the example this ascription does not change anything, all types are the same without it).

Consider def unit[A](value: A): Option[A] then the other way I sketched would be

for {
  x <- unit[MyType]( /* some expression */ )
} yield x.selectSomething

Sometimes it will be necessary to explicitly mention the MyType argument in order to make the elided expression do the right thing (i.e. give the compiler some missing piece of information).

Regards,

Roland



On Monday, January 23, 2017 at 4:46:01 AM UTC-8, rkuhn wrote:
Ah, finally the restriction makes sense: allowing type ascription syntax in this position would be ambiguous, type ascription and type-based pattern match are not at all the same things.

In any case, it usually is best to use a type ascription (or explicit type arguments) on the right-hand side of the arrow within a for-comprehension.

Regards,

Roland

23 jan. 2017 kl. 13:34 skrev Jasper-M <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="JaE4VNtPAwAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;" class="">jaspe...@...>:

Be careful though. The thing before the `<-` is a pattern, not a value definition. Which means it does not behave the same way:

scala> class Foo
defined class Foo


scala
> val (i, s): (Int,Foo) = (1,"One": Any)
<console>:14: error: type mismatch;
 found  
: Any
 required
: Foo
       val
(i, s): (Int,Foo) = (1,"One": Any)
                                       
^


scala
> val iy= for {
     
|   (i: Int, s: Foo) <- Some(1, "One": Any)
     
| } yield(i)
iy
: Option[Int] = None




Op maandag 23 januari 2017 11:10:52 UTC+1 schreef Lutz Huehnken:

I think the error you see is rooted in the way the parser de-sugars the destructuring assignment in the for expression, it seems it just doesn't expect a type annotation in this position. How about going with

val iy= for {
  (i: Int, s: String) <- frot(1, "One")
} yield(i)


Wouldn't that be explicit enough?



On Monday, 23 January 2017 04:06:10 UTC+1, Udas wrote:
Even though Scala's type inference is great, sometimes it is useful to add explicit
types for the variables. This works fine, but recently I came across one case where it
I can't make it work, can someone point out what I am doing wrong?


... 
...
val i = for {
  (i, s): Tuple2[Int, String] <- frot(1, "One")
} yield(i)

<console>:2: error: '<-' expected but ':' found.
         (i, s): Tuple2[Int, String] <- frot(1, "One")

Thanks
Udas


-- Copy paste this into scala console to reproduce --

// Function returning option of tuple
def frot(x: Int, y: String): Option[Tuple2[Int, String]] = Some((x, y))

// Function returning option of string
def fros(y: String): Option[String] = Some(y)

// This works fine.
val ix = for {
  s: String <- fros("Two")
  (i, s) <- frot(1, "One")
} yield(i)

// This does not compile
val iy= for {
  (i, s): Tuple2[Int, String] <- frot(1, "One")
} yield(i)

scala> :load fortuple.scala
Loading /fortuple.scala...
frot: (x: Int, y: String)Option[(Int, String)]
fros: (y: String)Option[String]
i: Option[Int] = Some(1)
<console>:2: error: '<-' expected but ':' found.
         (i, s): Tuple2[Int, String] <- frot(1, "One")
               ^
<console>:1: error: eof expected but '}' found.
       } yield(i)
       ^


--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="JaE4VNtPAwAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;" class="">scala-user+...@googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href='https://groups.google.com/d/optout';return true;" onclick="this.href='https://groups.google.com/d/optout';return true;" class="">https://groups.google.com/d/optout.


--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.