Overriding Constructors in F #

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

namespace Shared { public class SharedRegistry : PageRegistry { public SharedRegistry(bool useCache = true) : base(useCache) { // Repositories ForRequestedType<IAddressRepository>().TheDefaultIsConcreteType<SqlAddressRepository>(); ForRequestedType<ISharedEnquiryRepository>().TheDefaultIsConcreteType<SharedEnquiryRepository>(); // Services ForRequestedType<IAddressService>().TheDefaultIsConcreteType<AddressService>(); ForRequestedType<ISharedEnquiryService>().TheDefaultIsConcreteType<SharedEnquiryService>(); } } } 

As I succeeded, but I cannot inherit from PageRegistry at the same time as declaring my own default constructor.

 type SharedRegistry(useCache: bool) = inherit PageRegistry(useCache) new() = new SharedRegistry(true) 

Rich

+4
source share
2 answers

I'm not sure I understand your question; what you wrote above should seem to work fine. If you ask where to put the rest of the constructor logic, try the following:

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

If you want to define each constructor separately, you can also do this:

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

Or you can use an optional argument for your main constructor:

 type SharedRegistry(?useCache) as this = inherit PageRegistry(defaultArg useCache true) do this.ForRequestedType<IAddressRepository>().TheDefaultIsConcreteType<SqlAddressRepository>() // etc. 
+5
source

Your C # class uses parameters with a default value that is slightly different from overloaded constructors. In any case, F # supports both overloaded constructors and default parameters.

Using the default values โ€‹โ€‹of the parameters, the code will look like this:

 type SharedRegistry(?useCache: bool) = do // constructor logic inherit PageRegistry(defaultArg useCache true) 

Now you can create an instance as follows:

 let r1 = new SharedRegistry() // using the default value let r2 = new SharedRegistry(false) // specified explicitly let r3 = new SharedRegistry(useCache=false) // using named parameter 

I find using named parameters a little more elegant in F #. The way it works is that the useCache parameter becomes option<bool> under the cover (this can be a problem if you want to use a class from C #)

Relatively overloaded constructors . Your F # code must be correct (see answer from kvb). In the general case, it is probably better to have at least one implicit constructor (since this allows you to automatically access the constructor parameters inside the class body, declare fields with let and implement the constructor logic with do ). The implicit constructor must be one that accepts all parameters. In some cases, you can make it private, which can be done as follows:

 type SharedRegistry private (useCache: bool) = inherit PageRegistry(useCache) do // constructor logic new () = SharedRegistry(true) 
+4
source

Source: https://habr.com/ru/post/1305847/


All Articles