F#中没有构造函数的类 [英] Classes without constructor in F#

查看:97
本文介绍了F#中没有构造函数的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定为什么F#似乎允许在没有任何构造函数的情况下定义类.我的意思是,不可能实例化该类的对象.语言规范不应该将此视为非法行为吗?

I'm not sure why F# seems to allow the definition of a class without any constructors. I mean, it would be impossible to instantiate an object of the class. Shouldn't the language spec treat this as illegal behavior?

例如,我可以定义类

type myClass =
    class
        member this.x = 0
    end

myClass似乎具有类型

type myClass =
    member x: int

但是它无法实例化.

推荐答案

以我的经验,F#的面向对象功能有时可能不如C#所表达的优雅.以上问题可能是一个例子.另一个示例是自动实现的可变属性.

In my experience, the object-oriented features of F# can sometimes be less elegant than what C# enables you to express. The above question could be one example; another example is automatically implemented mutable properties.

大多数人(包括我)似乎都不在乎,因为我们很少使用这些功能. F#的面向对象功能主要是为了与其他.NET代码实现互操作而存在的,因此尽管它们很有用,但它们并不是该语言的重要部分.我的猜测是,没有人考虑实施该编译器检查,因为它不会提供太多价值.尝试使用myClass时,您会发现有些问题.

Most people (including me) seem not to care, because we rarely use those features. The object-oriented features of F# mainly exist in order to enable interoperation with other .NET code, so while they can be useful, they aren't the important parts of the language. My guess is that no one thought of implementing that compiler check because it wouldn't provide much value. As soon as you'd attempt to use myClass, you'd notice that something was wrong.

这篇关于F#中没有构造函数的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆