Does the same instance of the builder class use side effects at the same time?

I want to use calculation expressions inside an implementation of the F # class, intended for consumption by C #. The interop class itself is singleton (one instance is connected in the container) and is used in streams (web requests).

The builder itself consists only of methods and does not have fields or support states.

Given that in F # the following is accepted:

module A = let private build = new SomeBuilder() 

Does this mean that several expressions related to one builder can be evaluated simultaneously without problems?

+6
source share
2 answers

Under the hood, the builder does not work at all. The compiler simply converts the calculation expression into a series of method calls in the builder, and then compiles it.

Therefore, the thread safety of the builder is entirely dependent on the thread safety of its methods - that is, the methods you write.

For example, the following code:

 myBuilder { let! x = f() let! y = g(x) return x + y } 

Will be converted to the following:

 myBuilder.Bind( f(), fun x -> myBuilder.Bind( g(x), fun y -> myBuilder.Return( x + y ) ) ) 

(NOTE: the code above may not be accurate, but it conveys the essence)

+6
source

A clean, stagnant builder is safe to use at the same time.

Calculations express mostly syntactic sugar. There is no effective difference between using a builder's computational calculations or calling his methods directly.

+5
source

All Articles