Abstract type: Difference between revisions
Content added Content deleted
m (→{{header|Wren}}: Minor tidy) |
imported>Regattaguru |
||
Line 3,267: | Line 3,267: | ||
Then say we have a structure ListQueue which implements queues as lists. If we write <tt>ListQueue :> QUEUE</tt> |
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. |
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}}== |
=={{header|Tcl}}== |