RE: fundamental data types in Euphoria cf C/C++

new topic     » topic index » view thread      » older message » newer message

> From: Alex Caracatsanis <sunpsych at hotkey.net.au>
> To: EUforum <EUforum at topica.com>
> Reply-To: EUforum at topica.com
> Organization: Sunraysia Psychiatric Centre
> Subject: fundamental data types in Euphoria cf C/C++
> Date: 8/08/2001 10:41:22 AM
> 
Hi Alex,
its been years since I've been up Mildura way. Hope you are getting some
rain, Melbourne hasn't had any decent fails since August 1996 and we are
down to about 30% capacity at the end of Winter! Doesn't look good.
Anyhow...
 
> I'm intrigued at the difference in number of  basic data types in
> Euphoria compared with C/C++ ( with its int, float, double, 
> short, long, etc),  and the need to cast from one type to another.
> 
> Do you ever have to worry about these things in Euphoria? 
> Does it automatically choose the correct type?
> Does it always automatically cast types? 

Mostly, you don't have to worry about casting. You can't 'cast' types in
Euphoria. You can only assign integers to integers, atoms to atoms and
sequences to sequences. The only exceptions are that you can assign an
integer to an atom, and anything to an object.

To assign an atom to an integer one should really use the floor() function.

To assign an integer or an atom to a sequence one needs to use the braces
operator (eg. Seq = {atom})

As Euphoria only supports 31-bit integers, the concept of short and long
doesn't apply.

Same with float and double, Euphoria automatically uses the best floating
point representation for the value concerned.

Atoms are used to store floating point numbers. They can also store
integers.

Euphoria has some functions that tell you what type of data you are dealing
with and you can adjust your program accoridingly.

  integer(), atom(), sequence() return the value 1 if their parameter is of
the right type or zero otherwise.

This means you can do this sort of thing...

   if sequence(x) then
        printf(1, "%s\n", {x})
   elsif integer(x) then
        printf(1, "%d\n", {x})
   else
        printf(1, "%f\n", {x})
   end if

You cannot assign a sequence to an atom or integer.


> Can Euphoria programs go awry because of the type issue?

Yes. This is often because of the very flexible 'object' type. Programmers
can forget to check the actual data type stored in an object and make some
assumptions instead. These usually catch up with you one day blink


Euphoria also supports a rudimentary user-defined type system. It's not as
well rounded as we would like it but its better than nothing.

For example, if I wanted to use imaginary numbers, I could store them as a
two-atom sequence. So I could define a type checking function thus...

--------------
include machine.e
type ImagNumb(object x)
    if sequence(x) and
       length(x) = 2 and
       atom(x[1]) and
       atom(x[2]) then
      return 1
    else
       crash_message("Imaginary Number expected.\n" &
                     "The file 'ex.err' contains details.")
       return 0
    end if
end type

ImagNumb a,b,c

   a = {1.0, 3}  -- Assign works.

   b = {1,2,3}  -- assign fails.

   c = 1.5  -- assign fails.
------------------
cheers
Derek Parnell,
Melbourne,
Australia


confidential information intended solely for the use of the individual or
entity to whom they are addressed. If you are not the intended recipient of
this message you are hereby notified that any use, dissemination,
distribution or reproduction of this message is prohibited. If you have
received this message in error please notify the sender immediately. Any
views expressed in this message are those of the individual sender and may
not necessarily reflect the views of Global Technology Australasia Limited.

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu