Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Eiffel (programming language)
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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!
===Operator and bracket syntax, assigner commands=== Eiffel's view of computation is completely object-oriented in the sense that every operation is relative to an object, the "target". So for example an addition such as <syntaxhighlight lang="eiffel"> a + b </syntaxhighlight> is conceptually understood as if it were the method call <syntaxhighlight lang="eiffel"> a.plus (b) </syntaxhighlight> with target <code>a</code>, feature <code>plus</code> and argument <code>b</code>. Of course, the former is the conventional syntax and usually preferred. Operator syntax makes it possible to use either form by declaring the feature (for example in <code>INTEGER</code>, but this applies to other basic classes and can be used in any other for which such an operator is appropriate): <syntaxhighlight lang="eiffel"> plus alias "+" (other: INTEGER): INTEGER -- ... Normal function declaration... end </syntaxhighlight> The range of operators that can be used as "alias" is quite broad; they include predefined operators such as "+" but also "free operators" made of non-alphanumeric symbols. This makes it possible to design special infix and prefix notations, for example in mathematics and physics applications. Every class may in addition have ''one'' function aliased to "[]", the "bracket" operator, allowing the notation <code>a [i, ...]</code> as a synonym for <code>a.f (i, ...)</code> where <code>f</code> is the chosen function. This is particularly useful for container structures such as arrays, [[hash table]]s, lists etc. For example, access to an element of a hash table with string keys can be written <syntaxhighlight lang="eiffel"> number := phone_book ["JILL SMITH"] </syntaxhighlight> "Assigner commands" are a companion mechanism designed in the same spirit of allowing well-established, convenient notation reinterpreted in the framework of object-oriented programming. Assigner commands allow assignment-like syntax to call "setter" procedures. An assignment proper can never be of the form <code>a.x := v</code> as this violates information hiding; you have to go for a setter command (procedure). For example, the hash table class can have the function and the procedure <syntaxhighlight lang="eiffel"> item alias "[]" (key: STRING): ELEMENT [3] -- The element of key `key'. -- ("Getter" query) do ... end put (e: ELEMENT; key: STRING) -- Insert the element `e', associating it with the key `key'. -- ("Setter" command) do ... end </syntaxhighlight> Then to insert an element you have to use an explicit call to the setter command: <syntaxhighlight lang="eiffel"> [4] phone_book.put (New_person, "JILL SMITH") </syntaxhighlight> It is possible to write this equivalently as <syntaxhighlight lang="eiffel"> [5] phone_book ["JILL SMITH"] := New_person </syntaxhighlight> (in the same way that <code>phone_book ["JILL SMITH"]</code> is a synonym for <code>number := phone_book.item ("JILL SMITH")</code>), provided the declaration of <code>item</code> now starts (replacement for [3]) with <syntaxhighlight lang="eiffel"> item alias "[]" (key: STRING): ELEMENT assign put </syntaxhighlight> This declares <code>put</code> as the assigner command associated with <code>item</code> and, combined with the bracket alias, makes [5] legal and equivalent to [4]. (It could also be written, without taking advantage of the bracket, as <code>phone_book.item ("JILL SMITH") := New_person</code>. Note: The argument list of a's assigner is constrained to be: (a's return type;all of a's argument list...)
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Eiffel (programming language)
(section)
Add topic