В моем коде используется следующая общая библиотека
Module Shared
let state = new AClrClass()
let fun1 x .... = // shared function
.... // uses state
В примерах использования общей библиотеки state
совместно используется всеми функциями, даже если несколько функций main
(в следующем коде)вызывается параллельно.
Module Caller
let f1 x = Shared.fun1 x .... // other code omitted
let f2 x = Shared.fun1 x .... // many other functions uses the function in the Shared lib
let main () = // entry point of the module. Calls f1, f2, f3...
Теперь мне нужно переключиться на другую реализацию Shared
, которая определяет класс (поэтому каждый вызов Caller.main
будет иметь свой собственный state
)
Module Shared
type Shared () =
let state = new AClrClass()
member __.Fun1 x .... = // shared function
.... // uses state
Мне нужно обновить модуль Caller
.Могут быть следующие подходы
Добавить еще один параметр aClrObj
ко всем функциям вызова общей библиотеки
Module Caller
let f1 o x .... = o.Fun1 x .... // other code omitted
let f2 o x .... = o.Fun1 x .... // many other functions uses the function in the Shared lib
let main () = // entry point of the module. Calls f1, f2, f3...
let aClrObj = new AClrClass()
f1 aClrOjb x ....
Определитьизменяемой переменной и установите ее в функции main
.
Module Caller
let mutable o = new AClrClass()
let f1 x .... = o.Fun1 x .... // other code omitted
let f2 x .... = o.Fun1 x .... // many other functions uses the function in the Shared lib
let main () = // entry point of the module. Calls f1, f2, f3...
o <- new AClrClass()
let aClrObj = new AClrClass()
f1 aClrOjb x ....
Какой подход более идиоматичен для F #?Как должен быть построен код?