1. Record example

Here is a small example of bookstore application that makes use of "my"
idea of records in Euphoria. This kind of syntax won't break existing code.
Record members are accesed by subscripts, the interpreter would
automatically translate member names to their appropiate subscript index.
The "of" keyword is used to limit the data types supported by a sequence
(currently it's data type "object"). Comments and suggestions are welcome.
I would really like to hear Rob's opinion on the viability of such an
enhancement.

    record Author
                sequence FistName
                sequence MiddleName
                sequence lastName
    end record
   =20
    record Book
                sequence ISBN
                sequence Title
                sequence of Author
                integer Pages
                integer Year
                sequence Publisher
                sequence Summary
                atom Price
    end record

    sequence Books of Book
   =20
    function AddBook(record NewBook)
        for i =3D 1 to length(Books)
            if equal(Books[i][ISBN], NewBook[ISBN]) then
                return -1
            end if
        end for
        append(Books, NewBook)
    end function
   =20
    function DeleteBook(sequence ISBN)
        integer pos
        pos =3D 0
        for i =3D 1 to length(Books) do
            if equal(Books[i][ISBN], ISBN) then
                pos =3D i
            end if
        end for
        if pos then
            if pos =3D 1 and length(Books) =3D 1 then
                Books =3D {}
            elsif pos =3D length(Books) then
                Books =3D [1..length(Books)-1]
            else
                Books =3D Books[1..pos-1] & Books[pos+1..length(Books)]
            end if
            return 1
        else
            return 0
        end if
    end function
   =20
    function GetBook(sequence ISBN)
        for i =3D 1 to length(Books) do
            if equal(Books[i][ISBN]), ISBN) then
                return Books[i]
            end if
        end for
    end function
   =20
-- -- -- -- -- -- -- -- -- -- -- -- --

    Author new_author
   =20
    new_author[FirstName] =3D "Charles"
    new_author[MiddleName] =3D ""
    new-author[LastName] =3D "Petzold
   =20
    Book new_book
   =20
    new_book[ISBN] =3D "84-481-0723-3"
    new_book[Title] =3D "Programaci=A2n en Windows 95" -- It's in spanish
    new_book[Auhtor] =3D {new_author}
    new_book[Pages] =3D 1094
    new_book[Year] =3D 1996
    new_book[Publisher] =3D "McGraw Hill / Microsoft Press"
    new_book[Summary] =3D "Best introductory WinAPI \n book available"
    new_book[Price] =3D 49.95


Special attention requires Jacques Guy "ilumininated" suggestion about
routine id's. OO Euphoria now seems more than feasible! It's implementation
needs foward declarations, read-only fields, and a "this" kind of
reference. A a new syntax should be created:
call_proc(rec[Draw]) isn't very nice ;)


Regards,
        Daniel   Berstein
        daber at pair.com

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu