Overriding Constructors in F#

前端 未结 2 1740
無奈伤痛
無奈伤痛 2020-12-18 08:58

How would I write the following C# code in F#?

namespace Shared {
    public class SharedRegistry : PageRegistry {
        public SharedRegistry(bool useCach         


        
2条回答
  •  孤城傲影
    2020-12-18 09:15

    I'm not sure that I understand your question; what you've written above looks like it ought to work fine. If you're asking where to put the rest of the constructor logic, try this:

    type SharedRegistry(useCache) as this =
      inherit PageRegistry(useCache)
      do
        this.ForRequestedType().TheDefaultIsConcreteType()
        // etc.
      new() = SharedRegistry(true)
    

    If you want to define each constructor individually, you can do that too:

    type SharedRegistry =
      inherit PageRegistry
      new(useCache) as this = 
        { inherit PageRegistry(useCache) } then
        this.ForRequestedType().TheDefaultIsConcreteType()
        // etc.
      new() = SharedRegistry(true)
    

    Or, you could use an optional argument to your main constructor:

    type SharedRegistry(?useCache) as this =
      inherit PageRegistry(defaultArg useCache true)
      do
        this.ForRequestedType().TheDefaultIsConcreteType()
        // etc.
    

提交回复
热议问题