Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce implicit convertion from Any to Coeval.Now #191

Closed
alexandru opened this issue Jun 29, 2016 · 0 comments

Comments

Projects
None yet
1 participant
@alexandru
Copy link
Member

commented Jun 29, 2016

To facilitate the usage of Coeval, we are introducing this implicit conversion to Coeval:

implicit def anyToCoevalNow[A](value: A): Coeval[A] =
    Coeval.now(value)

It's available in the implicit scope, so this would work:

val x: Coeval[Int] = 3

The reason is that this facilitates usage of Coeval for function parameters where the default are strict values. And strictness is the default in Monix. For example foldLeftL:

observable.foldLeftL(0)(_ + _)

foldLeft now wants a Coeval for its initial state. The above still works because of the implicit conversion. But now we can control the evaluation:

import monix.eval.Coeval
val initialBuffer = Coeval.evalAlways(mutable.BufferList.empty[Int])

observable.foldLeftL(initialBuffer)(_ += _)
  .map(_.toList)

The above example would not work without the given parameter being evaluated on every subscription. That's because that buffer is a stateful, mutable object, so its instance cannot be shared between subscribers.

@alexandru alexandru added this to the 2.0 milestone Jun 29, 2016

@alexandru alexandru self-assigned this Jun 29, 2016

@alexandru alexandru closed this in 6bec77f Jun 29, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.