F # and inheritance modeling

my question is about how to work with inheritance in a functional way in F #. To describe this a bit, I will give a simple example. Suppose we want to model a world made up of different kinds of animals. Each animal species has some attributes with other species (e.g. name, size, etc.). In addition, each species may have others that are not shared by others (for example, the number of children is related to dogs and cats, but not for spiders, for example). In addition, there may be methods or functions associated with each animal species that may or may not be the same for the two animal species, that is, there is a default implementation that may be excessive for a particular species. Each animal species may have a method (s) that is defined only for that kind.

Now, in the OOP world, this is likely to lead to an abstract class having common attributes and abstract methods, followed by classes obtained for each animal species. I am not sure how to functionally define a domain model in F #. Here: What to use, an abstract class or interface in F #? states that "the idiomatic code of F # uses different extensibility points than C # (for example, using a function / interface as an argument), so you really don't need abstract classes."

If this is the way that should be adopted, is it possible to provide some basic example of this?

As for my further reflection on this, I believe that attributes should be encapsulated in some kind of structure. Then most of the idiomatic structure in this regard in F # is a notation. Does this mean that there should be a parent record, which should be included in other records corresponding to specific "children", i.e. Compositions instead of inheritance. This, however, seems to me a workaround that is neither idiomatic nor particularly elegant.

I think the preferred way to model inheritance in F # is through discriminatory unions. However, this does not solve the problem of common attributes and methods.

Models similar to those mentioned above are quite common, in my opinion, since they are implicitly included in most enterprise applications. So, is there any suggestion on how this can be handled in a functional way (for example, the method suggested by the link above or any other suggestion)? Or will we say that this is not a domain that can be easily modeled using a functional approach?

Thanks.

+7
source share
1 answer

Your description of the example already implies an object-oriented programming model. You describe the problem in object-oriented terms, such as redefinition and methods specific to certain types of animals. This makes it difficult to provide you with a reasonable alternative functional representation because your question already assumes object-oriented concepts in the answer.

Firstly, it is perfectly normal to use object-oriented constructs in F # when they make sense (the example in your question is just a toy example, so it may or may not be). The basic idea is that F # prefers composition over inheritance and therefore you will probably try to avoid inheritance (which can lead to complex hierarchies of objects) and instead make animals from different directions.

Secondly, if you explained your problem differently, then there can be an absolutely wonderful functional presentation using discriminated associations. For example:

type MammalKind = Cat | Dog type MammalInfo = { Legs : int; Children : int } type Animal = | Mammal of MammalKind * MammalInfo | Spider of (...) 

The idea is to structure the type so that different types of animals (which you can handle differently) have their own type. For example, you can write a function that takes MammalInfo and performs some calculations that make sense only for dogs and cats (but not for spiders). But, as I mentioned earlier, your description of the problem is inherently object-oriented, so it would be difficult to understand how this will be done.

+9
source

All Articles