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

> 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 message » categorize

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

Also, Euphoria doesn't support structures like
C/C++/COBOL/Pascal/Delphi/Java/VB/... does.

This means that ...
a) You can't name any of the elements in a sequence. They are only
referenced by integer index values.
b) You can't nominate the data type of a sequence element. All sequence
elements are of type 'object'.
c) You can't force Euphoria to have a fixed length sequence.
d) You can't map a sequence to an allocated RAM area.

If you want any of these things (that are standard in other languages) you
have to code them yourself.

For example,

--- Define some commonly used types ---
type StringASCII(object x)
    if not sequence(x) then
        return 0
    end if
    for i = 1 to length(x) do
        if not integer(x[i]) or
              x[i] < 0 or
              x[i] > 255 then
           return 0
        end if
    end for
    return 1
end type

type Empty(object x)
   if not sequence(x) or length(x) != 0 then
       return 0
   else
       return 1
   end if
end type

--- Define the ADDRESS structure ---
constant aType = 1,
         aBuilding = 2,
         aStreet = 3,
         aCity = 4,
         aState = 5,
         aPostCode = 6,
         aCountry = 7,
         AddressLength = 7,
         AddressType = "addr",
         AddressCreate = {AddressType, "", "", "", "", "", ""}

type Address(object x)
    if not sequence (x) then
        return 0
    end if
    if length(x) != AddressLength then
        return 0
    end if
    if not equal(x[aType], AddressType) then
        return 0
    end if
    if not StringASCII(x[aBuilding]) then
        return 0
    end if
    if not StringASCII(x[aStreet]) then
        return 0
    end if
    if not StringASCII(x[aCity]) then
        return 0
    end if
    if not StringASCII(x[aPostCode]) then
        return 0
    end if
    if not StringASCII(x[aCountry]) then
        return 0
    end if
    if not StringASCII(x[aState]) then
        return 0
    end if

    return 1
end type

--- Define the CUSTOMER structure ---
constant cType = 1,
         cName = 2,
         cAddress = 3,
         cTelephone = 4,
         cStatus = 5,
         CustomerLength = 5,
         CustomerType = "cust",
         ckStatusActive = 1,
         ckStatusInactive = 0,
         CustomerCreate = {CustomerType, "", "", "", ckStatusInactive}

type CustomerStatus(object x)
    return (find(x, {ckStatusActive, ckStatusInactive}) != 0)
end type

type Customer(object x)
    if not sequence (x) then
        return 0
    end if
    if length(x) != CustomerLength then
        return 0
    end if
    if not equal(x[cType], CustomerType) then
        return 0
    end if
    if not StringASCII(x[cName]) then         
        return 0
    end if
    if not Address(x[cAddress]) and not Empty(x[cAddress]) then         
        return 0
    end if
    if not StringASCII(x[cTelephone]) then         
        return 0
    end if
    if not CustomerStatus(x[cStatus]) then         
        return 0
    end if

    return 1
end type

-- Now use them.
Customer newCustomer
Address  newAddress

   newAddress = AddressCreate
   newAddress[aBuilding] = "Level 11"
   newAddress[aStreet] = "636 StKilda Road"
   newAddress[aCity] = "Melbourne"
   newAddress[aState] = "Victoria"
   newAddress[aPostCode] = "3004"
   newAddress[aCountry] = "Australia"

   newCustomer = CustomerCreate
   newCustomer[cName] = "Global Technology A'asia Ltd"
   -- If the address is bad, don't use it.
   if Address(newAddress) then
       newCustomer[cAddress] = newAddress
   else
       newCustomer[cAddress] = {}
   end if
   newCustomer[cTelephone] = "+61 3 92917500"
   newCustomer[cStatus] = ckStatusActive

   if Customer(newCustomer) then
      -- okay!
      puts(1, "Okay\n")
   else
      -- logic error somewhere
      puts(1, "Ooops!\n")
   end if
-----------

As you can see, this is not trivial.

Another thing is that whenever you make an assignment to any element in a
'type' that is based on a sequence, your type checking routine is run. This
means that even though you are changing only one element in a structure,
every element gets revalidated. This makes sense of course, but it does slow
things down.

----
cheers,
Derek Parnell
Senior Design Engineer
Global Technology Australasia Ltd
dparnell at glotec.com.au

---------------------



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     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu