Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Why I do not like the Go language style interface (ie Structural Typing)     - Linux platform host to prevent hacking skills (Linux)

- Linux system server network security management tips (Linux)

- How Bluetooth turned off by default in Ubuntu 14.04 (Linux)

- CentOS 7.0 Automatic installation CD-ROM production Comments (Linux)

- Spring Data JPA call a stored procedure examples (Programming)

- tcpdump Linux system security tools (Linux)

- Let OpenCV face detection score output codes (Programming)

- Using Linux stat command to view the files (Linux)

- ORA-00600 error solve one case (Database)

- Oracle 11g maintenance partitions (Seven) - Modifying Real Attributes of Partitions (Database)

- Creating and extracting archives 11 tar command examples in Linux (Linux)

- Red Flag Linux Desktop 6.0 hard disk installation guide (Programming)

- Java threads and thread pools (Programming)

- Python Dir find a folder several files (Programming)

- About Linux iptables firewall interview questions and answers (Linux)

- The correct way to open Xcode - Debugging (Programming)

- Hackers is how the invasion and control of Things devices? (Linux)

- Linux system package manager -RPM (Linux)

- How to create a remote (Linux)

- Oracle archive log summary (Database)

 
         
  Why I do not like the Go language style interface (ie Structural Typing)
     
  Add Date : 2018-11-21      
         
         
         
  The so-called Go-style interface language is not explicitly declared type T implements the interface I, as long as the open method of type T interface I fully meet the requirements, you can use an object of type T interface I need a place. Name This practice is called Structural Typing, some people also see it as a static Duck Typing. In addition to the Go interface, something like that there are such in Scala Traits like. Some people think this feature is very good, but I personally do not like this approach, it is here to talk about its shortcomings. Of course, this discussion with static languages ​​like dynamic languages, not simply rude the next "good" or "bad" conclusion.

Starting from scratch, then: What is the interface. In fact, simply, the interface is a protocol that specifies a group of members, for example, in the .NET ICollection interface:

interface ICollection {
    int Count {get;}
    object SyncRoot {get;}
    bool IsSynchronized {get;}
    void CopyTo (Array array, int index);
}
This is an agreement of all of it? Not so, in fact, the interface also provides every act of "features." Figuratively, Count this interface in addition to the need to return the number of elements in the collection, it also implies that it needs in the O (1) time to return to this requirement. Such a method can be used safely use interface ICollection Count property to get the size of the collection, in order to know the characteristics of the case choosing the right algorithm to write programs without worrying about performance problem, which can achieve a so-called "face interface programming. " Of course, this "feature" does not only refer to the "Performance" on, for example, also includes the Count "does not modify the content of the collection." This seemingly quite natural hide requirements, which are part of the ICollection agreement.

Thus, we can also explain some other issues, such as why in .NET List < T> is not called the ArrayList < T> (Of course, these are just my guess). My idea is that, since List < T> and IList < T> interface is the matching appears and like IList < T> of certain methods, such as indexing requires quick access to the elements, so use IList < T> interface methods You can safely use the index to access, and to meet this feature of the basic data structures and arrays hard to break away, so in the name of the array becomes a bit redundant.

If the List < T> renamed ArrayList < T>, it seems to imply that IList < T> can have other implementations, is it LinkedList < T> it? In fact, LinkedList < T> to do with IList < T> does not have any relationship, because of its features and List < T> too much difference, some of it is full of some AddFirst, InsertBefore method and so on. Of course, LinkedList < T> and List < T> are ICollection < T>, so we can safely use a small part of members, their behavior is characterized by clear.

One case in point is the opposite of Java. In the Java class library, ArrayList and LinkedList implement the List interface, they have a get method, passing an index, returns the element that position, but the two implementations of the former takes O (1) which is time-consuming O (N), both close to the big court. So good, I now want to implement a method, which requires from the beginning of the first element, returns every element P locations, we can for List programming interface it? If we rely on index access, the external accidentally passed LinkedList, the time complexity of the algorithm would expect from O (N / P) becomes O (N2 / P). If we choose to traverse the entire list, even if we can only get efficiency ArrayList O (N) of. Anyway, List interfaces is a joke Java class libraries, even Stack classes implement List, the designers really do not know how you think the year.

Simply put, if the interface does not guarantee behavioral characteristics, the "interface-oriented programming" does not make sense.

Go and formula language interface has a similar problem because Structural Typing just from the surface (member name, number and type of parameters, etc.) to understand an interface, not concerned with the meaning of the rules and interfaces, we are unable to check. Coursera is forgotten in the course of which mentioned one such example:

interface IPainter {
    void Draw ();
}

interface ICowBoy {
    void Draw ();
}
Draw in English while having a "painting" and "drew" meaning, so the painter (Painter) and Cowboys (Cow Boy) can have the Draw behavior, but two very different meanings. If we achieve a "Xiao Ming" type, he obviously just a painter, but we let him talk to other cowboy duel, which is to leave him to die thing. On the other hand, "Wang" can be both a "painter" is also a "cowboy", he will be two kinds of Draw in C # which we can put him to achieve:

class XiaoWang: IPainter, ICowBoy {
    void IPainter.Draw () {
        // Draw
    }

    void ICowBoy.Draw () {
        // Pull out a gun
    }
}
So I have been trade-offs do not understand Java standard. You say such a stressed object-oriented language designed to emphasize the interface emphasizes, further mandatory exception, show how to implement the interface does not support it?

This is what I prefer Java and C # are explicitly labeled abnormal reasons. Because the program is written by people, it will not exist as a class just because some members will be treated as some interfaces to use, all through the "design" rather than naturally occurring. Just as we will not because a person looks beautiful as a woman put it in Thailand, this year's makeup and PS technology horrible.

I have a suspicious mind again here: I guess some people would say that I see here is just sour grapes, because C # is not this feature so that it is not good. Really is not so early in the year I have not heard Structural Typing scientific name of this time to consider this issue. I wrote a helper methods, it can be of any type into some kind of interface, for example:

XiaoMing xm = new XiaoMing ();
ICowBoy cb = StructuralTyping.From (xm) .To < ICowBoy> ();
So, we would be very happy to only know how to paint Xiao Ming sent a duel. Its internal principle is very simple, just use Emit dynamically generate a wrapper class at runtime only. In addition, I also use Mono.Cecil analysis compiled assembly, and inspection From To generic parameters matches, this is equivalent to providing static type-checking compiler period. In addition, I support the covariance of the inverter, you can also make the interface method does not require the return value of the method is compatible with a return value (and even now can find its extension methods), this simple than by name and parameter types to determine more powerful.

With a variety of options, I was safely say that I like what. JavaScript callback code can only be written, so many people say that it is an advantage of JavaScript callbacks say how wonderful I deeply disagree - just could not resist began to enjoy nothing more thing ......

This article seems to Tucao a bit more? But this small article kinda cool.
     
         
         
         
  More:      
 
- String JavaScript type system (Programming)
- Atheros AR8161 / AR8162 network card driver problem solving in CentOS 6.4 (Linux)
- CentOS 6.5 opens the Xmanager Remote Desktop login (Linux)
- php security study (Linux)
- Smooth upgrade to OpenSSH 6.7 Procedure (Linux)
- Elaborate .NET Multithreading: Using Task (Programming)
- Category prevent DoS attacks against Linux (Linux)
- Use the Find command to help you find those files that need to be cleaned (Linux)
- Linux remote connectivity tools -OpenSSH (Linux)
- Oracle 11g through SCN do incremental backup repair standby library detailed process (Database)
- Linux kernel panic (because glibc result) Repair (Linux)
- Linux --- process tracking (Linux)
- Using Linux command line and execute PHP code (Programming)
- Ubuntu treated with cue file to ape and wav files automatically track points (Linux)
- Linux detection command (vmstat) (Linux)
- CentOS7 virtual machine creation failed Solution (Linux)
- Installation and operation GAMIT software under Linux operating system (Linux)
- How to install and configure in Ubuntu 14.10 'Weather Information Indicator' (Linux)
- Share Java-based multithreading file case (Programming)
- Linux (CentOS) SSH login without password authentication (Linux)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.