Condcannot be replaced with switch because in switch both data and [case] are the same thing, while cond:if and cond:data are not. Data is anything and :if is always bool. I.e. it's not possible with switch to route data based on some condition other than value of that data.
Why Cond is irreplaceable by deferred connections?
Short answer: It solves race-conditions (this is why it was introduced in the first place).
Long answer:
Let's try to recreate this example with switch and defers
A little bit more repetitive than cond statement but still ok and without need for extra syntax. However, the way it desugars, creates unintuitive race-condition:
Note that we replaced true with condition - it means condition is dynamic now. Now let's consider this set of events:
condition=true -> switch:data
condition=false -> switch:data
We expect sending to :yes first and to :no second right? But depending on undefined conditions (such as CPU temperature or phase of the moon) we might get different results! Let's continue:
switch:case[0] -> lock1:sig
switch:case[0] -> lock1:sig
Still everything ok but what we might get next?
lock2:data -> :no
lock1:data -> :yes
Race condition! Out of order delivery! But why? Well, because lock1 and lock2 are two separate goroutines that operate concurrently. Not just lock2, despite receiving second, might finish its job and send faster than lock1, even more than that - both lock1 and lock2 might be suspended and for some reason Go scheduler might run lock2 before lock1.
This is the reason Cond (and some other components) were introduced. Order of delivery can be controller at the level of one runtime function, but not multiple concurrent onces.
On missing language feature
It feels like this problem should be solved by some language feature like deferred connections but different one. I have no idea what it is at the moment though.
On need for this syntax
I should probably note existing way of using cond - without special syntax for it - is far form perfect but managable. So it's questionable if we truly need this.
Idea
Before
After
Why
Cond
is irreplaceable byswitch
Cond
cannot be replaced withswitch
because in switch bothdata
and[case]
are the same thing, whilecond:if
andcond:data
are not. Data is anything and:if
is alwaysbool
. I.e. it's not possible with switch to route data based on some condition other than value of that data.Why
Cond
is irreplaceable by deferred connections?Short answer: It solves race-conditions (this is why it was introduced in the first place).
Long answer:
Let's try to recreate this example with switch and defers
A little bit more repetitive than
cond
statement but still ok and without need for extra syntax. However, the way it desugars, creates unintuitive race-condition:Note that we replaced
true
withcondition
- it means condition is dynamic now. Now let's consider this set of events:condition=true
-> switch:datacondition=false
-> switch:dataWe expect sending to
:yes
first and to:no
second right? But depending on undefined conditions (such as CPU temperature or phase of the moon) we might get different results! Let's continue:Still everything ok but what we might get next?
Race condition! Out of order delivery! But why? Well, because
lock1
andlock2
are two separate goroutines that operate concurrently. Not justlock2
, despite receiving second, might finish its job and send faster thanlock1
, even more than that - both lock1 and lock2 might be suspended and for some reason Go scheduler might run lock2 before lock1.This is the reason
Cond
(and some other components) were introduced. Order of delivery can be controller at the level of one runtime function, but not multiple concurrent onces.On missing language feature
It feels like this problem should be solved by some language feature like deferred connections but different one. I have no idea what it is at the moment though.
On need for this syntax
I should probably note existing way of using cond - without special syntax for it - is far form perfect but managable. So it's questionable if we truly need this.