Repository navigation
Replies: 1 comment 7 replies
Aff<X> effect = from result1 in YourFirstAffFunction() // Aff effect
from result2 in YourSecondAffFunction() // Aff effect
select UseBothValues(result1, result2); // Regular, non-Aff returning function
Fin<X> result = await effect.Run();This is called monadic binding. All monadic types support this method of chaining. In fact, you can think of monads as programmable semi-colons (the side-effect of the monadic flavour is run between the lines of your code). If you weren't using LINQ, then the code above would be written like so: Aff<X> effect = YourFirstAffFunction().Bind(result1 =>
YourSecondAffFunction().Map(result2 =>
UseBothValues(result1, result2)));
Fin<X> result = await effect.Run();Which obviously isn't as elegant, so it's worth using LINQ when programming with monadic types. This can be a little bit of a headfuk when you first start this style of coding, so there's a couple of wiki pages that should help: One of the key aspects is try to never call
If you look at thew Here's your public class ChainTest
{
IAccessClient _accessClient;
public Aff<InputData> Delete(Guid clientId, InputData inputData) =>
from _1 in guard(inputData.IsValid(), Error.New("Input data isn't valid"))
from _2 in LogSomething("upfront logging")
from _3 in _accessClient.DeleteClient(clientId)
from _4 in LogSomething("additiona logging")
select inputData;
Aff<Unit> LogSomething(string msg) =>
Aff<Unit>(() => {
// Do your logging here
return default;
});
}Hopefully it should be clearer now why they're so powerful, they get rid of soooo much boilerplate.
|
Uh oh!
There was an error while loading. Please reload this page.
I have difficulty in getting method calls chained together that use
Affand can fail. Let's say I have an interface with a method that calls a Rest API and returnsAff<Unit>. Now I want to write another method that uses that interface method and embeds it in some error handling / logging. So basically, I have the following code (does not compile):In essence, I want to call
ChainTest.Delete().Run(). It's probably a simple scenario but I don't get it to compile. Could someone please enlighten me how to chain these two calls? Any help is very much appreciated.Regards,
Dirk.
All reactions