Note that there are some explanatory texts on larger screens.

plurals
  1. POModel inheritance using functional programming style data types
    primarykey
    data
    text
    <p>I've been using F# recently and tried to code in a functional way rather than doing OOP all over again in a different syntax. I've now run into a problem that I could solve with a mix of inheritance and discriminated unions but for which I'm trying to find a pure functional style representation.</p> <p>What I want to model is something like this (changed to preserve the pattern since I can't use the actual code):</p> <pre><code>type Shape = | Rectangle of Size * Size | Circle of Diameter </code></pre> <p>so far so good, but now I need to represent a collection of additional properties relevant for the different types of shapes, like:</p> <pre><code>type ShapeProperty = | Color of Shape * Color // Fine, valid for all shapes | Rotation of Shape * Angle // Wants to be Rotation of Rectangle * Angle | Details of Shape * int // Wants to be Detail of Circle * int </code></pre> <p>If instead of using a discriminated union for Shape, I'd used a base class and inheritance, I could refer to the actual types and make sure Rotation could only be applied to Rectangle and not Circle, but now I can't. Is there a way of implementing something like that while still keeping to pure functional data structures?</p> <p><strong>Edit:</strong></p> <p>My current solution is to separate the definition of a individual shapes from the fact that shapes are at all related, like this:</p> <pre><code>type Rectangle = Rectangle of Size * Size // Or using a record type type Circle = Circle of Diameter // Or using a record type type Shape = RectangleShape of Rectangle | CircleShape of Circle </code></pre> <p>which means that I then have type to refer to in ShapeProperty:</p> <pre><code>type ShapeProperty = | Color of Shape * Color | Rotation of Rectangle * Angle | Details of Circle * int </code></pre> <p>This feels a bit clumsy as now need to encapsulate each shape in the Shape type to store them in a collection, but it does give me a way of expressing the type safety I'm after. Any improvements on this would be welcome.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload