8000 Make MVar constructors return Task by oleg-py · Pull Request #537 · monix/monix · GitHub
[go: up one dir, main page]

Skip to content

Make MVar constructors return Task - #537

Merged
alexandru merged 1 commit into
monix:masterfrom
oleg-py:mvar-referential-transparency
Jan 17, 2018
Merged

alexandru merged 1 commit into
monix:masterfrom
oleg-py:mvar-referential-transparency

Conversation

@oleg-py
@oleg-py oleg-py commented Jan 16, 2018
Copy link
Copy Markdown
Collaborator

Closes #535

@Avasil
Avasil commented Jan 17, 2018
Copy link
Copy Markdown
Collaborator

What are your thoughts on making it polymorphic in effect type like Iterant?

Instead of:

def apply[A](initial: A): Task[MVar[A]] =
  Task.eval(new AsyncMVarImpl[A](AsyncVar(initial)))

something like:

def apply[F[_], A](initial: A)(implicit F: Sync[F]): F[MVar[A]] =
  F.suspend(new AsyncMVarImpl[A](AsyncVar(initial)))

Then you could use it with other types if you don't want to use Task for some reason

@alexandru
Copy link
Copy Markdown
Member

@Avasil that's a cool idea, however the transition wouldn't be smooth.

E.g. one pretty cool thing about Task is that the Scheduler gets injected. Thus you can describe async actions that need an ExecutionContext without taking it explicitly from the current scope:

    def take: Task[A] =
      Task.unsafeCreate { (context, callback) =>
        implicit val s = context.scheduler
        // Execution could be synchronous (e.g. result is null or not)
        av.unsafeTake(callback) match {
          case null => () // do nothing
          case a => callback.asyncOnSuccess(a)
        }
      }

This, plus Task is cancellable, although I didn't bother to describe a cancel action for MVar, because well, I don't think it's that useful plus it would make the implementation less performant.
But we can do that too, without waiting on cats-effect, which is an ongoing and slower process.

Another reason is consistency. The monix-eval project is about Task and Coeval and so you have TaskSemaphore, TaskLocal and so on. You can't describe TaskLocal for cats-effect and you'll probably never be able to do it, since the implementation is very tied to Task's run-loop.
And this consistency is important, as people know that in monix-eval they've got abstractions optimized for Task.

That said I do want us to work on abstractions, like MVar, that can work on any F[_] implementing cats-effect stuff (n.b. for MVar you need Async and not Sync).

But those are going to be in a separate sub-project. And maybe we should make it non-monix (in org.typelevel). Maybe it can be a sub-project of cats-effect.

@alexandru
Copy link
Copy Markdown
Member

@oleg-py can't merge it because Travis-ci is currently having issues unrelated to your code - their queue is messed up.

@codecov
codecov Bot commented Jan 17, 2018
Copy link
Copy Markdown

Codecov Report

Merging #537 into master will decrease coverage by 6.17%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master     #537      +/-   ##
==========================================
- Coverage   90.37%   84.19%   -6.18%     
==========================================
  Files         364      364              
  Lines        9629    10357     +728     
  Branches     1797     1865      +68     
==========================================
+ Hits         8702     8720      +18     
- Misses        927     1637     +710

@alexandru
alexandru merged commit 69860a5 into monix:master Jan 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

0