Setter for functions (I think I got it)

12 views
Skip to first unread message

Thomas Goering

unread,
Jun 14, 2024, 11:11:08 AMJun 14
to Eiffel Users
This is in reference to my question in the other thread "EiffelStudio support for common entry tasks":

---
Could someone please explain to me how a setter (procedure) like the example above (set_a) would work for an argumentless function? I simply don't get it, especially because there is already a way to add a new feature in EiffelStudio (menu Tools -> New Feature, then select feature type "Function") that suggests that this makes sense.
---

I think I now know how this is supposed to work but I'm not sure. Assume the following class:

class
PERSON

create
make

feature {NONE} -- Initialization

make (a_first_name: like first_name; a_last_name: like last_name)
do
first_name := a_first_name
last_name := a_last_name
end

feature -- Access

first_name: STRING
last_name: STRING

end

Using "Tools" -> "New Feature" I add a new function called "full_name" including a setter ("Assigner?" also checked). This is the code that is generated:

full_name: STRING assign set_full_name
-- `full_name'
do
check False then end --| Remove line when `Result' is initialized in body.
end

set_full_name (a_full_name: like full_name; )
-- Assign `full_name' with `a_full_name'.
do
--| Assigner code
end

And this is my attempt to implement it:

full_name: STRING
-- `full_name'
do
if attached full_name_function as fnf then
Result := fnf
else
Result := first_name + " " + last_name
end
end

full_name_function: detachable like full_name

set_full_name (a_full_name: like full_name; )
-- Assign `full_name' with `a_full_name'.
do
full_name_function := a_full_name
end

And a caller could be like:

full_name: STRING
do
Result := person.last_name + ", " + person.first_name
end

person: PERSON

do_something
do
create person.make ("John", "Doe")
print (person.full_name + "%N")
person.set_full_name (full_name)
print (person.full_name + "%N")
end

This outputs:

---
John Doe
Doe, John
---

Any comments?

If this is reasonable then why does the new composer functionality "Add Setter" not work for functions? ;-)

Thomas
Reply all
Reply to author
Forward
0 new messages