-
Hello, I'm currently exploring the Code: using LanguageExt;
using static LanguageExt.Prelude;
var comp1 = bracketIO(
Acq: IO.pure(new MyResource()),
Use: res =>
from _0 in liftIO(async () =>
{
await res.PrintAsync();
})
select "Finished1",
Fin: res => release(res)); // I'm not sure what should be here instead of "release(...)"
await comp1.RunAsync();
Console.WriteLine("Finished comp1");
Console.WriteLine();
var comp2 =
from res in use(() => new MyResource())
from __0 in liftIO(async () =>
{
await res.PrintAsync();
})
from __1 in release(res)
select "Finished2";
await comp2.RunAsync();
Console.WriteLine("Finished comp2");
class MyResource() : IDisposable
{
public ValueTask PrintAsync()
{
Console.WriteLine("MyResource");
return ValueTask.CompletedTask;
}
public void Dispose()
{
Console.WriteLine("Disposed");
}
} Output:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
So, if you want to use that approach, the This is what it looks like internally: new IOAsync<C>(async env =>
{
var x = await RunAsync(env);
try
{
return IOResponse.Complete(await Use(x).RunAsync(env));
}
catch (Exception e)
{
return IOResponse.Complete(await Catch(e).RunAsync(env));
}
finally
{
(await Finally(x).RunAsync(env))?.Ignore();
}
}); If you want something to guarantee that all resources within a particular scope are cleaned-up properly, use i.e: var comp = bracketIO(
from res in use(() => new MyResource())
from _ in liftIO(async () => await res.PrintAsync())
select "Finished2"); What that version of This is what it looks like internally: new IOAsync<A>(async env =>
{
using var lenv = env.LocalResources;
return IOResponse.Complete(await RunAsync(lenv));
});
Hope that helps. |
Beta Was this translation helpful? Give feedback.
bracketIO
with theAcq
,Use
, andFin
is a more explicit approach to managing resources. It doesn't use the built-in resource-management, sorelease
won't do anything becauseAcq
didn't put the resource into its tracking-map.So, if you want to use that approach, the
Fin
part should be:IO.lift(() => res.Dispose())
.This is what it looks like internally: