RE: fundamental data types in Euphoria cf C/C++
- Posted by Derek Parnell <ddparnell at bigpond.com> Aug 07, 2001
- 433 views
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.