Fresh Async With Kotlin

Roman Elizarov

Recorded at GOTO 2018

developer since 2000 worked a lot of
virus high loaded distributed concurrent
systems I also teach coercing University
on concurrency which actually kind of
well down from my from the things i i i
did in my day job also do programming
competitions and the work now I work in
JetBrains on cosmic programming language
in the libraries team and that's that's
what we'll be talking about today
so we'll be talking about a synchronous
programming and let's take a look at
history bit so where it came from so
Catalan has the thing called coroutines
which for many people sounds new but if
you look at a secure the hitch was
actually very old concept so it's
it's similar in success 7 that first
introduced the concept of quarantines so
that you can do from a single program
can do multiple concurrent things then
you know the junior radios that you have
in some modern language is actually
appeared in 75 and 77
but the languages that are mainstream
now see that appeared in 72 say plus
pass from 85 Java C sharp and Kotlin
initially when it appeared in 2011 they
did not have anything like the courteous
of the 15 years old I mean from from all
these languages the prevailing paradigm
to do multiple thinks it was was threat
and I mean I once spent analyzing why
how and how this happened
you know this introduces did not include
curtain you saw that it was not deemed
necessary you know by the creators at
some point of time you know
system appears small to sweating and
everybody thought fares are fine
you can program with threats at least
that how it happened in mainstream you
know there were language just like
airline in 86 and for example go the
more recent that got away with threats
altogether they did not expose you know
threats to developers at all instead
they exposed lightweight processes go do
something like that that you know let
you do very concurrent applications
without working with residents but
that's not what we see in the mainstream
languages may swell just you know so
we're still in are still mostly very
oriented but that started to change in
the course of the last ten years so in
you know people started to write way
more connected applications that instead
of just number crunching they constantly
communicate which is other lots of
networking and that people started to
realize that in order to scale the
threats are not enough they cannot run
this massively concurrent things on
threats and so with these rights of a
synchronous programming we see that
among the mainstream languages C sharp
Wars first and 2012 India tree uses in
Kuwait the N consonant in seventeen
Willis experimental curtains and we just
recently made them released and that
that we'll be talking about now and we
also see that it probably come into C++
the also discussion in C++ working group
about any courteous to C++ Java was
working version so it's gaining interest
like all the major languages are looking
at it and since Xu sharp was the
among the general-purpose languages many
languages just coughing c-sharp approach
but cotton took a slightly different
path there's actually more based on the
historical approaches there were in the
in in history and to
we'll see what's approach of cotton and
how is different from what other
languages are doing an actual you see
that cotton and somewhat closer to like
food in current languages like Perl and
can go in its approach to proteins so
let's start with a traditional corn
accord approach to a synchronous
callbacks traditional I mean in general
purpose languages like that's how you
would be programming a synchronous code
in JavaScript say 15 years ago you'll be
doing it and you still can find out of
course I there's no more it's it's it's
becoming legacy but still that's kind of
traditional approach to doing a
synchrony in the general-purpose
language so the way you do it like so
instead of writing functions like this
that just you know you request a token
from server instead of just declaring a
function that returns a talking the
problem is this function that it will
have to wait and block the color until
they talking is there so we want to
really scale and you wouldn't be able to
do it if all you have like a single
thread like in JavaScript so with
callbacks instead of returning result
you you add an additional parameter
that's called a callback and that
receives this result when it's available
in the future so you do like with every
function you have in your code like if
you now then need to use this talking to
create some post on your social
application then any social network call
you also turn it into a callback by
changing its resolve to a parameter
function and then if you had this
process to request I can create this
post and somehow post process it then
you turn it into something like this so
instead of simple sequential procedure
you now have this nesting convicts and
this closing braces they called callback
hell and if you actually go to Google
and Google callback hell you'll find
like lots of hilarious pictures like
showing screenshots of real and imagined
code in this style and that's was kind
of comical was
main motivator for all the alternative
approach that appeared it was like
really this called the hell was really
you'll if you google yes you'll find
mostly futures in JavaScript because
again javis could be in single threaded
there was had no other way to do
concurrent things but in the past but
program was callbacks so all the
pictures about like in c-sharp Java
cotton you can do threads
you don't have to do it but anyway and
this is code is actually simplified
because the real code was called but
you'll have to worry about failures like
what happens is it called fails then you
have to add processing of this failure
so will code is much messier than this
simplified code I show it on this slide
so the solution is that's widely known
widely publicized and it's also very old
approach is to use futures which also
known as promises and they have actually
over here should open Wikipedia article
you'll see lots of synonyms to future
promise deferred you know Bubba Wallace
of different
they've been historically had different
names the idea is instead of returnable
zero to wrap them so instead of using
callbacks we return some wrapper on top
of our result type call it prom is
called future doesn't matter it seems
like every programming language has to
invent its own name for a future so
JavaScript call it promise
c-sharp calls a task you know Java calls
its future so it's like it looks like
you're not a big foreign language unless
you invent your own name for a future
like it's just not cool to use somebody
else's name for a future object so so in
JavaScript is promised for example so
the idea is that you come so late this
future resultant the object you wrap it
and that's how you transform all your
functions in the end you know instead of
writing this nesting Colbeck code you're
operators to combine
result in two successive computations so
callbacks are still there they're still
behind the scenes they're just
encapsulated and you add this call this
on each step of your multi stages in
Joe's computations but you don't nest
indentation anymore so there's no this
Cobra he'll letter of closing curly
braces and you actually this this is the
actual code that actually currently
handles exceptions so that makes
features so much popular they solve like
many many of the issues of the called X
but they still way away from being
perfect and which is especially
noticeable if you compare them for
example this code to the code you tried
in Erlang or in gold which gives you a
lightweight process is out of the box
because you know with futures you have
to program with all those operators you
can't just write code like you usually
do you have to literally learn a
different way of programming to write a
synchronous code with futures you have
to instead of your regular control
console it's a credential execution
eaves loops exceptional you have to
learn completely different set of
primitives and the set of primitives is
the the way it's name also depends on
futures so that's that's that's kind of
its harms readability and lets you learn
new stuff for no particular reason
so the end that's what cotton curtains
help content curtains were designed not
only to solve the problem of callback
hell but also to make programming easy
to make your code readable so with
cotton curtains instead of changing a
signature of your function so it's a
result type is different now and you
have to use different combinations in it
you just live in natural signature here
for function but you mark it with a
special modifier suspect and this
modifier suspend
means that this function can suspend
execution and that's what Koreans are
known for their was these few years
concept that you can have a computation
but instead of you can suspend in some
point and continue working on it when
some result is becomes available so
every function you have in your multi
state approaches process you transform
the same way so instead of returning I
promise you again
signature is natural just suspend
modifier item and now instead of this
new way of programming you actually
program it in the old way you just write
the code as you usually write it just
but you just mark it with suspend
modifier everything inside staves
wriggle you don't have to learn you
programming style anymore you is just a
normal code with normal control concepts
you know of and the difference is of
course if you program with this highly
manual highly variable style with
promises of futures you actually saw the
places in code where those as
synchronous points where result will be
available in the future you will
continue the use in the future you don't
it's not obvious anymore in the code
which is made this way for reason
because there's a different concept that
you should not be thrown at you when you
think about your business logic but you
you there's also an option to see like
if you open this code in ID you can
actually see what are the suspension
points in your code so if you need this
information it's there it just slightly
to the side so it doesn't obscure your
main business the flow of your business
logic what happens after what and
there's also bonus features who comes
with it you can use regular loops you
don't have to figure out what my
operational future is to do a loop Inc
on a list you can use regular exception
howling like you learn language once and
then you apply it in different scenarios
you can use all the higher-order
functions like cotton is very functional
language it comes lots of lots of this
higher-order function that allows your
process lists efficiently etc and you
can use all of them without learning new
functions for your promise
you can write your own higher-order
functions like if you need to retry
operation a number of times with some
exponential they call for sums like this
you just write it as you would write it
regularly and then write your own high
high order function use it so everything
works with concurrent just at worse in a
regular code there's no new style to
learn but just one suspend modifier so
let's quickly take a listen because when
when you show people who haven't seen it
first time it'll say magic and there's
nothing good about major software
development like magic is something you
open do not mix well so you have to
understand at least on some level how it
so when content compiler sees a function
that's marked with suspend modifier any
caught mine runs on GBM so I'd actually
behind this thing gets compiled into
seven GBM declaration in which GBM
itself does not have a concept of
suspended for since the calls in concept
not a GBM concept so when it compiles
it's actually composed to function with
one additional parameter and this
conditional parameter has a type which
called continuation but it's actually
you would recognize it's just a callback
program it was call back so if you go to
coughing standard library and see what
continuation is you'll see in this
interface it's there's not much inside
of it you know it has a context that
defines where the stuff at executes and
provides additional information and it
has a callback function to resume this
computation with a certain result where
result can be their success or failure
that's it there's nothing more callbacks
and initiation is actually the same
thing and when you hear somebody says
you know we're doing continuation
passing style
blah blah blah it's just a very
glorified name for programmers callbacks
now how it soul integrates into the
ecosystem conventional cotton language
they translate GBA Mecca system
in particular in TV I mean hot so
happens there is a zoo of those futures
libraries that's historical isn't what
Java came out back there in 95 it did
not have any facilities for it did not
have a future type and the the actual
future type that you can use for
synchronous programming that's called
computer future completely disable
future appeared as far as a member jelle
5 which somewhere somewhere in the I
remember what was the year but but
anyway it's by that time already there
were lots of competing libraries and if
you look at any reasonably sized
enterprise piece of software you'll
definitely find so much own future types
hidden somewhere inside it's again the
GBA maker system at this respect is
really ugly
like it's not like for example dotnet
it's more standard there is more
standard future sites but in Java there
is a zoo so you would also face you
often face this situation you have this
for example there's a library called
retrofit now that's quite popular on the
engineering world in a retrofit you can
call you know you can call remote
services over rest easily just a bit
funny an interface with corresponding
function but you're right Arif it has
its own future type this future type is
called call so it's when you see this
function you quickly recognize this
spire you know we study for result we
will return some rapper in top referrers
out that represents this result in the
future that's that's that's a promise or
a future so how do we integrate it with
calling coyote ins so in cousin
quarantines we're working with
suspending functions that have the
suspend modifier so we just try to adapt
our function that you stand for journey
call returns just just the result object
in this case it's a passed and by
convention what we do for the zoo or
future types we define an extension
function called the weightages
convention we use it's not
anything they would suspend the
execution of this the courage in calling
this function until the result is
available but how we write this way you
know so in color so of course it is a
function that has suspended if I
declared as extension on our call but
what's going inside the only way for a
synchronous execution in most of the
libraries like that it's actually based
on callbacks the callback is the most
primitive approach to a simplest program
if if you look at this call for example
you'll find any queue a method that
installs a callback in in every single
future types you'll find anywhere has
some method it may be called at listener
you know something this that installs a
copy but instantly Kovac is not enough
because if we write the body of this
function here I am still callback you
would not do anything to the color just
you know the function returns but we
need true somehow return result from it
we have somehow need to suspend
execution of curtain until the call
backfires and that's where a special
library function called suspend care
routines come from so that's suspend
curtain that's a deed of it's a special
function in the coughing standard
library that's that actually does a
trick of suspending execution until some
event happens let's take a look at it
that's a signature of suspend curtain so
suspend curtain you see it's defined as
suspending function but you see it takes
inside a functional type that's regular
that's not marked with suspend we're
just a regular block of code that takes
continuation so if effectively what
suspect curtain does it takes what
happens after it revised this into this
container object and passes it to
parameter to this block and this concept
that should really really old so in
doing for example is from a language
called scheme this function was called
call slash cc-cold with current continue
actually we were this concept from the
scheme into Kotlin an actual initial
prototype we called it you know call
with current consideration but then you
know then after future agency we name it
to suspend co-routine to make it more
understandable what it actually does but
it's make no mistake it's basically
that's what call CC didn't scheme we're
way back in time so now when the
invoking suspend curtain you can get
continuation and the curtain that wasn't
working this function is suspended now
so now you can install a callback and
when the co backfires receives to
respond you can analyze it and you can
either resume this continuation with the
successful that there's a Hindi
extension called resume or you can
resume it with exception and tell no the
you know this is failed and only when
of the whatever process you had
continuous that that's all that's basic
all the code it takes to write to
integrate some future library with
cotton codes no no that kind of reduces
the whole zoo of futures you have to a
simple kind kind of concept of cartoons
when so but that's just one side of
integration the other side of
curtains so how you start your a
synchronous processes when I showed you
this slide with this simple example
initially I said all you have to do is
to add a suspense magnifier if I have a
multi-stage process that is invoking
some a synchronous functions but what if
I remove it whatever you do like this
what if I just want to do something just
read regular function and start doing
things that take long enough time that
do network requests I get an error from
compiler and it says you know I
this is request okay suspending function
and I can only call it from another
suspending function
huh what does it mean you see if you
think about it it makes sense functions
marked with suspend minified they have
this super power they can suspend
execution of the caller for a long time
which regular function can't I mean your
regular function can just block the
caller completely blocking the poker
thread but they cannot do it like in a
synchronous way without but blocking
anyone but suspend function can they
have this super power in regular
functions they don't in our regular
world regular languages for programming
they don't have this super power so and
so they can't invoke anything that that
suspending so what do we do we cannot
simply invoke a suspending function we
need to find some workaround and the
word current in this we have a family
function called curtain builders and the
most primitive of them is launched it's
called lunch because it's used for
fire-and-forget Kuragin you can think of
roughly like a starting you thread your
finding of a new process you can think
of a very lightweight process in your
program that starts doing something
completely concurrently with the rest of
your code and does it once on any the
rest of you could just continues doing a
thing we use launch when we interested
in side effects of some code for example
this case we're interested we want to
juice a network request to get something
our back end so we'll launch a
concurrent process to do it for us the
launch itself returns immediately like
this is regular function doesn't block
anything so our post item function just
returns immediately having started this
program process in a background thread
poll but this means that if we do any
post processing here it means that the
sample grant set pool which is OK for
server-side applications but may not be
a key for UI applications because he in
more UI applications he usually limited
to working with you UI from the
main UI thread that's that's how it
works in Android that how it works in
all the major you have frameworks out
there so so what if after doing this I
want to update something on you I I can
do this it from here
because that's working in background
thread and current use give me very easy
solution I can just when I launched in
encourage and I can specify dispatcher
for it and dispatcher controls that's a
bit of coding context that controls word
execute and I can says please execute on
my main thread and that's the only
change I need to do to make sure that
everything inside the curly braces gets
executed on my main thread where I can
go and safely touch you I it and see
because it's all based on suspending
functions and suspending functions deep
inside a based on the callbacks you know
my main threat is not actually blocked
so I get multiple concurrent activities
running in the main thread with just
just one simple invocation what what
makes launch such magical if I told you
that I cannot simply invoke suspending
function but launch somehow does it the
launch itself is a regular function it's
simplified signature shown here because
it's very long evening woken from
anywhere but it accepts as a block not a
simple functional type but it's in
collisional type Marcus suspend metaphor
so inside of the block that I give to
launch it says a lambda that is
parameter to lunch I can I can actually
use the spin function so that's so it
works as adapter between a suspending
world in regular world in another way so
it lets me it can be called from regular
functions but I can use as many
and launch also actually not just first
to the process I just created we just in
continent speak called a job
and this lets me like cancel it check
its status if I need to I don't have to
but but I can I can keep it if I if I
want now let's think I've promised you
at the beginning let's take a look at
the asynch await I call it a classical
approach to secrets program because this
that's if you look at the history and
among the mainstream languages C sharp
was the first to integrate a synchronous
facility right into the language and
they introduced this a single weight
pair and it is kind of catch on like
lots of other languages followed this
you so let's see how it works there
so in cotton I would write a code like
this we've seen it multiple times so far
if I write this in code in C sharp the
end you'll noticed a few differences
let's see what look very similar but
there are important differences let's
count them the so that's that's also not
just C sharp it's a typescript in Dart
this is approach is going to integrate
it in JavaScript like it's it's like C
sharps set the trend here so the first
difference that's obvious syntax a
they use a sink that's just different
words okay and we'll the other
difference is that in this approach like
my all my points where I am suspending
our explicit I have to use a weight
keyword and the other difference there's
not so obvious but very very important
as we'll see later on is that whenever a
distinct function it's not to returning
the original result research only future
in c-sharp this call to task and that's
actually not the syntactic stuff like a
weights in a sink but that's the fact
that these functions return a future
actually makes a difference so we don't
have a weight keyword in common and some
people ask why don't we have it
like on this keyword but it's not the
problem with the weight because we don't
have it's actually the whole problem
with a sink approach the reason we did a
different way in cotton and you see if
you look at C sharp and you have a sync
function C sharp for example request
token that's a valid expression and in C
sharp or JavaScript a type script and it
produces your future so the question can
self were trans immediately concurrently
start this process you know if
requesting it okay voters the future and
when you wear a weight request token
that's also valid expression its type is
a token it is and if you compare those
two you want is that the first is
concurrent I'm writing across token and
I'm now have two concurrent processes
working in my system one is working on
talking the others continue execution
and the second one is sequential
behavior when I write a way to request
talk and there's no concurrency it's all
happen in sequential any languages like
C sharp and all the other images that
copy this approach this concurrent
behaviour default like that's that's
what you get by default unless you
explicitly obtain investigational
behavior by using a keyword and we
believe that certain approach we believe
that concurrency hard for developers is
error-prone and it has to be explicit
so in Kotlin suspended function designed
in such a way as they imitate sequential
behavior by default in cotton sequence
behaviors they hold and you have to opt
in into concurrency when you need it
we'll see how it happens so cotton
approach to a thing is slightly
different it's just as powerful
it just defaults a flipped around so to
see why we needed let's see an actual
use case well is it hard to to discuss
absolutely let's see what's the actual
use case for c-sharp style async
functions what c-sharp why you need
sometimes actually need this Congrats
so let's take this example I have a
that's reading a seizure there's a
sequence function New Jersey future and
it synchronously loads an image and when
and then what I can't do in c-sharp I
can write this kind of code I can see
let's load first image and concurrent
lowest or the second image and then I
can right let's have wait for the first
image and I wait for the second one and
while writing the code like this I truly
get concurrency and that's the use keys
we're actually want this confront sight
you want my two images to billow it
concurrently just to speed things up
because later they want to combine them
somehow process but I don't want to
sequential one after another I want
concurrency and c-sharp you know being
up working currencies default lets me do
this so how do they do it in cotton I
can also write a sync function in Kelvin
the differences is that instead of
writing a sync to the left I write it as
a regular function and it returns the
future again being a languages that I've
become big player we needed to invent
our own name for future so we opened
Wikipedia found a list of synonyms for
future found deferred so it's not used
yet in severe my sister so great will
we'll take it so our future type is
called deferred and we read I think on
the right so alright as seen in codling
world is just this cartoon builder that
takes a bit of code it turns it into the
future I mean I mean basically you can
do the same like you see sharp words you
can write those icing functions just
since its slightly different and you can
use them just as you did in c-sharp
you can start multiple versions
concurrently by using the sync function
because they now regular functions that
return immediately and just give you a
handle if you should promise to get this
result and then you can await for them
again the difference is only syntactical
with c-sharp code weight is not an
extension it's not a keyword in the
languages of function on this defer that
lets you wait for its result but if you
carefully look at this code
you notice a problem here they actually
began ugly problem what if this wait
crashes what if loading first image
fails some network error something this
just does not complete and throws
exception cannot load the first image
now this the way they were very defined
in Catalan and in all the future based
languages is that throws exception in
this case and the whole procedure that
we have on the screen the words but now
this second future just leaves it's
still award in the second image in
background just nobody going through
this result anymore because we're not
coming to a second a wetland never now
we have this big run process that's
occupying resources doing some work and
we can put a lots of reference to it now
incorporation and it fails on her first
image again we have another leak of law
in second image and we try and do it too
I will quickly run out of resources and
that happens like all over the place in
real actual code people right people
just don't notice it because it happens
only so rarely and occasionally when
there is something is not working just
exactly right and that's that's a real
problem so the way we solve it in cotton
is is based on few things first thing is
what we Adam attic style of writing this
stuff of cotton is by writing a
suspending function so what we tell
people don't write a single function
don't write functions there to return
futures because they are error-prone
don't do it write functions with spent
modifier they're sequential what do you
think is there less error-prone we'll
see why yeah if I need concurrency I can
do concurrency just where I needed so I
when I want to do something concurrent
first of all I use a concept of
structured concurrently I delimit the
scope of Michael Garcia all right
this thing is cousin it's called curtain
scope I just write some block of
code to explicit l here's I'm doing some
current things and inside this blog I
can't just use SC as a high-level
function and it becomes concurrence now
becomes explicit I read a synchronously
Lord first image and the right eye sees
a lot second image and it's returned me
it would transmit those futures and then
I can wait on them and process them
later so cotton approach to think let's
analyze the difference
the first difference in content approach
and the sync versus C sharp is that in
cotton if I follow :
in a magic approach of suspended
function just invoking request token is
a valid expression and it produces me a
result and there's also another way to
invoke it tell a sync request token it's
also valid expression and it produces me
in future and the first one corresponds
to a sequential the behavior and the
second one concurrent and the first one
shorter is and default so signal the
behavior is default and concurrent is
what you opt in if you need to but also
cotton approach is different in the
other things because it forces you to
add her to structure differential
concurrency see when you write this code
like this you are forced to write this
code in scope construct because a sync
is defined extension function encouragin
scope object you can just write a sink
out of thin air you'll see previous
example I have to say global scope a
sink you have to explicitly tell what is
the scope you're doing your concurrent
things in and why it's helpful let's see
what happens here
now if now loading for the second image
crashes just we had the problem before
with the futures because this is sink is
started in this current in scope it goes
and cancels this courage in scope and by
canceling it it also careful cancels all
other concurrent activities we launched
in this scope so now this whole
process your awards cleanly without
leaking any concurrent activities and
leaving them behind you know that's a
game changer that lets you not only
write software that uses concurrency
where you need it but it's also as you
do without risk of losing your
concurrent processes and leaking
resources so the key takeaway here is is
caught and suspend functions may be
concurrent if they needed but they don't
leak it they don't leave any practice
behind with it where they're recharged
there were we force concurrency to be
structured and the one important thing
to note there is when people often
confuse concurrency and parallelism they
you know you'll see it everywhere but
how do i do threads blah blah blah and
column based on Shiv employ from does
support parallelism you can have
multiple threads but and do things in
parallel but this prism is completely
optional like because parallelism is
like all level implementation detail of
your code like how you schedule it among
thread that's not what you should be
thinking up front when you design your
software so when you design your sorter
you should be thinking about concurrency
like how you decompose your large task
into concurrent processes that interact
with each other and realism which is
this implementation detail and in cousin
curtains you can have hundreds thousands
of concurrent processes all running the
same thread you have your main thread
that does all UI and you have have
concurrent animations concurrent network
requests all working in the same thread
and you can use prove ilysm but it's
like optional concern you can place
those dispatchers and you know specify
them just in places where you need pearl
is and where you want them cpu heavy
computation to be offloaded on to
another thread to be parallel with the
rest of your work you can do it but it's
optional you're not forced in being
parallel with cause encourages it's an
optional aspect
and what's important and this also gives
you an answer why we didn't call these
modifier wise call suspend or the sync
like we got three different wait and we
know why but like we could have called
the suspend keyword and a sing keyword
think of them but no we didn't because
suspending functions in cotton they do
way more than allow for a synchronous
programming they actually the language
mechanism to support synchronous code at
the same time using the same mechanism
so let's take a look at this example we
want to define a sequence of Fibonacci
number as some piece of code and
Wisconsin curve since that's easy to do
we just use this sequence curtain
builder and then we can say you me first
ten few minutes numbers and cover to
list and and you get a list of first ten
Fibonacci numbers and it's worth based
on the same concept like sequence is
it's also curtain builder it's just a
difference with regular curtain builders
that it's restricted in the sense you
cannot do much inside of it but just
yield values to the color and the
operation of this the difference between
those curtains encourages we see before
it does the curtain here is completely
synchronous there's no I think only
whatsoever here the operation of this
curtain for sequence is completely
synchronous with with the rest of the
code what does it mean if I take my
Fibonacci sequence security an iterator
nothing happens no piece of code from
inside the Fibonacci sequence wants when
when I request a next number from it
that's where this code starts to run it
goes over its logic it initializes some
variable and yields the first value and
this place you know control turns back
to the color completely synchronous way
Noah synchrony whatsoever
then if I request next item of Malaysia
sequence then it resumes you know
completes the rest of it logic you know
confuse the next well your Fibonacci
sequence and returns to the completely
synchronous there's no I think only here
and that's why you know that's just one
of the reason why we couldn't have
called you know this cure this thing
that just felt wrong here so however
when you start programming with multiple
colleges it doesn't matter whether the
sequence s it goes you you at the end
you run into this problem of sharing
state and what do you mean by share
instead you know in real life we have
some entities in our applications they
have a state and we apply lots of
operations to them and the state needs
usually new synchronization if we follow
traditional approach of multi-threading
you know we start worrying about locks
about monitors etc that gets messy and
this is a good example of what kind of
problems shared mutable state leads in
real application like if you forget
synchronize something stuff like that
now you get this kinds of behavior that
you you can you can will like you know
spend days trying to troubleshoot their
mysterious crashes in your apps that's
that's that's like very good promise
shared state list you but there was a
solution solution is again very old one
it's been invented in Anthony for like
like ages ago
it's called CSP communicating sequential
processes the idea is that of using
beautiful state we we don't do it we
don't share mutable state instead we
communicate and just give you because I
mean it's it's not a presentation about
this but just give you a glimpse of how
it works the cut encourages
so Colin Curtis we can instead of this
sequence precious sequence we can also
create this concurrent for manage the
sequence you can just change a builder
to build called produce
and produce is this a synchronous
builder that launches a curtain network
simple grounded synchronously
and this communicates with the rest of
the world over communication primitive
called the channel so if from inside you
can send values to this channel and from
outside you know we can run another car
routine from our main thread we can say
I want this main curtain that just
blocks the main thread and I can receive
from this channel this like lets me
encapsulate my state inside a sequential
piece of code that's where this equation
comes from in CSP but let them
communicate over the channels and this
opens a completely different style of
programming where instead of sharing
mutable state we share information by
communicating and again it's there's
also accumulated knowledge of how to
program on the state lots of other
languages supported and that's yet
another reason for example why don't we
have a weight keyword because it just
looks ugly when you start writing says
PS much easier to have this nice
affluent receive and other functions
like that so it all works just be like
before but now it's a synchronous now we
have a channel a synchros channel that
lets currents even run a difference fact
communicate to close it up let me recap
some non-traditional choices that cotton
made it with subjective origins first of
all if you look at classic approach like
in pythons say for example one example
is language that does it to do carbon
teams they have loss of keyword lots of
new concept I think origin radials
lots of different keywords in cotton
this is just one suspend modifier that
does all of that I mean just just one
addition actual to the language we did
we added support for the suspending
functions and in cotton actually the
standard library is very thin standard
librarian cotton provides very few basic
suspend curtain that enable you to
integrate with all the other libraries
you have in everything else like current
team builders support for futures etc
leaves in a separate library which is
open source you can poke poke around
eight you can take how its implemented
what does it do you can we implement
improve do your own things on top of
this very small core library of
primitives and if you want to learn more
the best place to learn just go to
official website cotton land Network so
we just recently released cotton 1.30
and stabilized curtains now it's stable
support the feature of cotton and it's
now featured on the website there's in
the learning section there are lots of
documentation that you know you can read
to learn tutorials etc etcetera
so take a look there's very active
community communicating over slack you
know discussion going where you can ask
questions thank you very much