Abstract type: Difference between revisions

imported>Regattaguru
(7 intermediate revisions by 4 users not shown)
Line 643:
===Interface===
{{trans|F#}}
<syntaxhighlight lang="cobol"> INTERFACE-ID.IDENTIFICATION ShapeDIVISION.
INTERFACE-ID. Shape.
PROCEDURE DIVISION.
IDENTIFICATION DIVISION.
METHOD-ID. perimeter.
DATA DIVISION.
Line 654 ⟶ 656:
END METHOD perimeter.
IDENTIFICATION DIVISION.
METHOD-ID. shape-area.
DATA DIVISION.
Line 664 ⟶ 667:
 
IDENTIFICATION DIVISION.
CLASS-ID. Rectangle.
Line 671 ⟶ 675:
INTERFACE Shape.
IDENTIFICATION DIVISION.
OBJECT IMPLEMENTS Shape.
DATA DIVISION.
Line 679 ⟶ 684:
PROCEDURE DIVISION.
IDENTIFICATION DIVISION.
METHOD-ID. perimeter.
DATA DIVISION.
Line 684 ⟶ 690:
01 ret USAGE FLOAT-LONG.
PROCEDURE DIVISION RETURNING ret.
COMPUTE ret = width * 2.0 + height * 2.0
GOBACK ret = width * 2.0 + height * 2.0
.END-COMPUTE
GOBACK.
END METHOD perimeter.
IDENTIFICATION DIVISION.
METHOD-ID. shape-area.
DATA DIVISION.
Line 694 ⟶ 702:
01 ret USAGE FLOAT-LONG.
PROCEDURE DIVISION RETURNING ret.
COMPUTE ret = width * height
GOBACK ret = width * height
.END-COMPUTE
GOBACK.
END METHOD shape-area.
END OBJECT.
Line 1,003 ⟶ 1,013:
{{trans|Go}}
<syntaxhighlight lang="emal">
^|EMal doesn'tdoes not support abstract types with partial implementations,
|but can use interfaces.
|^
Line 3,173 ⟶ 3,183:
END TEXTHASHKEY;
</syntaxhighlight>
=={{header|Skew}}==
{{works with|skewc|0.9.19}}
 
In Skew, interfaces must be explicitly implemented with `::`.
 
<syntaxhighlight lang="skew">
@entry
def main {
var rgb = Rgb.new(0, 255, 255)
var hex = Hex.new("00ffff")
 
var color Color
color = hex
color.print
 
color = rgb
color.print
 
(hex as Color).print
}
 
interface Color {
def toString string
def print {
dynamic.console.log(self.toString)
}
}
 
class Rgb :: Color {
var r int, g int, b int
def toString string { return "rgb(\(r), \(g), \(b))" }
}
 
class Hex :: Color {
var code string
def toString string { return "#\(code)" }
}
</syntaxhighlight>
 
 
=={{header|Smalltalk}}==
A class is declared abtract by responding to the query <tt>isAbstract</tt> with true, and defining the required protocol for subclasses to raise an error notification. Optionally, instance creation can be blocked (but seldom done, as you will hit a subclassResponsibility anyway soon). Typically, the IDE provides menu functions to generate these definitions automatically (eg. "Insert Abstract Class" in the refactoring submenu of the class browser):
Line 3,193 ⟶ 3,243:
 
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.
Line 3,217 ⟶ 3,267:
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|Swift}}==
Swift uses Protocols to provide abstract type features. See [https://docs.swift.org/swift-book/documentation/the-swift-programming-language/protocols/ the docs]
 
A trivial example showing required properties and methods, and the means of providing a default implementation.
<syntaxhighlight lang="sml">
protocol Pet {
var name: String { get set }
var favouriteToy: String { get set }
 
func feed() -> Bool
 
func stroke() -> Void
 
}
 
extension Pet {
// Default implementation must be in an extension, not in the declaration above
 
func stroke() {
print("default purr")
}
}
 
struct Dog: Pet {
var name: String
var favouriteToy: String
// Required implementation
func feed() -> Bool {
print("more please")
return false
}
// If this were not implemented, the default from the extension above
// would be called.
func stroke() {
print("roll over")
}
}
 
</syntaxhighlight>
 
=={{header|Tcl}}==
Line 3,372 ⟶ 3,463:
 
The Go example, when rewritten in Wren, looks like this.
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
class Beast{
Anonymous user