Dealing with generic parameters of a class

24 views
Skip to first unread message

Gachoud Philippe

unread,
Oct 19, 2018, 2:29:17 PM10/19/18
to Eiffel Users
As described here, I was wondering if there is such a mechanism and why can't I do something like G.bark if G -> DOG

Is there a way to check on given Generic parameter of a Class without any attached instance of it?

class BAG[G -> MOUSE]

feature --

    discriminate
        do
            if G.conforms_to (MAMMAL) then
                io.putstring ("you gave me a real animal")
            elseif G.conforms_to (COMPUTER_ACCESSORY) then
                io.putstring ("Seems you don't like animals such as computers")
            else
                io.pustring ("Still dont know what you are dealing with")
            end
        end


williams Lima

unread,
Oct 19, 2018, 2:39:44 PM10/19/18
to Eiffel Users
As probably many others here will say, it looks like you have a bad design.

regards
Williams

Colin Adams

unread,
Oct 19, 2018, 2:41:57 PM10/19/18
to eiffel...@googlegroups.com
No.

--
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.
Visit this group at https://groups.google.com/group/eiffel-users.
For more options, visit https://groups.google.com/d/optout.

Gachoud Philippe

unread,
Oct 19, 2018, 2:49:31 PM10/19/18
to Eiffel Users

What I'd like to do is to set the router of a EWF in a class that depending on the handler will set the router in a different way.... try to be more clear even if I'm maybe wrong with my design

class SOME_WSF_RESOURCE_HANDLER[G -> PARENT]

inherit
    WSF_RESOURCE_HANDLER_HELPER

feature

    set_handler
        do
            router.handle ("/Father", ...)
            router.handle ("/Father/" + found_child_class_name, ...)
        end

    found_child_class_name: STRING
        do
            Result := G.class_name
        end





On Friday, October 19, 2018 at 3:29:17 PM UTC-3, Gachoud Philippe wrote:

Gachoud Philippe

unread,
Oct 19, 2018, 2:52:04 PM10/19/18
to Eiffel Users
What I'd like to do is to set the router of a EWF in a class that depending on the handler will set the router in a different way.... try to be more clear even if I'm maybe wrong with my design

class SOME_WSF_RESOURCE_HANDLER[G -> PARENT]

inherit
    WSF_RESOURCE_HANDLER_HELPER

feature

    set_handler
        do
            router.handle ("/Father", ...)
            router.handle ("/Father/" + found_child_class_name, ...)
        end

    found_child_class_name: STRING
        do
            Result := G.class_name
        end


The only way I find in that case is to redefine the set_handler in an inheriting class or 2, or 3 depending on the decendents of G

Gachoud Philippe

unread,
Oct 19, 2018, 3:02:24 PM10/19/18
to Eiffel Users
As I may still be wrong with my design, Alexander gave an answer to this case if there's somebody else which needs it...

Larry Rix

unread,
Oct 19, 2018, 3:57:54 PM10/19/18
to eiffel...@googlegroups.com
Agree—there is bad assumptions (perhaps design) afoot here. Many here have been using Eiffel in small and large projects for 30+ years and never needed such mechanisms.

First—one presumes that MOUSE is the descendant of MAMMAL (i.e. MOUSE is a type of MAMMAL). Second, one presumes MOUSE is also a type of COMPUTER_ACCESSORY.

You're trying to have a BAG (container) filled with MOUSE objects and want to determine if they descend from MAMMAL or COMPUTER_ACCESSORY.

Thus—the code might be:

across items as ic loop
   if attached {MAMMAL} ic.item then
       io.putstring ("you gave me a real animal")
   elseif attached {COMPUTER_ACCESSORY} ic.item then
       io.putstring ("Seems you don't like animals such as computers")
   else 
        io.putstring ("Still don't know what you are dealing with") 
   end
end 

The BAG can be filled with polymorphic items descending from MOUSE (e.g. MAMMAL items or COMPUTER_ACCESSORY items).

A BAG is about its items (i.e. HASH_TABLE, ARRAYED_LIST, and so on). The compiler knows that Clients of this BAG [G -> MOUSE] can only put items in the BAG which conform to MOUSE.

Later—one does not want to know what {G} conforms to. We know from Generics list of BAG in the example that G conforms to MOUSE. Later, one wants to know the descendant type of each item in the BAG (presuming that MOUSE is perhaps deferred and we can only create instances of MAMMAL or COMPUTER_ACCESSORY).

Given all of this, your original statement of the problem incorrect. That matter is already handled by the type system. The runtime system is concerned with the items in the BAG. The Generic Parameter only serves to control what items go into the bag by conformance (type) checking.

Hope this makes sense.

Gachoud Philippe

unread,
Oct 19, 2018, 4:06:23 PM10/19/18
to eiffel...@googlegroups.com
Thx for the tip and remarks! actually, I think my example is bad and dont want to present the whole pattern to the group as I think it would be borring and maybe inadequate :-(

--
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.
Visit this group at https://groups.google.com/group/eiffel-users.
For more options, visit https://groups.google.com/d/optout.


--
**********************************************
Philippe Gachoud
Nuestra señora del Rosario 629
Las Condes
Santiago de Chile
0056 2 27 27 21 29
ph.ga...@gmail.com
**********************************************

Eric Bezault

unread,
Oct 19, 2018, 4:35:50 PM10/19/18
to eiffel...@googlegroups.com, Gachoud Philippe
Did you try:

found_child_class_name: STRING
do
Result := ({G}).name
end

--
Eric Bezault
mailto:er...@gobosoft.com
http://www.gobosoft.com


On 19/10/2018 20:52, Gachoud Philippe wrote:
> What I'd like to do is to set the router of a EWF in a class that
> depending on the handler will set the router in a different way.... try
> to be more clear even if I'm maybe wrong with my design
>
> class SOME_WSF_RESOURCE_HANDLER[G -> PARENT]
>
> inherit
>     WSF_RESOURCE_HANDLER_HELPER
>
> feature
>
>     set_handler
>         do
>             router.handle ("/Father", ...)
>             router.handle ("/Father/" + found_child_class_name, ...)
>         end
>
>     found_child_class_name: STRING
>         do
>             Result := G.class_name
>         end
>
>
> *The only way I find in that case is to redefine the set_handler in an
> inheriting class or 2, or 3 depending on the decendents of G*
>
> On Friday, October 19, 2018 at 3:49:31 PM UTC-3, Gachoud Philippe wrote:
>
>
> What I'd like to do is to set the router of a EWF in a class
> that depending on the handler will set the router in a different
> way.... try to be more clear even if I'm maybe wrong with my design
>
> class SOME_WSF_RESOURCE_HANDLER[G -> PARENT]
>
> inherit
>     WSF_RESOURCE_HANDLER_HELPER
>
> feature
>
>     set_handler
>         do
>             router.handle ("/Father", ...)
>             router.handle ("/Father/" + found_child_class_name,
> ...)
>         end
>
>     found_child_class_name: STRING
>         do
>             Result := G.class_name
>         end
>
>
>
>
>
> On Friday, October 19, 2018 at 3:29:17 PM UTC-3, Gachoud Philippe wrote:
>
> As described here
> <https://stackoverflow.com/questions/52898033/eiffel-is-there-a-way-to-test-a-given-generic-parameter-of-a-class-without-any>,

williams Lima

unread,
Oct 19, 2018, 5:03:33 PM10/19/18
to Eiffel Users
I think, Gachoud, that there is nothing inadequate as long as all the information is necessary for a clear and unambiguous description
of the problem. Post here the necessary amount of code/information for a clear statement of the problem, space here is free. And again,
if the code is too big just put something on github that can illustrate the problem, it is so easy and takes virtually no time.

Good luck!

best regards
Williams

Gachoud Philippe

unread,
Oct 20, 2018, 5:48:21 PM10/20/18
to Eiffel Users
Hi,
many thx for your patience!! so you'll find a better description on this page and all the code.

Many thx again for all who dedicate time to my project/problems ;-)


Feel free to do some pull request with comments into the code, or either do you find some easier alternatives to comment what you see.
Reply all
Reply to author
Forward
0 new messages