Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Eiffel (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Genericity=== {{See also|Generic programming#Genericity in Eiffel}} A generic class is a class that varies by type (e.g. LIST [PHONE], a list of phone numbers; ACCOUNT [G->ACCOUNT_TYPE], allowing for ACCOUNT [SAVINGS] and ACCOUNT [CHECKING], etc.). Classes can be generic, to express that they are parameterized by types. Generic parameters appear in square brackets: <syntaxhighlight lang="eiffel"> class LIST [G] ... </syntaxhighlight> G is known as a "formal generic parameter". (Eiffel reserves "argument" for routines, and uses "parameter" only for generic classes.) With such a declaration G represents within the class an arbitrary type; so a function can return a value of type G, and a routine can take an argument of that type: <syntaxhighlight lang="eiffel"> item: G do ... end put (x: G) do ... end </syntaxhighlight> The <code>LIST [INTEGER]</code> and <code>LIST [WORD]</code> are "generic derivations" of this class. Permitted combinations (with <code>n: INTEGER</code>, <code>w: WORD</code>, <code>il: LIST [INTEGER]</code>, <code>wl: LIST [WORD]</code>) are: <syntaxhighlight lang="eiffel"> n := il.item wl.put (w) </syntaxhighlight> <code>INTEGER</code> and <code>WORD</code> are the "actual generic parameters" in these generic derivations. It is also possible to have 'constrained' formal parameters, for which the actual parameter must inherit from a given class, the "constraint". For example, in <syntaxhighlight lang="eiffel"> class HASH_TABLE [G, KEY -> HASHABLE] </syntaxhighlight> a derivation <code>HASH_TABLE [INTEGER, STRING]</code> is valid only if <code>STRING</code> inherits from <code>HASHABLE</code> (as it indeed does in typical Eiffel libraries). Within the class, having <code>KEY</code> constrained by <code>HASHABLE</code> means that for <code>x: KEY</code> it is possible to apply to <code>x</code> all the features of <code>HASHABLE</code>, as in <code>x.hash_code</code>.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)