Conversion Method polymorphism and duck typing; C # Poor and F # elegant

Source: Internet
Author: User

In: http://blog.csdn.net/hikaliv/article/details/4559927

I like functional programming, and I like to think deeply about object-oriented technology. I appreciate this article.

 

Method polymorphism and type Polymorphism

Those who know OOP are familiar with type polymorphism. In other words, the polymorphism of Type polymorphism is reflected in the method. Why is the method polymorphism? Type polymorphism refers to the object type, and its method is subject to the object constraints. Method polymorphism is not restricted by the image type. The specific differences are as follows:

  1. Method polymorphism does not require inheritance; Type polymorphism must implement interface or class inheritance.
  2. Method polymorphism no matter who you are, you only care what you can do. Type polymorphism stresses who you are from.
  3. For method polymorphism, only the part of the method that is actually called during the runtime is needed. For type polymorphism, A is-a B is required for image compatibility, that is, no need to use it, as long as you worship the master, you have to implement it all.

duck typing method polymorphism

duck typing is a Dynamic Language NK "> Program Design Style, used to practice method polymorphism. Concept author James Whitcomb Riley said:

when you see a bird walking like a duck, swimming like a duck and screaming like a duck, the bird can be treated as a duck.

Duck typing does not focus on the object type, but on its performance. Let's let a pair of objects go to "take two steps", "swim two times", and "call two sounds". If the object cannot complete the specified action, a runtime exception will be thrown --It is checked during runtime.. Duck typing does not have any static checks, such as type checks, attribute checks, and method signature checks. Guido van rosum believes that the security of duck typing depends on good documentation and clarity.CodeAnd complete testing. For examplePythonLanguage example:

Figure 1

The code is concise. Both ducks and humans use quack, so they can pass in the checkasduck method for processing. Checkasduck can be called by a duck, no matter what it is. This approach is unreasonable in OOP.

duck typing serious problems

duck typing demonstrates the idea of Dynamic Language Programming, different from the type check emphasized by static languages, dynamic languages assume that you will perform an action, and you may find that you cannot do it during the runtime; before running the static language, you must determine whether you can do it. Duck typing is more like a protocol that represents openness and freedom-we welcome you to participate and hope you can abide by each other's agreement. If you violate or fail to complete the specified arrangement, the cost is an exception during running. The static type check is more like a kind of constraint, and the door is shelved -- want to come in? You are not good enough to look at yourself first!

The cost of openness and freedom is the operational risk. A lame duck and a dinosaur mistaken as a duck will cause program crash, as shown below:PythonExample:

Figure 2

The duck is not grown into a duck. The Sb method has not been implemented, soTry-try tAt the same time, the dinosaur successfully Imitated All the behavior of the duck and was mistakenly treated as a duck for release, causing program crash.PythonThe exception handling mechanism is simply a "magino line of defense", which has benefited us. You can't blame the dinosaurs for deliberate disruption. The Sb method is defined elsewhere, but its fck method is defined for you. you mistakenly think of it as a duck, dinosaurs are very wronged. This trap can only be avoided by humans, and dynamic languages cannot be prevented by themselves.

C # Thin, incompetent, rogue, and abnormal

image python and other dynamic languages are not ideal for" spreading ducks ". In static languages, strong type constraints are typical in C. For example, how to write the checkasduck method in C? From the perspective of OOP, the person class and duck Class " relationship "...... Both can be derived from the quackanimal class of quack, or the iquack interface with the quack method can be implemented. The design of the base class derived from the class is not considered. The light single inheritance cannot be considered; the design of the implementation interface seems to be light and bloated, as shown in the following example:

Human beings are superior to ducks and cats, requiring both callfck and callsck. Therefore, interfaces are contaminated.

Why C # is used as an example? C # is too typical in the OOP family.C # is the pure father of chunge in OOP!C #.Pure OOP ConstitutionIt's just a shame to be honest with your dynamic language. Rush failed. We haveReflection (reflection)After the technology is done, I will try again.Dynamic), 6:

C # looks plump, but it is actually fat. C # behind the scenes, the barriers are strict. C # High Inter-individual interaction costs. Reflection is the most expensive. The dynamic in C #4.0 is essentially a sugar coating for the reflection. Dynamic semantics is incompatible with OOP philosophy. Looking at it, C # Is Not lightweight. CPs of functional programming at the end of this article, we also mentioned that C #3.0 only needs its tables for introducing lambda. Today, the dynamic semantics of rape is powerful, and it is a bit of an incompetent taste of its OOP constitution.

do C ++ require no reflection? C ++ has a pointer! As the saying goes: " pointers can penetrate everything !" C ++'s spear exploits C #'s shield. Therefore, C # And pointer are not used for days. What about unsafe ? Sorry! C # Ah, chunge, which is so cool ......

Can we avoid the polymorphism of reflection implementation methods? You cannot.Where is the crux of OOP constitution? The crux of the issue lies in the OOP language, and functions have no position at all.In C #, the only thing that has a position is the image. There are no methods beyond the object, and independent methods also need to be encapsulated by the delegate object. On the one hand, the strong type constraints of C # are elevated at the image level, and it is impossible to directly operate its internal methods through the image; on the other hand, C # Using Reflection, such as dynamic language, usually throws the method check to the runtime, which is called playing rogue. Here's a summary: You can get rid of abuse and exile me. Essentially, this is a face or a transgender between a strong type and an untyped one. C # It is really the father of the OOP family.

F # elegant implementation method polymorphism

Dynamic Language indulgence, OOP language incompetence, among which C # language is abnormal. We just want to find a language, and the implementation of method polymorphism is not suffering, but also ensures security.Functions are also of the type.The data type is determined by the parameter table and return type. Therefore, we hope to perform a static type check for the function directly. In functional languages, functions are prominent. NowF #Come forward and implement for us:

Figure 7

18 rows in F #InlineAnd 19 rows^Directly checks the static type of the member functions of the image on the duck parameter. Row 23 cat cannot pass the check because its fck member function type is unqualified. A 24-row dog cannot pass the check because it does not have a fck member function.

As for how the inline and ^ operators in F # are combined to complete the operation, this article does not discuss the scope. I want to know more about F, see F # manual " and " F # static parsing variable type .

Related Article

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.