Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Rob Norris
@tpolecat
Can you assign it to a variable?
val x = def ...?
Can you pass it as an argument?
foo(def ...?
Vinayak Pathak
@vinayakpathak
i see hmm
Fabio Labella
@SystemFw
@vinayakpathak def is not enough, def + by name arguments might be
Vinayak Pathak
@vinayakpathak
so what is def then?
Fabio Labella
@SystemFw
but then you realise that that is the same as saying that IO is () => A
Ethan
@esuntag
syntax for defining a method
Vinayak Pathak
@vinayakpathak
@SystemFw yes fair
Fabio Labella
@SystemFw
which I show is not enough in the post
for example you can't do async or concurrency
error handling is weirder
and the api you get is much more limited
Vinayak Pathak
@vinayakpathak
i see i see
so above I asked "what's the minimal thing that would ensure referential transparency"... perhaps () => A is a good candidate?
Fabio Labella
@SystemFw
for synchronous stuff only though
Vinayak Pathak
@vinayakpathak
yeah ok
Fabio Labella
@SystemFw
and without errors
Vinayak Pathak
@vinayakpathak
true
Fabio Labella
@SystemFw
but yeah, at the end of the day you need suspension
because you want to separate evaluation and execution
that being said, this is trying to understand (imho) how IO is implemented
rather than its semantics
Vinayak Pathak
@vinayakpathak
i see
Drew Boardman
@drewboardman
is he saying def is not RT because it's lazy and not memoized?
because I don't see how that follows
Fabio Labella
@SystemFw
no
we're trying to discuss where def is enough to ensure referential transparency, which it isn't
Vinayak Pathak
@vinayakpathak
def + by value is not referentially transparent because you will evaluate the arguments prematurely
Fabio Labella
@SystemFw
exactly
def + by name is, and in that model val is the equivalent of <-
but the whole thing is then easier to understand by considering IO[A] = () => A , which has the limitations outlined above
(you can replace all def t: A with val t: () => A)
Paul Snively
@paul-snively
Maybe it's worth looking at the implementation of Id, which you might think of as "the fake monad."
Drew Boardman
@drewboardman
Id is a datatype
Paul Snively
@paul-snively
So is IO, and they both have Monad instances.
Among others.
The point is, you can compare what happens in various scenarios in code using Id and in code using IO, and see how they differ (because the latter takes great care to satisfy various laws under a broad range of operational conditions, and the former doesn't).
Fabio Labella
@SystemFw

https://github.com/SystemFw/Scala-World-2019/blob/master/Examples.scala

Here there are a few implementations of IO, the first one of which is essentially with () => A. But I do think that looking at the implementation is not the right way to understand IO at first

which is great, it means we actually have an abstraction
Christopher Davenport
@ChristopherDavenport
I’m missing a lot of context. But Id is by no means a fake monad.
Paul Snively
@paul-snively
Looking at the implementation of IO is a horrible way to understand it, I think.
Vinayak Pathak
@vinayakpathak
@SystemFw nice, that's great. that answers a lot of my questions. I'm probably going to start thinking of IO as () => A until I realize it's a huge simplification
Fabio Labella
@SystemFw
well, I wouldn't recommend that :)
Paul Snively
@paul-snively
@ChristopherDavenport: Sure. I should have put "fake" in scare-quotes, at the very least.
Fabio Labella
@SystemFw
this is a general point about FP btw
trying to start using algebraic thinking
it's actually the biggest benefit imho, one of the ultimate goals
and it's hard to get your head around that at first