This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Strange By-name parameter behavior

5 replies
Walter Chang
Joined: 2008-08-21,
User offline. Last seen 3 years 26 weeks ago.
Has anyone gotten this before?  I am using scala 2.8.1 and jre 1.6.0_22 on a mbp.
object ByName {
  implicit def toRunnable(body: => Any): Runnable = new Runnable { def run = body }
  val executor = Executors.newSingleThreadScheduledExecutor
  def main(args: Array[String]) {    executor.scheduleWithFixedDelay({       println("this prints just once " + System.currentTimeMillis)      println("this prints every time " + System.currentTimeMillis)     }, 0, 500, TimeUnit.MILLISECONDS)  } }
when run, the following gets printed:
this prints just once 1293437727073 this prints every time 1293437727084this prints every time 1293437727585 this prints every time 1293437728085this prints every time 1293437728585 this prints every time 1293437729086this prints every time 1293437729586 this prints every time 1293437730087this prints every time 1293437730587 ...
Walter Chang
--
.......__o
.......\<,
....( )/ ( )...
Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Strange By-name parameter behavior
scala> implicit def toRunnable[T](body: => T): Runnable = new Runnable { println("creating runnable"); def run = body }
toRunnable: [T](body: => T)java.lang.Runnable

scala> val r: Runnable = { println("foo"); println("bar") }
foo
creating runnable
r: java.lang.Runnable = $anon$1@3d689405

scala> r.run
bar

scala> r.run
bar

So, the reason is that the implicit conversion is applied to the result of the block.
Block != thunk in all cases.

On Mon, Dec 27, 2010 at 9:27 AM, Walter Chang <weihsiu [at] gmail [dot] com> wrote:
Has anyone gotten this before?  I am using scala 2.8.1 and jre 1.6.0_22 on a mbp.
object ByName {
  implicit def toRunnable(body: => Any): Runnable = new Runnable { def run = body }
  val executor = Executors.newSingleThreadScheduledExecutor
  def main(args: Array[String]) {    executor.scheduleWithFixedDelay({       println("this prints just once " + System.currentTimeMillis)      println("this prints every time " + System.currentTimeMillis)     }, 0, 500, TimeUnit.MILLISECONDS)  } }
when run, the following gets printed:
this prints just once 1293437727073 this prints every time 1293437727084this prints every time 1293437727585 this prints every time 1293437728085this prints every time 1293437728585 this prints every time 1293437729086this prints every time 1293437729586 this prints every time 1293437730087this prints every time 1293437730587 ...
Walter Chang
--
.......__o
.......\<,
....( )/ ( )...



--
Viktor Klang,
Code Connoisseur
Work:   Scalable Solutions
Code:   github.com/viktorklang
Follow: twitter.com/viktorklang
Read:   klangism.tumblr.com

Ittay Dror 2
Joined: 2010-05-05,
User offline. Last seen 42 years 45 weeks ago.
Re: Strange By-name parameter behavior


Walter Chang wrote:
70UD [at] mail [dot] gmail [dot] com" type="cite">Has anyone gotten this before?  I am using scala 2.8.1 and jre 1.6.0_22 on a mbp.
object ByName {
  implicit def toRunnable(body: => Any): Runnable = new Runnable { def run = body }
  val executor = Executors.newSingleThreadScheduledExecutor
  def main(args: Array[String]) {     executor.scheduleWithFixedDelay({       println("this prints just once " + System.currentTimeMillis)       println("this prints every time " + System.currentTimeMillis)     }, 0, 500, TimeUnit.MILLISECONDS)   } }

scala> import ByName._
import ByName._

scala> val r: Runnable = {
     |       println("this prints just once " + System.currentTimeMillis)
     |       println("this prints every time " + System.currentTimeMillis)
     |     }
this prints just once 1293440090079
r: java.lang.Runnable = ByName$$anon$1@2d6b76

scala> r.run
this prints every time 1293440136883

scala> toRunnable{
     |       println("this prints just once " + System.currentTimeMillis)
     |       println("this prints every time " + System.currentTimeMillis)
     |     }
res1: java.lang.Runnable = ByName$$anon$1@da5686

scala> res1.run
this prints just once 1293440251666
this prints every time 1293440251667

I guess the reason is the compiler first builds the ast and then sees the result should be a runnable, so it takes the last expression and applies the implicit conversion on it.

Ittay


70UD [at] mail [dot] gmail [dot] com" type="cite">
when run, the following gets printed:
this prints just once 1293437727073 this prints every time 1293437727084 this prints every time 1293437727585 this prints every time 1293437728085 this prints every time 1293437728585 this prints every time 1293437729086 this prints every time 1293437729586 this prints every time 1293437730087 this prints every time 1293437730587 ...
Walter Chang
--
.......__o
.......\<,
....( )/ ( )...
Walter Chang
Joined: 2008-08-21,
User offline. Last seen 3 years 26 weeks ago.
Re: Strange By-name parameter behavior
Is this a bug?  I am getting around this by using no-arg function instead of by-name block as function argument.  What is the rule of thumb when using by-name parameters?  Should compiler behave correctly for case like this?

On Mon, Dec 27, 2010 at 4:58 PM, Ittay Dror <ittay [dot] dror [at] gmail [dot] com> wrote:


Walter Chang wrote:
Has anyone gotten this before?  I am using scala 2.8.1 and jre 1.6.0_22 on a mbp.
object ByName {
  implicit def toRunnable(body: => Any): Runnable = new Runnable { def run = body }
  val executor = Executors.newSingleThreadScheduledExecutor
  def main(args: Array[String]) {     executor.scheduleWithFixedDelay({       println("this prints just once " + System.currentTimeMillis)       println("this prints every time " + System.currentTimeMillis)     }, 0, 500, TimeUnit.MILLISECONDS)   } }

scala> import ByName._
import ByName._

scala> val r: Runnable = {
     |       println("this prints just once " + System.currentTimeMillis)
     |       println("this prints every time " + System.currentTimeMillis)
     |     }
this prints just once 1293440090079
r: java.lang.Runnable = ByName$$anon$1@2d6b76

scala> r.run
this prints every time 1293440136883

scala> toRunnable{
     |       println("this prints just once " + System.currentTimeMillis)
     |       println("this prints every time " + System.currentTimeMillis)
     |     }
res1: java.lang.Runnable = ByName$$anon$1@da5686

scala> res1.run
this prints just once 1293440251666
this prints every time 1293440251667

I guess the reason is the compiler first builds the ast and then sees the result should be a runnable, so it takes the last expression and applies the implicit conversion on it.

Ittay



when run, the following gets printed:
this prints just once 1293437727073 this prints every time 1293437727084 this prints every time 1293437727585 this prints every time 1293437728085 this prints every time 1293437728585 this prints every time 1293437729086 this prints every time 1293437729586 this prints every time 1293437730087 this prints every time 1293437730587 ...
Walter Chang
--
.......__o
.......\<,
....( )/ ( )...



--
.......__o
.......\<,
....( )/ ( )...
Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: Strange By-name parameter behavior

It's not a bug. There was a proposal to add a warning, but this was
not accepted as it is tricky to avoid false positives.

http://lampsvn.epfl.ch/trac/scala/ticket/3237

On Mon, Dec 27, 2010 at 10:18 AM, Walter Chang wrote:
> Is this a bug?  I am getting around this by using no-arg function instead of
> by-name block as function argument.  What is the rule of thumb when using
> by-name parameters?  Should compiler behave correctly for case like this?
>
> On Mon, Dec 27, 2010 at 4:58 PM, Ittay Dror wrote:
>>
>>
>> Walter Chang wrote:
>>
>> Has anyone gotten this before?  I am using scala 2.8.1 and jre 1.6.0_22 on
>> a mbp.
>> object ByName {
>>   implicit def toRunnable(body: => Any): Runnable = new Runnable { def run
>> = body }
>>   val executor = Executors.newSingleThreadScheduledExecutor
>>   def main(args: Array[String]) {
>>     executor.scheduleWithFixedDelay({
>>       println("this prints just once " + System.currentTimeMillis)
>>       println("this prints every time " + System.currentTimeMillis)
>>     }, 0, 500, TimeUnit.MILLISECONDS)
>>   }
>> }
>>
>> scala> import ByName._
>> import ByName._
>>
>> scala> val r: Runnable = {
>>      |       println("this prints just once " + System.currentTimeMillis)
>>      |       println("this prints every time " + System.currentTimeMillis)
>>      |     }
>> this prints just once 1293440090079
>> r: java.lang.Runnable = ByName$$anon$1@2d6b76
>>
>> scala> r.run
>> this prints every time 1293440136883
>>
>> scala> toRunnable{
>>      |       println("this prints just once " + System.currentTimeMillis)
>>      |       println("this prints every time " + System.currentTimeMillis)
>>      |     }
>> res1: java.lang.Runnable = ByName$$anon$1@da5686
>>
>> scala> res1.run
>> this prints just once 1293440251666
>> this prints every time 1293440251667
>>
>> I guess the reason is the compiler first builds the ast and then sees the
>> result should be a runnable, so it takes the last expression and applies the
>> implicit conversion on it.
>>
>> Ittay
>>
>>
>>
>> when run, the following gets printed:
>> this prints just once 1293437727073
>> this prints every time 1293437727084
>> this prints every time 1293437727585
>> this prints every time 1293437728085
>> this prints every time 1293437728585
>> this prints every time 1293437729086
>> this prints every time 1293437729586
>> this prints every time 1293437730087
>> this prints every time 1293437730587
>> ...
>> Walter Chang
>> --
>> .......__o
>> .......\<,
>> ....( )/ ( )...
>
>
>
> --
> .......__o
> .......\<,
> ....( )/ ( )...
>

Walter Chang
Joined: 2008-08-21,
User offline. Last seen 3 years 26 weeks ago.
Re: Strange By-name parameter behavior
thanks, that clears up my confusions.

On Mon, Dec 27, 2010 at 5:33 PM, Jason Zaugg <jzaugg [at] gmail [dot] com> wrote:
It's not a bug. There was a proposal to add a warning, but this was
not accepted as it is tricky to avoid false positives.

http://lampsvn.epfl.ch/trac/scala/ticket/3237

On Mon, Dec 27, 2010 at 10:18 AM, Walter Chang <weihsiu [at] gmail [dot] com> wrote:
> Is this a bug?  I am getting around this by using no-arg function instead of
> by-name block as function argument.  What is the rule of thumb when using
> by-name parameters?  Should compiler behave correctly for case like this?
>
> On Mon, Dec 27, 2010 at 4:58 PM, Ittay Dror <ittay [dot] dror [at] gmail [dot] com> wrote:
>>
>>
>> Walter Chang wrote:
>>
>> Has anyone gotten this before?  I am using scala 2.8.1 and jre 1.6.0_22 on
>> a mbp.
>> object ByName {
>>   implicit def toRunnable(body: => Any): Runnable = new Runnable { def run
>> = body }
>>   val executor = Executors.newSingleThreadScheduledExecutor
>>   def main(args: Array[String]) {
>>     executor.scheduleWithFixedDelay({
>>       println("this prints just once " + System.currentTimeMillis)
>>       println("this prints every time " + System.currentTimeMillis)
>>     }, 0, 500, TimeUnit.MILLISECONDS)
>>   }
>> }
>>
>> scala> import ByName._
>> import ByName._
>>
>> scala> val r: Runnable = {
>>      |       println("this prints just once " + System.currentTimeMillis)
>>      |       println("this prints every time " + System.currentTimeMillis)
>>      |     }
>> this prints just once 1293440090079
>> r: java.lang.Runnable = ByName$$anon$1@2d6b76
>>
>> scala> r.run
>> this prints every time 1293440136883
>>
>> scala> toRunnable{
>>      |       println("this prints just once " + System.currentTimeMillis)
>>      |       println("this prints every time " + System.currentTimeMillis)
>>      |     }
>> res1: java.lang.Runnable = ByName$$anon$1@da5686
>>
>> scala> res1.run
>> this prints just once 1293440251666
>> this prints every time 1293440251667
>>
>> I guess the reason is the compiler first builds the ast and then sees the
>> result should be a runnable, so it takes the last expression and applies the
>> implicit conversion on it.
>>
>> Ittay
>>
>>
>>
>> when run, the following gets printed:
>> this prints just once 1293437727073
>> this prints every time 1293437727084
>> this prints every time 1293437727585
>> this prints every time 1293437728085
>> this prints every time 1293437728585
>> this prints every time 1293437729086
>> this prints every time 1293437729586
>> this prints every time 1293437730087
>> this prints every time 1293437730587
>> ...
>> Walter Chang
>> --
>> .......__o
>> .......\<,
>> ....( )/ ( )...
>
>
>
> --
> .......__o
> .......\<,
> ....( )/ ( )...
>



--
.......__o
.......\<,
....( )/ ( )...

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland