Abstract type: Difference between revisions
Content added Content deleted
(Added zkl) |
(Added Standard ML section) |
||
Line 1,832: | Line 1,832: | ||
When integrating with Java, traits without implementation appear as interfaces. |
When integrating with Java, traits without implementation appear as interfaces. |
||
=={{header|Standard ML}}== |
|||
Standard ML does not have any built-in support for object-oriented programming. |
|||
Instead it supports abstraction through a module system. Every module has a signature |
|||
describing the types and values that can be accessed from outside a module. |
|||
The act of giving a signature to a module is called ascription. There are two type of ascription: |
|||
Transparent (written <tt>:</tt) and opaque (written <tt>:></tt>). If a structure is ascribed transparently, |
|||
none of the types are abstract. If it is ascribed opaquely, all types are abstract by default, but can be specified |
|||
explicitly in the signature, in which case they are not abstract. |
|||
Here is an example signature for a queue data structure: |
|||
<lang sml>signature QUEUE = sig |
|||
type 'a queue |
|||
val empty : 'a queue |
|||
val enqueue : 'a -> 'a queue -> 'a queue |
|||
val dequeue : 'a queue -> ('a * 'a queue) option |
|||
end</lang> |
|||
Because we did not specify an implementation for <tt>'a queue</tt>, the type |
|||
will be abstract if we use opaque ascription. Instead we could create a version of the signature which specifies the type, |
|||
in which case it will never be abstract: |
|||
<lang sml>signature LIST_QUEUE = sig |
|||
type 'a queue = 'a list |
|||
val empty : 'a queue |
|||
val enqueue : 'a -> 'a queue -> 'a queue |
|||
val dequeue : 'a queue -> ('a * 'a queue) option |
|||
end</lang> |
|||
Then say we have a structure ListQueue which implements queues as lists. If we write <tt>ListQueue :> QUEUE</tt> |
|||
then the queue type will be abstract, but if we write <tt>ListQueue : QUEUE</tt> or <tt>ListQueue : LIST_QUEUE</tt> it won't. |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |