raulraja
01/03/2019, 9:07 PMNarayan Iyer
01/04/2019, 1:51 PMmain
or some such designated function marked with IO
or similar monad.Narayan Iyer
01/04/2019, 1:59 PM/api/users/:username
In the http
layer, the request is forwarded to:
Users.get(:username): IO[User]
in the domain
layer, where it then does some business validation, and then forwards to the database
layer:
DB.get(table: User, username: String): IO[User]
, where it interacts with external world and executes SQL and retrieves the User
profile.
Now, what if I do not use an IO
monad? This thing could blow up anywhere, right? What are the disadvantages? I am trying to explain this to a colleague who has C++/Java background, and his question is like what difference does it make, when a program fails it fails! How does it matter? Appreciate your time @raulraja . Thanks!raulraja
01/04/2019, 5:37 PMraulraja
01/04/2019, 5:37 PMraulraja
01/04/2019, 5:38 PMraulraja
01/04/2019, 5:38 PMraulraja
01/04/2019, 5:40 PMraulraja
01/04/2019, 5:43 PMfun printStuff(): Unit = println("stuff")
That is impure whereas
fun printStuff(): IO<Unit> = IO { println("stuff") }
it's pureraulraja
01/04/2019, 5:46 PMprintStuff()
and that makes a huge difference because now you can reason about that function knowing that when invoked it won't trigger effects. Also the fun
can be replaced by a val
and now you have a program in a value.