What is the functional-programming alternative to an interface?
https://softwareengineering.stackexchange.com/questions/189047
-
05-11-2019 - |
Pergunta
If I want to program in a "functional" style, with what would I replace an interface?
interface IFace
{
string Name { get; set; }
int Id { get; }
}
class Foo : IFace { ... }
Maybe a Tuple<>
?
Tuple<Func<string> /*get_Name*/, Action<String> /*set_Name*/, Func<int> /*get_Id*/> Foo;
The only reason I'm using an interface in the first place is because I want always want certain properties/methods available.
Edit: Some more detail about what I'm thinking/attempting.
Say, I've got a method which takes three functions:
static class Blarf
{
public static void DoSomething(Func<string> getName, Action<string> setName, Func<int> getId);
}
With an instance of Bar
I can use this method:
class Bar
{
public string GetName();
public void SetName(string value);
public int GetId();
}
...
var bar = new Bar();
Blarf.DoSomething(bar.GetName, bar.SetName, bar.GetId);
But that's a bit of a pain as I have to mention bar
three times in a single call. Plus, I don't really intend for callers to supply functions from different instances
Blarf.DoSomething(bar1.GetName, bar2.SetName, bar3.GetId); // NO!
In C#, an interface
is one way to deal with this; but that seems like a very object-oriented approach. I'm wondering if there's a more functional solution: 1) pass the group of functions together, and 2) ensure the functions are properly related to each other.
Nenhuma solução correta