Anders Hejlsberg talks about generics in C #, Java and C
[Translation] lover_p 2004-03-25 ------------------------------------------------------------------------------------------------------------------------------------------------ --------------------------------------- Original: Bill Venners, Bruce Eckel 2004.2.26 : Http://www.artima.com/intv/Generics.html Translation: lover_p
Character introduction]
Anders Hejlsberg, Microsoft famous engineers, leading his group to design C # (read: c-sharp) programming language. Hejlsberg's first mounting the software industry history stage was in the early 1980s because he designed a Pascal compiler for MS-DOS and CP / M. At that time, a small company's Borland quickly hired him, and bought his compiler and renamed Turbo Pascal. In Borland, Hejlsberg continued to develop Turbo Pascal and finally led his group to design Turbo Pascal alternatives Elphi. In 1996, after 13 years of entering Borland, Hejlsberg joined Microsoft. Initially, he did the architect of Visual J and Windows Fundatioin Classes (WFC). Subsequently, Hejlsberg became the key participants of C #'s CEO and .NET Framework. Currently, Anders Hejlsberg is also leading the continued development of C # programming languages.
Bruce Eckel, Think IN C (C Programming Thoughts) and the author of Think in Java (Java Programming Thoughts).
Editor - in-chief of Bill Venners, Artima.com.
[content]
Pan-type Overview of the generic C # generic and Java generic comparison C # generics and C templates constraints in C # generics
Wild outline
Bruce Eckel: Can you make a quick introduction to generics?
Anders Hejlsberg: The generic is actually an ability to add type parameters to your type, also known as parameterized types or parameter polymorphism. The most famous example is the List collection class. A List is an easy-to-grow array. It has a sorting method that you can index it for it, and so on. Now, if there is no parameterized type, it is not very good regardless of the use of the array or using List. If you use an array, you can get a strong type, because you can declare an array of Customer types, but you lose your growth and those convenient methods; if you use a list, you can get all your convenience, but you lose Strong type. What is it difficult to say that a list is (type) List, it is just an Object's List [Demon "," What type of list "refers to what type is the element stored by the List. This will bring you trouble, because the type can only be checked when running, that is, the type check is not performed when compiling. Even if you have a hard to put a Customer into a list and try to get a string from it, the compiler will not be unhappy. You can't find it can't work before running. At the same time, when you put simple types [translation: pointing type], you must also pack them. It is because of these issues, you have to hover between the List and arrays, you often have to decide which one should be used.
The greatness of generics is that you can enjoy your cake now, because you can define a list
Bill Venners: How is the generic work in C #?
Anders Hejlsberg: In C # without generics, you can only write class list {...}; and in the generic C #, you can write Class List
In CLR (Common Language Runtime), when you compile List
Bruce Eckel: So it is instantiated at runtime.
Anders Hejlsberg: It is indeed instantiated at runtime. It produces a specific native code when needed. Field, when you say List
Bruce Eckel: Does this class be collected by the garbage collector at a certain time?
Anders Hejlsberg: Yes, this is an orthogonal issue. It will create a class in the assembly, which will always exist in the assembly. If you terminate the assembly, this class will disappear, like other classes.
Bruce Eckel: But if my program declares a list
Anders Hejlsberg: ... Then the system will not instantiate List
Bruce Eckel: So you only need to conversion.
Anders Hejlsberg: No, it is actually unnecessary. We can share the original mirroring, but they actually have independent VTABLE. What I want to point out is that we just try to make meaningful sharing on the code, but we are very clear, for efficiency, there are many codes that cannot be shared. Typical is the value type, you will care about whether it is Int. You must not want to pack them as Object. Putting the value type is a shared method, but it will be large.
Bill Venners: For reference types, different are just classes. List
Anders Hejlsberg: Yes. As the detail of implementation, they actually share the same native code.
Comparison of C # generics and Java generics
Bruce Eckel: How to compare the generics and generics in Java in C #?
Adners Hejlsberg: Java's generic is initially based on Martin Odersky and other people called a project called Pizza. After PIZZA is renamed GJ, then become JSR, and finally ended by Java language. This generic is capable of transporting in the original VM (Virtual Machine, virtual machine). That is, you don't have to modify your VM, but it will bring a lot of limitations. These restrictions will not appear soon, but soon you will say: "Well, this is a bit unfamiliar."
For example, using Java generics, I think you don't actually get any performers, because when you compile a Java generic class, the compiler will replace all type parameters to Object. Of course, if you try to build a list
Comparison of C # generics and C templates
Bruce Eckel: How to compare C # generics and C templates?
Anders Hejlsberg: I think the best understanding of C # generics and C templates is: C # generics more like class, but it has type parameters; C template is close to the macro, but it looks like class.
The biggest difference between the C # generic and C templates is the timing of the type check, and how to instantiate. First, C # is instantiated at runtime. When C is compiled, or may be instantiated when it is connected. Anyway, C is instantiated before the program is running. This is the first point. The second point is different, when you compile generic types, C # will perform a strong type check. For a non-constrained type parameters, such as List
C is opposite. In C , you can perform any operation you want to do on the type of type specified by the type parameter. But once you instantiate it, it may not work, you will get some sense of blurred error messages. For example, if you have a type of parameter t, and x and y are T type variables, then you execute x y, if you define an Operator for two T, otherwise you can only get some meaningless wrong information. Therefore, in a sense, the C template is actually unmatched, or a weak type. And C # generics is a strong type. Constraint in the C # generic
Bruce Eckel: How is the constraint work in a C # generic?
Anders Hejlsberg: In the C # generics, we can apply constraints for type parameters. Take our List
Bruce Eckel: Interest. In C , the constraint is implicit.
Anders Hejlsberg: Yes. We can also do this in C #. For example, we have a Dictionary
Using constraints, you can eliminate dynamic inspections in your code, while in compiling or loading. There will be a lot of things when you ask K to implement the IComparable interface. For the value of the K type, you can now access the interface method without the need for type conversion. Because the program can ensure that this interface is implemented in semantics. Whenever you try to build an instance of this type, the compiler checks if these types have implemented this interface, if not implemented, will give you a compilation error. If you use reflection, you will get an exception.
Bruce Eckel: You are a compiler and run (you will check)?
Anders Hejlsberg: The compiler will check it, but you still have to do this by reflection at runtime, so the system will check it. Just as I said earlier, anything that can be made when compiling can be done by reflection.
Bruce Eckel: I can make a function template, in other words, a function with a parameter that does not know the type? You add a strong type of check to the constraint, but can I get a weak type template like a C template? For example, can I write a function, with two parameters A and B b, and write A B in the code? Can I say that I don't care about whether A and B have Operator because they are weak types?
Anders Hejlsberg: You really want to ask questions should be what this is in the constraint? The constraint, like other characteristics, will eventually be any complicated. When you consider it, the constraint is just a pattern matching mechanism. You may want to be able to say that "this type of parameters must have a constructor with two parameters, implement Operator , with this static method, there are two instance methods, etc.". The problem is, do you want this mode matching mechanism? Never match anything to complete mode is a whole continuous body. There is no thing (pattern matching) too small, you can't explain the problem; and complete mode matches is too complicated, so we need to find a balance point in the middle. We allow you to specify constraints as a class, one or more interfaces, and some constructors constraints. For example, you can say: "This type must implement ifoo and IBAR" or "this type must inherit the base class X". Once you do this, you will check this constraint when compiling and running. Any method implied by this constraint is directly effective for the value specified by the type parameters.
Now, in C #, the operator is a static member. Therefore, the operator cannot be a member of the interface, so the interface constraint cannot bring you Operator . You can only get Operator through class constraints, you can say that this type of parameters must inherit, such as Number classes, and Number class has Operator for two Nubmerges. But you can't abstract "I must have an Operator ", we can't know the specific meaning of this sentence.
Bill Venners: You are constrained by the type instead of signing.
Anders Hejlsberg: Yes.
Bill Venners: So this type must expand a class or implement an interface.
Anders Hejlsberg: Yes. And we can still go further. In fact, we have thought about it again, but this will become quite complicated. And the increased complexity is not worth it compared to the result. If you want to do, you can use it in the constraint system, you can use a factory model. For example, you have a Martix
Bruce Eckel: And Calculator is also a parameterized type.
Anders Hejlsberg: Yes. This is like factory models, and there are many ways to do it, this may not be your favorite method, but doing anything to pay for a price.
Bruce eckel: Yes, I started thinking that C template is a weak type mechanism. And when you want to add a constraint, you have moved from the weak type to a strong type. But this will definitely bring more complexity. This is the price.
Anders Hejlsberg: About Type You can think it is a roulette. The higher the roulette, the less good days, but higher security. But you can turn this roulette to any way.