Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you need to define two types that are <em>mutually recursive</em> (meaning that they can both refer to each other), then you need to place them in a single file and use <code>type ... and ...</code> syntax.</p> <p>In your example, this means that <code>Genre</code> and <code>Album</code> need to be defined like this:</p> <pre><code>// Start a definition block using 'type' as normal type Genre() = let mutable name = String.Empty let mutable albums = [new Album()] member x.Name with get() = name and set (value) = name &lt;- value member x.Albums with get() = albums and set ( value ) = albums &lt;- value // Continue single type definition block using 'and' and Album() = let mutable genre = new Genre() let mutable albumId = 0 let mutable artist = new Artist() member x.Genre with get() = genre and set (value) = genre &lt;- value member x.AlbumId with get() = albumId and set ( value ) = albumId &lt;- value member x.Artist with get() = artist and set ( value ) = artist &lt;- value </code></pre> <p>However, your example is using F# in a very C#-style, so the code does not really look very elegant and it may not give you many of the benefits of functional programming. </p> <p>If I wanted to represent a structure that you're using, then I probably wouldn't add reference to the genre into the <code>Album</code> type. When you place a list of albums inside a <code>Genre</code>, you will always be able to recover the genre when you process the data structure (i.e. to turn it into some other structure, maybe an F# record, that can be passed to data-binding). The beneift of F# is that it lets you write the domain on a few lines, but that works only for functional types. </p> <p>Using discriminated unions with a single case, you can write:</p> <pre><code>// Type aliases to make code more readable type Name = string type AlbumID = int // Simple type definitions to represent the domain type Artist = Artist of Name type Album = Album of AlbumID * Artist type Genre = Genre of Name * Album list </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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