简体   繁体   English

F#中的类型问题

[英]Types issue in F#

In my ongoing adventure deep diving into f# I am understanding a lot of this powerful language but there are things that I still do not understand so clearly. 在我正在深入探索f#的过程中,我理解了很多这种强大的语言,但有些事情我仍然不太清楚。 One of the most important issues I need to master is types. 我需要掌握的最重要的问题之一是类型。

Well the book I am reading is very straight forward and introduces entities and main functionalities with a direct approach. 我正在阅读的这本书非常直接,并通过直接的方法介绍了实体和主要功能。 The first thing I could get start with is types. 我能开始的第一件事是类型。 It introduces the main types as list, option, tuples, and so on... It is clearly underlined that all these types are IMMUTABLE for many reasons regarding functional programming and data consistance in functional programing. 它将主要类型引入列表,选项,元组等等......显然,由于功能编程和功能编程中数据一致性的诸多原因,所有这些类型都是IMMUTABLE。 Well, no problems until now... 好吧,直到现在没问题......

But now I am getting started with Concrete Types... 但现在我开始使用混凝土类型......

Well... I have problems in managing with types like list, option, tuples, types created through new operator and concrete types created using type keyword (for abbreviations, concrete types...). 嗯...我在使用list,option,tuples,通过new运算符创建的类型以及使用type keyword创建的具体类型(对于缩写,具体类型......)进行管理时遇到问题。

So my question is: how can I efficently catalogue/distinguish all types of data in f#???? 所以我的问题是:如何在f#中有效地编目/区分所有类型的数据?

I can create a perfect separation among types in C#, VB.NET... FOr example in VB.NET there are value and reference types while in C# there are only references and also int, double are treated as objects (they are objects while in VB.NET a value type is not a object and there is a split in types for this reason). 我可以在C#,VB.NET中创建类型之间的完美分离......在VB.NET中有一些示例,它们有值和引用类型,而在C#中只有引用和int,double被视为对象(它们是对象而在VB.NET中,值类型不是对象,因此在类型中存在拆分)。 Well in F# I cannot create such differences among types in the language. 在F#中,我无法在语言中的类型之间创建这种差异。 Can you help me? 你能帮助我吗? I hope I was clear. 我希望我很清楚。

It's possible that you've misunderstood types in C# and VB.NET. 您可能在C#和VB.NET中误解了类型。 Types work almost the same across all .NET languages; 所有.NET语言的类型几乎相同; for instance, both VB.NET and C# have value types and reference types. 例如,VB.NET和C#都有值类型和引用类型。

Any type can be immutable if there's no way to change it from outside. 如果无法从外部更改它,任何类型都可以是不可变的。 For instance, these two pieces of code are equivalent: 例如,这两段代码是等价的:

// an immutable reference type in F#
type Person =
    {
        FirstName : string
        LastName : string
    }

// an immutable reference type in C#
public class Person
{
    public Person(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }

    public string FirstName { get; private set; }
    public string LastName { get; private set; }
}

Edit: There's nothing really special about the built-in types in F#, except for some special syntax in the compiler. 编辑:除了编译器中的一些特殊语法之外,F#中的内置类型没有什么特别之处。 You can make your own - here are examples of list and option : 你可以自己做 - 这里是listoption例子:

type List<'a> = Empty
              | Item of 'a * List<'a>

type Option<'a> = None
                | Some of 'a

It helps to take an F# assembly and load it into Reflector, alongside assemblies from C# and VB.NET; 它有助于采用F#程序集并将其加载到Reflector中,以及来自C#和VB.NET的程序集; you can see that the compiler produces standard .NET types from your F# type declarations. 您可以看到编译器从F#类型声明中生成标准.NET类型。

Update 更新

For a good reference of the different types in F#, scroll down to F# Types on the MSDN article . 有关F#中不同类型的良好参考,请向下滚动到MSDN文章中的 F#类型 I've found that the MSDN site really does have some of the best information about F# out there. 我发现MSDN网站确实有一些关于F#的最佳信息。 To summarize, you've got F#'s own flavor of collections (ie Map , Seq , etc), tuples (which are just a like cross product of several types), discriminated unions, and more. 总而言之,你有F#自己的集合风格(即MapSeq等), 元组 (它们只是几种类型的交叉产品),有区别的联合等等。

F# uses type inference , so you do not need to explicitly specify the type your member variables in a type/class. F#使用类型推断 ,因此您无需在类型/类中显式指定成员变量的类型。 This means, that in most scenarios you do not need to specify that variable is an int , double , or any reference type like you would in C#. 这意味着,在大多数情况下,您不需要像在C#中那样指定该变量是intdouble或任何引用类型。 The distinction between reference types and value types is hidden from you basically. 基本上隐藏了引用类型和值类型之间的区别。

Also, when you say: 另外,当你说:

VB.NET there are value and reference types while in C# there are only references VB.NET中有值和引用类型,而在C#中只有引用

This isn't exactly accurate, it's actually the same in VB.NET/C#, see Jon Skeet's article about reference vs. value types for a really clear explanation. 这不完全准确,它在VB.NET / C#中实际上是相同的,请参阅Jon Skeet关于参考与值类型的文章以获得非常明确的解释。

The challenge of hybrid functional/object-oriented languages like F# is that they must span both functional and object-oriented concepts. 像F#的混合功能/面向对象语言的挑战是,他们必须跨越功能 面向对象的概念。 Although this makes such languages very powerful, it also makes them large; 虽然这使得这些语言非常强大,但它也使它们变得庞大; larger than strictly functional languages or strictly OO languages. 大于严格的功能语言或严格的OO语言。 One place that is most evident is the type catalog. 一个最明显的地方是类型目录。

The official documention for F# types is here . F#类型的官方文档在这里 As you can see, there are no less than 17 different types of...types. 如您所见,有不少于17种不同类型的......类型。

One thing to keep in mind, though, is that any type that can be defined or used in F# must be representable as a .NET type (which implies that it can also be used by other .NET languages). 但要记住的一件事是,任何可以在F#中定义或使用的类型都必须可以表示为.NET类型(这意味着它也可以被其他.NET语言使用)。 An interesting example of that is Discriminated Union types (discriminated union types are similar to enumerations, except that each alternate value can have other values associated to it). 一个有趣的例子是Discriminated Union类型(区别联合类型与枚举类似,除了每个备用值可以具有与之关联的其他值)。 Other .NET languages do not have discriminated union types, so the F# compiler translates such types to object hierarchies. 其他.NET语言没有区别的联合类型,因此F#编译器将这些类型转换为对象层次结构。 Something gets lost in the translation, though: F# 'knows' all possible cases of the type at compile time, but C# and VB.NET must assume that the number of cases is open-ended. 但是在翻译中丢失了一些东西:F#'在编译时'知道'所有可能的类型的情况,但C#和VB.NET必须假设案例的数量是开放的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM