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!
===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...)
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)