- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Returning None from anonymous function
Sun, 2011-12-11, 22:21
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
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
Sun, 2011-12-11, 22:51
#2
Re: Returning None from anonymous function
On Mon, Dec 12, 2011 at 7:21 AM, Edmondo Porcu <edmondo.porcu@gmail.com> wrote:
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 } )
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 } )
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.