Abstract type: Difference between revisions

m (Automated syntax highlighting fixup (second round - minor fixes))
Line 1,390:
int add(int a, int b);
}</syntaxhighlight>
 
=={{header|jq}}==
jq does not support abstract types but has a namespace-based module system which can be used
to support an "abstract type" approach of programming, as
illustrated here using an extension of the Beast/Cat/Dog example.
 
The following is tailored to the C implementation of jq but could also be adapted for the Go implementation.
<syntaxhighlight lang=jq>
def Beast::new($kind; $name): {
superclass: "Beast",
class: null,
$kind,
$name,
cry: "unspecified"
};
 
def Ape::new($kind; $name):
Beast::new($kind; $name)
| .class = "Ape"
| .cry = "Hoot";
 
def Cat::new($kind; $name):
Beast::new($kind; $name)
| .class = "Cat"
| .cry = "Meow";
 
def Dog::new($kind; $name):
Beast::new($kind; $name)
| .class = "Dog"
| .cry = "Woof";
 
 
def print:
def a($noun):
$noun
| if .[0:1] | test("[aeio]") then "an \(.)" else "a \(.)" end;
 
if .class == null
then "\(.name) is \(a(.kind)), which is an unknown type of \(.superclass)."
else "\(.name) is \(a(.kind)), a type of \(.class), and cries: \(.cry)."
end;
 
Beast::new("sasquatch"; "Bigfoot"),
Ape::new("chimpanzee"; "Nim Chimsky"),
Dog::new("labrador"; "Max"),
Cat::new("siamese"; "Sammy")
| print
</syntaxhighlight>
{{output}}
<pre>
Bigfoot is a sasquatch, which is an unknown type of Beast.
Nim Chimsky is a chimpanzee, a type of Ape, and cries: Hoot.
Max is a labrador, a type of Dog, and cries: Woof.
Sammy is a siamese, a type of Cat, and cries: Meow.
</pre>
 
=={{header|Julia}}==
2,442

edits