Abstract type: Difference between revisions
→{{header|PascalABC.NET}}
(5 intermediate revisions by 5 users not shown) | |||
Line 18:
You can declare a virtual function to not have an implementation by using <code>F.virtual.abstract</code> keyword. A type containing at least one abstract virtual function cannot be instantiated.
<syntaxhighlight lang="11l">T AbstractQueue
F.virtual.abstract enqueue(Int item) ->
T PrintQueue(AbstractQueue)
F.virtual.assign enqueue(Int item) ->
print(item)</syntaxhighlight>
Line 1,012:
=={{header|EMal}}==
{{trans|Go}}
<syntaxhighlight lang="
^|EMal does not support abstract types with partial implementations,
|but can use interfaces.
Line 2,554:
See [[#Delphi | Delphi]]
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
type
MyAbstract = abstract class
public
procedure Proc1; abstract;
procedure Proc2;
begin
end;
end;
MyClass = class(MyAbstract)
public
procedure Proc1; override;
begin
end;
end;
begin
var a := new MyClass;
a.Proc1;
end.
</syntaxhighlight>
Abstract method is a virtual method. You must overload it in a subclass using an override modifier.
It is also possible to define an interface and class, implementing this interface:
<syntaxhighlight lang="delphi">
type
IMyInterface = interface
procedure Proc1;
procedure Proc2;
end;
MyClass = class(IMyInterface)
public
procedure Proc1;
begin
Print(1);
end;
procedure Proc2;
begin
Print(2);
end;
end;
begin
var a: IMyInterface := new MyClass;
a.Proc1;
a.Proc2;
end.
</syntaxhighlight>
=={{header|Perl}}==
Line 3,267 ⟶ 3,320:
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,422 ⟶ 3,516:
The Go example, when rewritten in Wren, looks like this.
<syntaxhighlight lang="
class Beast{
|