Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Sep 05 2019 14:43
    @typelevel-bot banned @jdegoes
  • Jan 31 2019 21:17
    codecov-io commented #484
  • Jan 31 2019 21:08
    scala-steward opened #484
  • Jan 31 2019 18:19
    andywhite37 commented #189
  • Jan 31 2019 02:41
    kamilongus starred typelevel/cats-effect
  • Jan 30 2019 00:01
    codecov-io commented #483
  • Jan 29 2019 23:51
    deniszjukow opened #483
  • Jan 29 2019 23:37
  • Jan 29 2019 23:22
  • Jan 29 2019 20:26
    Rui-L starred typelevel/cats-effect
  • Jan 29 2019 18:01
    jdegoes commented #480
  • Jan 29 2019 17:04
    thomaav starred typelevel/cats-effect
  • Jan 28 2019 17:43
    asachdeva starred typelevel/cats-effect
  • Jan 28 2019 07:12
    alexandru commented #480
  • Jan 28 2019 05:45
    codecov-io commented #482
  • Jan 28 2019 05:35
    daron666 opened #482
  • Jan 27 2019 13:56
    codecov-io commented #481
  • Jan 27 2019 13:46
    lrodero opened #481
  • Jan 27 2019 05:47
    codecov-io commented #460
  • Jan 27 2019 05:37
    codecov-io commented #460
Oleg Pyzhcov
@oleg-py
it's because there's some odd shitfuckery in CE3 IOApp
I think if you set sbt to fork in run it'll work
Gabriel Volpe
@gvolpe:matrix.org
[m]
let me try that, Scastie does not like it either
makes sense in the browser, though
Oleg Pyzhcov
@oleg-py
I think it's because unsafeRunSync forces IORuntime and then running it as main tries to force it too.
Gabriel Volpe
@gvolpe:matrix.org
[m]
[info] running (fork) examples.state.LeakyState
[error] Exception in thread "main" java.lang.IllegalArgumentException: requirement failed
[error]     at scala.Predef$.require(Predef.scala:325)
[error]     at cats.effect.unsafe.IORuntimeCompanionPlatform.installGlobal(IORuntimeCompanionPlatform.scala:67)
[error]     at cats.effect.IOApp.main(IOApp.scala:58)
[error]     at cats.effect.IOApp.main$(IOApp.scala:35)
[error]     at examples.state.LeakyState$.main(LeakyState.scala:9)
[error]     at examples.state.LeakyState.main(LeakyState.scala)
[error] Nonzero exit code returned from runner: 1
[error] (Compile / run) Nonzero exit code returned from runner: 1
Set Compile / run / fork := true in my build.sbt
Oleg Pyzhcov
@oleg-py
actually if you can submit an issue for CE3 that should help. For your case use lazy val
Gabriel Volpe
@gvolpe:matrix.org
[m]
cool, lazy val does the trick
Surely I can submit the issue for CE3, thanks!
Oleg Pyzhcov
@oleg-py
yeah you basically are using unsafe.global runtime and IOApp runtime at the same time, and those aren't the same and try to write to the same global var :\
Gabriel Volpe
@gvolpe:matrix.org
[m]
makes sense, it should not be a big deal, this example tries to demonstrate what not to do but if it can cause issues dealing with side-effectful libraries then that's probably a bug that needs fixing 😃
Oleg Pyzhcov
@oleg-py
the problem is that IOApp.main is supposed to run before the global is touched, but since val runs as an init code, the global is touched first.
Gabriel Volpe
@gvolpe:matrix.org
[m]
tpetillot
@tpetillot
Hi all, is it a normal behavior for IO.async callback to shift on calling thread for the following evaluations?
def printThreadName(): Unit = println(Thread.currentThread.getName)

def completeStageToIO[E](cs: CompletionStage[E]): IO[E] = IO.async { cb =>
  printThreadName() // thread-X
  cs.whenComplete { (either) =>
    printThreadName() // thread-Y
    cb(either)
  }
}

for {
  _ <- IO.delay(printThreadName()) // thread-X
  _ <- completeStageToIO(cs)
  _ <- IO.delay(printThreadName()) // thread-Y
} yield ()
Adam Rosien
@arosien
in CE2 async doesn't shift back, so you should add a <* IO.shift to the end of the completeStageToIO definition
Oleg Pyzhcov
@oleg-py
Yeah you need to use ContextShift[IO] to shift back and then back is only easy if it means "back to main compute pool".
Anton Sviridov
@velvetbaldmime:matrix.org
[m]

A common way is to do IO.async{...}.guarantee(ContextShift[IO].shift)

Not needed in CE3

Daniel Spiewak
@djspiewak
this is basically the number 1 CE2 gotcha in my experience
Anton Sviridov
@velvetbaldmime:matrix.org
[m]
Who doesn't want to run their entire IO app on AWS' threadpool.
tpetillot
@tpetillot
Right, didn't get quit well why the evaluation continue on this thread by default (in the JVM pov I guess). Got surprise finding myself on the datastax driver thread :crying_cat_face:
Glad to hear that it's shift back by default on CE3 (can't migrate yet, but still!). Thanks!
Kevin Meredith
@kevinmeredith

Hello - reading the well-written https://typelevel.org/cats-effect/docs/tutorial, I see:

def transfer(origin: InputStream, destination: OutputStream): IO[Long] =
  for {
    buffer <- IO(new Array[Byte](1024 * 10)) // Allocated only when the IO is evaluated

Can someone please explain why the new Array[Byte](1024 * 10) is wrapped in IO? I'm confused since I've not seen IO used in the context of construction, i.e. new ....

Thanks!

Fabio Labella
@SystemFw
@kevinmeredith have you watched my talk on shared state?
creating mutable state is a side-effect because it will break referential transparency unless wrapped in IO
Domas Poliakas
@domaspoliakas:matrix.org
[m]
Do you have an example of this? I've seen this mentioned a few times, but I can't quite come up with a scenario where that'd be the case
Where referential transparency would be broken that is
Fabio Labella
@SystemFw
I have a whole talk about it :)
anyway
def foo:IO[A] = {
  val a = new Array
  IO { a[0] = a[0] + 1 }
}
val a: IO[A] = foo
a >> a
behaves differently than
foo >> foo
Domas Poliakas
@domaspoliakas:matrix.org
[m]
Is it the shared state talk? My apologies, I have not seen it
Fabio Labella
@SystemFw
yeah that one
Domas Poliakas
@domaspoliakas:matrix.org
[m]
A very nice and concise example, thank you!
daenyth
@daenyth:matrix.org
[m]
You might find my talk useful: https://github.com/daenyth/intro-cats-effect
video link in there as well as the slides
almost everything in there translates to CE3, and where it doesn't there's analogous details
Domas Poliakas
@domaspoliakas:matrix.org
[m]
I did watch it aeons ago when I was getting started with ce 😃 yours and Gabriel Volpe's talks as well as the IO page on the ce website is what got me through the initial complexity bump. But the issue of how referential transparency is broken by just allocating mutable state eluded me
daenyth
@daenyth:matrix.org
[m]
a rule of thumb: if def foo = myexpr is not the same as val foo = myexpr, then it's impure
Kevin Meredith
@kevinmeredith
Thanks, @SystemFw and @daenyth:matrix.org! I did not know that Array[Byte] was mutable.
daenyth
@daenyth:matrix.org
[m]
yes, Array is mutable
Kevin Meredith
@kevinmeredith

^^ Per your val and def point, @daenyth:matrix.org , should the following code (from https://typelevel.org/cats-effect/docs/tutorial) be wrapped in IO

      orig = new File(args(0))
      dest = new File(args(1))

since new File will throw a NPE if the input is null?

daenyth
@daenyth:matrix.org
[m]
no
you shouldn't have null inputs in scala
scala code doesn't use null except when talking with java code
if args there is args from main, then use .get(0) which properly returns an Option
Kevin Meredith
@kevinmeredith
So the (1) impossibility of a null value from args(0|1) and (2) principle of never using null in Scala-only code, makes the IO wrapper unnecessary?
daenyth
@daenyth:matrix.org
[m]
basically
you can also Option(nullableExpr) and it will give you a Some/None based on nullity
Kevin Meredith
@kevinmeredith
:thumbsup:
daenyth
@daenyth:matrix.org
[m]
IO is just way too strong and you never want to use NPE as control flow
using IO to wrap those because of NPE is like having a try/catch block catching NPE