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

Returning None from anonymous function

2 replies
edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 4 days ago.
Dear all,
I have a problem with None that I can't understand:
error: type mismatch;found   : None.type (with underlying type object None)required: () => Option[Unit] None;

  eventGenerator = RandomDistributedEvent.fixedInterval[Unit](200,{      val selectedSecurityIndex = randomGenerator.nextInt(subscribedSecurities.size());       val selectedSecurity = subscribedSecurities.values.toIndexedSeq(selectedSecurityIndex);      updateSecurity(selectedSecurity);      gigaSpace.write(selectedSecurity,Lease.FOREVER,2000,UpdateModifiers.UPDATE_OR_WRITE|UpdateModifiers.NO_RETURN_VALUE);       None    })


Should I cast None to something else?
Like None.asInstanceOf[Option[Unit]] ? 
Best RegardsEdmondo
Lars Hupel
Joined: 2010-06-23,
User offline. Last seen 44 weeks 3 days ago.
Re: Returning None from anonymous function

Apparently the `fixedInterval` method needs a `() => Option[Unit]` as a
second parameter. Try writing the following:

eventGenerator = RandomDistributedEvent.fixedInterval[Unit](200,{ () =>
// ...

None
}

> Like None.asInstanceOf[Option[Unit]] ?

`None` is already an instance of `Option[Unit]`. And no, casting is
*not* the way to go for type mismatch errors.

Two further remarks: Drop the `;` in your code. Also, when posting on a
mailing list, please give a minimal example which contains the same
error as in your real code. This helps narrowing the problem.

Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: Returning None from anonymous function
On Mon, Dec 12, 2011 at 7:21 AM, Edmondo Porcu <edmondo.porcu@gmail.com> wrote:
Dear all,
I have a problem with None that I can't understand:
error: type mismatch;found   : None.type (with underlying type object None)required: () => Option[Unit] None;

  eventGenerator = RandomDistributedEvent.fixedInterval[Unit](200,{      val selectedSecurityIndex = randomGenerator.nextInt(subscribedSecurities.size());       val selectedSecurity = subscribedSecurities.values.toIndexedSeq(selectedSecurityIndex);      updateSecurity(selectedSecurity);      gigaSpace.write(selectedSecurity,Lease.FOREVER,2000,UpdateModifiers.UPDATE_OR_WRITE|UpdateModifiers.NO_RETURN_VALUE);       None    })


Should I cast None to something else?
Like None.asInstanceOf[Option[Unit]] ? 

The method `fixedInterval` (presumably, you didn't provide it's type signature), calls for a `Function0[Option[Unit]]`; you are providing `None` which is a subtype of `Option[Unit]`, but not of `Function0`

Use the syntax: `() => expr` to construct a `Function0`.

scala> foo( { 1; None } )
<console>:9: error: type mismatch;
 found   : None.type (with underlying type object None)
 required: () => Option[Int]
              foo( { 1; None } )
                        ^

scala> foo( { () => 1; None } )

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