ARRAY [G] creation routine make_filled. How to get default value of G?

68 views
Skip to first unread message

Brother Bill

unread,
May 29, 2024, 11:58:06 AMMay 29
to Eiffel Users
OOSC2 page 349 allowed:   representation.make (1, capacity)
But modern 23.09 Eiffel provides 
make_empty or 
make_filled (a_default_value: G; min_index, max_index: INTEGER)

These are used to represent our humble STACK.

For make_filled, min_index := 1, max_index := capacity.
How do I get a default value of G, where G is unconstrained?

Brother Bill

unread,
Jun 13, 2024, 10:48:45 AMJun 13
to Eiffel Users
Some classes don't have a default value, so they can't prefill values of the container.
For example, what would be a default value of BOOK1?  A book with blank author, blank title, etc.?

Thomas Goering

unread,
Jun 13, 2024, 1:58:50 PMJun 13
to Eiffel Users
You should really provide more context in your posts and explain what exactly you are trying to achieve.

For those who didn't look into OOSC2: Page 349 shows parts of class STACK2, one of four stack implementations that are discusses in the book, this chapter is about assertions. The call representation.make (1. capacity) is from STACK2's creation procedure and representation is of type ARRAY which is discussed at least twice elsewhere in the book and its implementation of make is always empty.

Now what are you trying to achieve?

If you look at either the concepts and classes in OOSC2 or the classes in the current libraries and don't try to mix them then I should get clearer:

The classes in OOSC2 are not complete. But you see that STACK2 doesn't need a default value for the ARRAY creation. And to me nothing else makes sense because the created stack is empty. A "default value" would then not even be accessible and thus is not needed.

Stacks are implemented differently in the current libraries. Class STACK is deferred but there are e.g. ARRAYED_STACK and BOUNDED_STACK and neither need default values. So there's still the question about ARRAY.make_filled. And in this case I would look at callers of this creation procedure. E.g. look at FIBONACCI.all_lower_fibonacci and how it creates an ARRAY of BOOLEAN values (False) and then processes it. So it's about the client and if the client decides to create a "prefilled" array for a specific task then it is also able to provide a default value for it.

Thomas

Brother Bill

unread,
Jun 13, 2024, 4:34:55 PMJun 13
to Eiffel Users
Thomas, thanks for the response.  I have been hacking with Eiffel for decades, but now am getting serious.
I am creating the first Eiffel Udemy.com course, as no one else has stepped up to do so.

The course is intended to make it easier to learn Eiffel and be useful with it.

I learn best from examples, so there are tons of working examples, and many corresponding C# examples, like a Rosetta Stone.
Here is the C# code, here is the equivalent Eiffel code.  Aha, that's what's going on!

Regarding default values, there are classes with default values, such as INTEGER and classes without default values such as BOOK1.
Of course, classes that require a default value and the generic parameter doesn't have one, the compiler must complain.
My course is using ARRAYED_LIST as it has sufficient behavior as an implementation class.

Is there a class such as CLASS_WITH_DEFAULT_VALUE so that we can have this constraint?

Thank you all for your comments.

Thomas Goering

unread,
Jun 13, 2024, 5:26:04 PMJun 13
to Eiffel Users
I don't know if I understand you correct. ARRAYED_LIST.make_filled is:

make_filled (n: INTEGER_32)
-- Allocate list with `n` items.
-- (`n` may be zero for empty list.)
-- This list will be full.
require
valid_number_of_items: n >= 0
has_default: ({G}).has_default
do
index := 0
make_filled_area (({G}).default, n)
ensure
correct_position: before
filled: full
end

It's the client obligation to satisfy the preconditions. Are you proposing to have, instead of precondition has_default, class ARRAYED_LIST defined like "class ARRAYED_LIST [G -> {CLASS_WITH_DEFAULT_VALUE}]"?

If yes, what if someone wants to use ARRAYED_LIST with classes not conforming to CLASS_WITH_DEFAULT_VALUE by using e.g. the make (not make_filled) creation procedure? This wouldn't be possible anymore.

Thomas

rfo amalasoft.com

unread,
Jun 13, 2024, 6:07:23 PMJun 13
to eiffel...@googlegroups.com
Arrayed list's precondition for make_filled requires that the type of item contained does have a default value.
It seems a common check, most taking the form:   ({G}).has_default
Where G is the type of items the container will hold (e.g. ARRAYED_LIST [G])
One might imagine, in a redefined creation routine, that one could fidthe type of the items at that point, if one were so inclined.
Even ANY has a feature called default (frozen and detachable).  How one might uses that to any benefit, I don't know.
R

From: eiffel...@googlegroups.com <eiffel...@googlegroups.com> on behalf of Thomas Goering <tho...@goering.com>
Sent: Thursday, June 13, 2024 5:26 PM
To: Eiffel Users <eiffel...@googlegroups.com>
Subject: [eiffel-users] Re: ARRAY [G] creation routine make_filled. How to get default value of G?
 
--
You received this message because you are subscribed to the Google Groups "Eiffel Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to eiffel-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/eiffel-users/926e02d1-a600-4cbc-8dc1-db0d6068bdbcn%40googlegroups.com.

Ulrich Windl

unread,
Jun 14, 2024, 4:30:24 AMJun 14
to eiffel...@googlegroups.com
Hi!

I wonder: Wouldn't it be a better constructor to have a call-back (agent) receiving the position to setup, and returning the object to assign to that array position. A default agent could still return a default value.

Ulrich

Brother Bill

unread,
Jun 15, 2024, 7:50:31 AMJun 15
to Eiffel Users
Is there a class similar to COMPARABLE that indicates that a default value exists.?
Then we could add a Generic Constraint such as HAS_DEFAULT_VALUE.

Ulrich Windl

unread,
Jun 16, 2024, 12:31:50 AMJun 16
to eiffel...@googlegroups.com
Isn't it that every object in Eiffel does have a default value (by recursing to basic types)? It just may be that such default value does not match the class invariant
Reply all
Reply to author
Forward
0 new messages