Problem determining how to order F# types due to circular references

≡放荡痞女 提交于 2019-11-30 11:43:42

This example is very far from what I'm accustomed to in functional programming. But for the problem of ordering mutually recursive types, there is a standard solution: use type parameters and make two-level types. I'll give a simple example in OCaml, a related language. I don't know how to translate the simple example into the scary type functions you are using.

Here's what doesn't work:

type misc = State of string
          | City  of city

type city = { zipcode : int; location : misc }

Here's how you fix it with two-level types:

type 'm city' = { zipcode : int; location : 'm }

type misc = State of string
          | City of misc city'
type city = misc city'

This example is OCaml, but maybe you can generalized to F#. Hope this helps.

In F#, it is possible to define mutually recursive types, that is, you can define the two types that need to reference each other together and they will see each other. The syntax for writing this is:

type CityDAO() = 
  inherit CommonDAO<CityType>(...)
  // we can use DAOMisc here

and DAOMisc = 
  member internal self.FindIdByType item =  
    // we can use CityDAO here

The limitation of this syntax is that both of the types need to be declared in a single file, so you cannot use the typical C# organization 1 type per 1 file.

As Norman points out, this isn't a typical functional design, so if you designed the whole data access layer in a more functional way, you could probably avoid this problem. However, I'd say that there is nothing wrong with combining functional and object-oriented style in F#, so using mutually recursive types may be the only option.

You can probably write the code more nicely if you first define interfaces for the two types - these may or may not need to be mutually recursive (depending on whether one is used in the public interface of the other):

type ICityDAO = 
  abstract Foo : // ...

type IDAOMisc = 
  abstract Foo : // ...

This has the following benefits:

  • Defining all mutually recursive interfaces in a single file doesn't make the code less readable
  • You can later refer to the interfaces, so no other types need to be mutually recursive
  • As a side-effect, you'll have more extensible code (thanks to interfaces)

F# directly supports mutually recursive types. Consider the following chicken/egg type definition:

type Chicken =
   | Eggs of Egg list
and Egg =
   | Chickens of Chicken list

The point is that mutually recursive types are declared together using the 'and' operator (as opposed to two separate types)

How about eliminating DAOMisc.FindIdByType, and replacing it with a FindId within each DAO class? FindId would only know how to find its own type. This would eliminate the need for the base class and dynamic type test, and the circular dependency between DAOMisc and all of the other DAO classes. DAO types can depend on one-another, so CityDAO can call StateDAO.FindId. (DAO types could depend on one another mutually, if need be.)

Is this what you were talking about when you said, "The simple approach would be to put this function in each DAO... But, this just strikes me as wrong..."? I'm not sure because you said that the function would refer only to the types that come before it. The idea that I am presenting here is that each FindId function knows only its own type.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!