# How to convert base class inheritance approach into f# way

**URL:** <https://forums.fsharp.org/t/how-to-convert-base-class-inheritance-approach-into-f-way/1485>\
**Category:** General\
**Tags:** beginners\
**Created:** [October 16, 2020, 7:23am UTC](https://forums.fsharp.org/t/how-to-convert-base-class-inheritance-approach-into-f-way/1485 "2020-10-16T07:23:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RicoSaupe](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ricosaupe/32/1092_2.png) [@RicoSaupe](https://forums.fsharp.org/u/RicoSaupe)\
**Post date:** [October 16, 2020, 7:23am UTC](https://forums.fsharp.org/t/how-to-convert-base-class-inheritance-approach-into-f-way/1485/1 "2020-10-16T07:23:18Z")

</div>

Hi all

Can anyone please point me in the right direction ?  
I have something like this. That is some c# pseudo code, just to demonstrate the problem. It wont compile. A base class and some children classes. Some childs just inherit the base class, some change it.

My questions are now:

1. How does this translate to F#? Use classes, records, union types?

2. how can i get rid of the boolean and implement it in a better way =\> better domain modelling

F#

```
type Prop1 = Prop1 of int
type Prop3 = Prop3 of string

// how to implement the boolean
type containerbase =
        { Prop1 : Prop1 
          Prop2: DateTime option
          Prop3: Prop3 
        }

// how to implement the deviations from the containerbase???
type container =
        | containervariety1 of containerbase 
        | containervariety1 of containerbase 
        | containervariety1 of containerbase 

type containers = container list

```

In the end i will have a list of containers of any type end have to determine if the container is active or not active. Depending on the state there are different actions triggered.

I hope i got my question across and looking forward to some help for beginners. 🙂 Thanks.

If there is already an article about that topic please point me to it as well. I might have missed it.

---

<div class="post-metadata">

**Author:** ![Prash](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/prash/32/815_2.png) [@Prash](https://forums.fsharp.org/u/Prash)\
**Post date:** [October 16, 2020, 10:14am UTC](https://forums.fsharp.org/t/how-to-convert-base-class-inheritance-approach-into-f-way/1485/2 "2020-10-16T10:14:01Z")

</div>

Welcome to the forum 🙂

It doesn’t look like you’re trying to port some C# code but rather that you’re trying to understand the F# way of doing things.

Therefore my first recommendation is to not think of the model in terms of C# and then translate it to F#. Instead, forget anything you know about C# (and similar languages) for now.

Think of the model as data definitions where you can use both “AND” (records) and “OR” (unions), and inheritance and overrides are not available. We are only defining the shape of the data for now, not the implementation of any code. Your example is too abstract to explain this so I’ll make up another one:

> A customer has a name AND a contact method, which is either an address OR a phone number. An address has 2 lines and a postcode.

These type definitions model this domain:

```fsharp
type Address =
    { Line1 : string
      Line2 : string
      PostCode : string }

type ContactMethod =
    | PhoneNumber of string
    | Address of Address

type Customer =
    { Name : string
      ContactMethod : ContactMethod }

```

Now, what if some code needs to behave differently for the different contact methods? You can write a function which uses pattern matching on the union type:

```fsharp
let isContactMethodValid contactMethod =
    match contactMethod with
    | PhoneNumber phoneNumber -> isPhoneNumberValid phoneNumber
    | Address address -> isAddressValid address

```

In my opinion, these fundamental parts of F# are inherently simpler than the comparable parts of C#, so it’s easier to learn them from scratch rather than trying to translate your understanding of the problem from C#. Of course, understanding the relationship between the two ways of doing can still be valuable, especially if you are porting code between the languages. I hope that answers your question!

---

<div class="post-metadata">

**Author:** ![RicoSaupe](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ricosaupe/32/1092_2.png) [@RicoSaupe](https://forums.fsharp.org/u/RicoSaupe)\
**Post date:** [October 19, 2020, 9:27am UTC](https://forums.fsharp.org/t/how-to-convert-base-class-inheritance-approach-into-f-way/1485/3 "2020-10-19T09:27:33Z")

</div>

Thanks Prash

This did help me a lot.  
I ended up creating the types as described and added a member method for each type called Is active to calculate the state. The state is calculated differently for each type.

---

<div class="post-metadata">

**Author:** ![jackyjoy123](https://avatars.discourse-cdn.com/v4/letter/j/d6d6ee/32.png) [@jackyjoy123](https://forums.fsharp.org/u/jackyjoy123)\
**Post date:** [August 17, 2021, 10:26am UTC](https://forums.fsharp.org/t/how-to-convert-base-class-inheritance-approach-into-f-way/1485/4 "2021-08-17T10:26:57Z")

</div>

thanks for the awesome information.
