liberty-eiffel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Liberty-eiffel] Array with integers. real and strings


From: Paolo Redaelli
Subject: Re: [Liberty-eiffel] Array with integers. real and strings
Date: Wed, 30 Mar 2016 17:33:34 +0200
User-agent: K-9 Mail for Android

It looks like the VARIANT and TYPED_VARIANT design pattern...

Il 30 marzo 2016 17:20:33 CEST, Laurie Moye <address@hidden> ha scritto:
Hi Germán,

On 27/03/16 20:23, Germán Arias wrote:
Is posible have an array/native array with integers, real and strings? I
try with ARRAY[ANY], but don't work. Or maybe a tuple with different
types? The problem is that I don't know. beforehand, the order of the
arguments. This is, something it can be: integer, string, integer, .....
and others: string. string, real,... And I need pass it to a C
function.
I don't know whether this would solve your problem, but I use a generic
wrapper class inheriting from a dummy parent class to wrap up mixed-type
variables in an array.
First, the dummy class

class POLY_ARG
-- no content at all
end class POLY_ARG

Then a generic wrapper class that inherits POLY_ARG

class POLY_THIS [G]

inherit
POLY_ARG

create {ANY}
make, make_empty



feature {ANY}-- Public features

item : G

make (x : G) is
do
item := x
end

make_empty is
do
end

put(x : G) is
do
item := x
end



end -- class POLY_THIS

Then you can put POLY_THIS[ INTEGER ], POLY_THIS[ STRING ], etc.
variables into an ARRAY[ POLY_ARG ]. In Eiffel, you can sort them out
with code like this (I'm reading arguments from a parameters file and
need to parse each one correctly), but as you want to pass the array to
C, it might be more difficult to sort them out. I think this is quite an
Eiffelish solution!



feature {} -- Concealed features

vars : ARRAY[ POLY_ARG ]
... ... ...;
feature {ANY} -- Public features
... ... ...
var : POLY_ARG
this_b : POLY_THIS[BOOLEAN]
this_c : POLY_THIS[CHARACTER]
this_i : POLY_THIS[INTEGER]
this_r : POLY_THIS[REAL]
this_s : POLY_THIS[STRING]
... ... ...
var := vars @ i
if var /= Void then
-- convert string and put into variable
this_b ?= var
if this_b /= Void then
this_b.put(s2b(str))
else
this_c ?= var
if this_c /= Void then
this_c.put(s2c(str)
else
this_i ?= var
if this_i /= Void then
this_i.put(s2i(str))
... ... ... -- and so on



Best wishes,
Laurie



-- Inviato dal mio cellulare Android con K-9 Mail.
reply via email to

[Prev in Thread] Current Thread [Next in Thread]