9.4.2.2 up and Down transformations in F # (Upcasts and Downcasts)

Source: Internet
Author: User

9.4.2.2 up and down conversions in F # (Upcastsand downcasts)

If the conversion between types does not fail, it is called an upward conversion (upcast). As we have seen, this is the case when converting a type to an interface implemented by that type, and another example is converting a derived class to its base class, in which case the compiler can guarantee that the operation is correct and will not fail.

If you have a value of a base type that you want to convert to an inheriting class, the operation may fail because the value of the base class may be the value of the target class or it may not be. In this case, we must use the second type of conversion, called a Down conversion (downcast). Let's illustrate with an example that we will use the standard Random class, which is derived from the Object class (just like any other. NET Class):

> Open System;;

> Let rnd = new Random ();

Val Rnd:random

> Let Rndobject = (rnd: >object);; <--successful, operation will not fail

Val Obj:object

> Let Rnd2 = (rndobject: >random);; |

StdIn (4,12): Error:type constraintmismatch. | Incorrect up conversion

The type ' Object ' is not compatible withthe type ' Random ' |

> Let Rnd2 = (rndobject:? >random);; <--success, but may fail

Val Rnd2:random

> (rndobject:?> String);

System.InvalidCastException:Unable to Castobject of type

' System.Random ' to type ' System.String '.

It can be found that the F # compiler reports This error if we accidentally try to inappropriately use an upward conversion. The error message says that object is incompatible with random, which means that the compiler cannot guarantee that a value of type object can be converted to the random type. Finally, the manifest shows that the down conversion may fail and throws an exception if you try to convert the object to the wrong inheriting class.

To remember the syntax for F # up-conversion (>) and down-conversion (:?>), there is a good way to use a down conversion when there are some uncertainties because the operation might fail. The uncertainty is that the down conversion operator contains a question mark, and the upward conversion does not contain the reason. The F # language also provides an operator equivalent to IS in C #, returning a Boolean value that indicates whether an object instance can be converted to the specified type. In order to test if obj can be converted to string, it is written as obj:?> string.

Here, it is worthwhile to think about the difference between F # and C #. In C #, we don't even need a type conversion, as in Listing 9.17: When the compiler knows that the conversion succeeds, it does not need disambiguation and can make it happen implicitly. F # does not make any implicit conversions, so it makes sense to use a language structure to represent transformations and ensure success. There is no point in using it in C #, so it is seldom used, because the two transformations use the same syntax and are simpler. Explicitly specifying the programming style of the transformation makes type inference possible, and often helps clarify what the code is actually doing.

Use only one chapter (reasonable size!) ), it is impossible to explain all of F # Object-oriented features, but we already know that to turn a functional application into the actual. NET code, those are the most important.

As we have said many times, these changes make F # code easier to access in C #, so now is the time to prove it, and to show how interoperability works together.

9.4.2.2 up and Down transformations in F # (Upcasts and Downcasts)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.