I'm trying to solve this issue (psdeudo code):
let myObject : T option = None
onEvent (fun t -> if myObject.IsSome then myObject.Value(t))
some logic (
while forever do
myObject <- Some T()
// do something
myObject <- None
// do something
I have an object that gets created and destroyed depending on some external parameters. There is an even that is always firing that brings data to either be processed by the object, if it exists, or to just be ignored.
The issue here is that between:
if myObject.IsSome
and
myObject.Value
the state could change. Is there a mechanism to handle this?
like something that could be like Option.TryGet that will either return an object, or not, in an atomic fashion?
or is there any other mechanism I can use with that?
I guess I could try to get the object value directly in a try / with section, but I was hoping for something cleaner.
question from:
https://stackoverflow.com/questions/66047448/thread-safe-option-in-f 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…