F# 中的重写构造函数
如何在 F# 中编写以下 C# 代码?
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>();
}
}
}
据我所知,但我无法在声明我自己的默认构造函数的同时继承 PageRegistry
。
type SharedRegistry(useCache: bool) =
inherit PageRegistry(useCache)
new() = new SharedRegistry(true)
富有的
How would 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 is as far as I have managed, but I can't work out to 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定我是否理解你的问题;你上面写的看起来应该可以正常工作。如果您询问将构造函数的其余逻辑放在哪里,请尝试以下操作:
如果您想单独定义每个构造函数,您也可以这样做:
或者,您可以对主构造函数使用可选参数:
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:
If you want to define each constructor individually, you can do that too:
Or, you could use an optional argument to your main constructor:
您的 C# 类使用带有默认值的参数,这与重载构造函数略有不同。无论如何,F# 支持重载构造函数和默认参数。
使用参数的默认值,代码将如下所示:
现在您可以按如下方式创建实例:
我相信在 F# 中使用命名参数稍微优雅一些。它的工作方式是参数
useCache
在幕后变成option
(如果您想使用 C# 中的类,这可能是一个问题) strong>重载构造函数 - 您的 F# 代码应该是正确的(请参阅 kvb 的答案)。一般来说,最好至少有一个隐式构造函数(因为它允许您自动访问类体内的构造函数参数,使用
let
声明字段并使用实现构造函数逻辑做
)。隐式构造函数应该是接受所有参数的构造函数。在某些情况下,您可能希望将其设为私有,可以这样做:Your C# class uses parameters with default value which is slightly different than overloaded constructors. In any case, F# supports both overloaded constructors and default parameters.
Using default values of a parameters, the code would look like this:
Now you can create an instance as follows:
I belive that using named parameters is slightly more elegant in F#. The way it works is that the parameter
useCache
becomes anoption<bool>
under the cover (This may be a problem if you wanted to use the class from C#)Regarding overloaded constructors - Your F# code should be correct (see the answer from kvb). In general, it is probably better to have at least one implicit constructor (as that allows you to access constructor parameters inside the body of the class automatically, declare fields using
let
and implement constructor logic usingdo
). The implicit constructor should be the one that takes all the parameters. In some cases, you may want to make it private, which can be done like this: