TAILIEUCHUNG - Foundations of F#.Net phần 6

Tham khảo tài liệu 'foundations of f#.net phần 6', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | 148 CHAPTER 7 THE F LIBRARIES The untyped- Functions The .NET Framework s BCL contains two versions of the lEnumerable interface one defined in and an older one defined in . All the samples shown so far have been designed to work with the new generic version from System. . However sometimes it might be necessary to work with collections that are not generic so the F lEnumerable module also provides a number of functions to work with nongeneric collections. These functions all start with the prefix untyped and then have the same name as their generic counterpart. The big disadvantage of using these functions is that they do not contain any type information therefore the compiler cannot type check them properly which can mean some code might throw an invalid cast exception at runtime. Before using these functions I strongly recommend that you see whether you can use the list comprehension syntax covered in Chapters 3 and 4 instead. This is because the list comprehension syntax can infer the types of many untyped collections usually by looking at the type of the Item indexer property so there is less need for type annotations which generally makes programming easier. The following example looks at an array list that stores a sequence of integers and then uses untyped_map to double each of the integers in the list light open let arrayList let temp new ArrayList 1 2 3 temp let doubledArrayList arrayList fun x - x 2 doubledArrayList fun x - printf i . x The results of this code when compiled and executed are as follows 2 . 4 . 6 . As you can see from the previous example when the programmer gets the types right using the untyped functions is pretty straightforward. However consider the following example that tries to perform the same operation on the list except this time it contains strings light open let stringArrayList .

Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.