Differences between version 8 and predecessor to the previous major change of PolymorphicTypes.
Other diffs: Previous Revision, Previous Author, or view the Annotated Edit History
Newer page: | version 8 | Last edited on Monday, June 6, 2005 1:28:50 am | by DavidHallett | Revert |
Older page: | version 3 | Last edited on Tuesday, April 8, 2003 8:47:24 am | by SamJansen | Revert |
@@ -1,7 +1,7 @@
-ProgrammingLanguages
with __polymorphic types__
allow you to write great range of generic functions, yet preserve StrongTypeChecking. With polymorphic typing you don't have to specify the exact type of everything in your program, you may use blank ''type variables''.
+[ProgrammingLanguage]s
with PolymorphicTypes
allow you to write great range of generic functions, yet preserve StrongTypeChecking. With polymorphic typing you don't have to specify the exact type of everything in your program, you may use blank ''type variables''.
-This function type in Ocaml
is not polymorhic:
+This function type in [OCaml]
is not polymorhic:
__val__ zip_ints : int list -> int list -> (int * int) list
That says __zip_ints__ will take two lists of integers and return a list of pairs of integers. It's useful, but only for one thing. This function in Ocaml is polymorhic:
@@ -16,23 +16,44 @@
[ML] and [Haskell] have polymorphic typing.
In [C] you can approximate polymophic typing with __void__ pointer arguments, which will take pointers to any type. In [Java] and [C++] approximate polymorphic typing by using Object classes, which all objects belong to. But in both cases you lose strong type checking, so the quality of your code suffers.
-''It should be noted here that this page is talking about what is sometimes known as __parametric polymorphism__. [C++] has templates, which satisfy the definition above, but are known to be syntactic; each instantiation creates a specialised version of the code. In a "true polymorphic system" only one version of the generated code would be used. There are other ways to have polymorphic types, they often come up in ObjectOriented ProgrammingLanguages
. Oh, and parametric polymorphism is also often referred to as generics. -- SamJansen''
+
+
''It should be noted here that this page is talking about what is sometimes known as __parametric polymorphism__. [C++] has templates, which satisfy the definition above, but are known to be syntactic; each instantiation creates a specialised version of the code. In a "true polymorphic system" only one version of the generated code would be used. There are other ways to have polymorphic types, they often come up in ObjectOrientation
. Oh, and parametric polymorphism is also often referred to as generics. -- SamJansen''
+
+''Beginning with version 1.5 of the [Java] SDK, [Java] now also supports the concept of generics. -- DavidHallett''
+
+!!!Java Syntax
+__Paramaterized Type__
+ Vector<String> stringVector = new Vector<String>
+ List<Integer> integerList = new List<Integer>
+
+__Interface__
+ interface List<Element> implements !MyInterface{...}
+
+__Class__
+ class !MyList<Element> {...}
+ class !MyList<Element> implements List<Element> {...}
+
+__Method__
+ boolean containsBoth(Element a, Element b);
+ static <Element> boolean swap(List<Element> list, int i, int j);
+
+[Java Generics on developer.java.sun.com | http://developer.java.sun.com/developer/technicalArticles/releases/generics/]
!!!An Example
-This is Glyn defining a binary tree type on his [Ocaml
] interpreter:
+This is Glyn defining a binary tree type on his [OCaml
] interpreter:
# __type__ 'a tree = Leaf __of__ 'a | Node __of__ 'a tree * 'a tree ;;
''type 'a tree = Leaf of 'a | Node of 'a tree * 'a tree''
-(That's Glyn typing at the Ocaml
interpreter prompt and Ocaml
responding.) The type __tree__ is polymorphic: __'a__ is a place holder for any type. For example you might want a __string tree__:
+(That's Glyn typing at the [OCaml]
interpreter prompt and [OCaml]
responding.) The type __tree__ is polymorphic: __'a__ is a place holder for any type. For example you might want a __string tree__:
# __let__ my_tree = Node (Node (Leaf "Cow", Leaf "Pig"), Leaf "Cat") ;;
''val my_tree : string tree = Node (Node (Leaf "Cow", Leaf "Pig"), Leaf "Cat")''
-The Ocaml
interpreter worked that was a string tree for itself. From his tree type he can make __int trees__, __char trees__, __string tree trees__, whatever. These would all be incompatible types, because __ints__, __chars__ and __strings__ are incompatible. However, functions he writes for __'a tree__s can be used on any type of tree:
+The [OCaml]
interpreter worked that was a string tree for itself. From his tree type he can make __int trees__, __char trees__, __string tree trees__, whatever. These would all be incompatible types, because __ints__, __chars__ and __strings__ are incompatible. However, functions he writes for __'a tree__s can be used on any type of tree:
# __let rec__ count_leaves (t : 'a tree) : int =
__match__ t __with__
| Leaf _ -> 1