Getting TYPE on Generic

20 views
Skip to first unread message

lrix

unread,
Mar 25, 2021, 11:07:50 AM3/25/21
to Eiffel Users
How does one get the TYPE [K] of a Generic without creating an object of that type? I need to base creation of KL_COMPARATOR [K], where K = type of the key.


determine_K_type_from_generic.png

lrix

unread,
Mar 25, 2021, 11:09:25 AM3/25/21
to Eiffel Users
Obviously, my code example (above) does not work because I cannot do a non-object call on generating_type. I have to think that such a test to determine precisely what [K] is can be done. :-)

lrix

unread,
Mar 25, 2021, 11:17:00 AM3/25/21
to Eiffel Users
Nevermind ...
comparable_comparator.png

Gachoud Philippe

unread,
Mar 25, 2021, 11:32:43 AM3/25/21
to eiffel...@googlegroups.com
as far as I remember ({K}).generating_type, see ANY and TYPE class for more details and functions

On Thu, Mar 25, 2021 at 12:07 PM lrix <lr...@jinny.com> wrote:
How does one get the TYPE [K] of a Generic without creating an object of that type? I need to base creation of KL_COMPARATOR [K], where K = type of the key.


--
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/52ee39ea-54f1-4a0c-889a-a69336dc1b2en%40googlegroups.com.


--
**********************************************
Philippe Gachoud
Puerto Williams 6657
Las Condes
Santiago de Chile
RUT: 26374747-k
**********************************************

lrix

unread,
Mar 25, 2021, 11:36:20 AM3/25/21
to Eiffel Users
So, the solution turned out to be writing a very generic KL_COMPARATOR that could be sufficient for type-variants for K

Ulrich Windl

unread,
Mar 26, 2021, 2:43:58 AM3/26/21
to eiffel...@googlegroups.com
Hi!

I'm not good in Java, but I think the major difference is that in Java you make a class comparable by adding and implementing the comparator interface, while in Eiffel you inherit COMPARABLE.

Regards,
Ulrich

>>> lrix <lr...@jinny.com> schrieb am 25.03.2021 um 16:17 in Nachricht
<11947d82-6718-48a7...@googlegroups.com>:
> --
> 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/11947d82-6718-48a7-a1d5-69b45d
> 391e95n%40googlegroups.com.




Rosivaldo Fernandes Alves

unread,
Apr 1, 2021, 9:43:28 AM4/1/21
to eiffel...@googlegroups.com
Hi Irix.

I hope I'm not getting wrong your question, but I have managed to get
the type of a generic via querying {TYPE}.generic_parameter_type
feature. Below is the code of a function I've written where I use such a
facility.

Hope this helps.

Best regards,

Rosivaldo.

result_type (a_new_instance: FUNCTION [detachable separate ANY]): TYPE
[detachable ANY]
-- Result type of the function accessed via `a_new_instance' agent
local
l_new_instance_type: TYPE [detachable ANY]
do
l_new_instance_type := a_new_instance.generating_type
check
large_enough: l_new_instance_type.generic_parameter_count >= 1 --
FUNCTION [detachable separate ANY] has generic parameters.
small_enough: l_new_instance_type.generic_parameter_count <=
l_new_instance_type.generic_parameter_count -- By definition
end
Result := l_new_instance_type.generic_parameter_type
(l_new_instance_type.generic_parameter_count)
end
> --
> 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
> <mailto:eiffel-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/eiffel-users/52ee39ea-54f1-4a0c-889a-a69336dc1b2en%40googlegroups.com
> <https://groups.google.com/d/msgid/eiffel-users/52ee39ea-54f1-4a0c-889a-a69336dc1b2en%40googlegroups.com?utm_medium=email&utm_source=footer>.
Reply all
Reply to author
Forward
0 new messages