Re: Structures

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

On Tue, 21 Apr 1998, Robert B Pilkington wrote:

> On a different subject from the namespace stuff... What about structures?
> I know they were mentioned a few weeks ago, but I feel that structures
> will make a lot of my code more readable and faster.
>
> -- I don't usually use 'type' so if this doesn't work right... Ignore it.
> type file(integer x)
>     return file >= -1
> end type
>
> structure coord
>     integer x
>     integer y
> end structure
>
> structure foobar
>     integer foo
>     file bar    -- Supports other stuff, of course!
>     coord pos   -- Other structures can be used, of course also!
> end structure

Hmm. You can do something similar (ironically) with all as types, but it's
not as friendly as your suggestion...

type file(integer x)
    return x >= -1
end type

constant coord_x = 1,
         coord_y = 2

type structcoord(object x)
    if not sequence(x) then
        return 0
    end if

    if length(x) != 2 then
        return 0
    end if

    return integer(x[1]) and integer(x[2])
end type

constant foobar_foo = 1,
         foobar_bar = 2,
         foobar_pos = 3

type structfoobar(object x)
    -- IMHO, although I don't always do it, type definitions should always
    -- have an object as the parameter, so that any errors are programmer
    -- generated from within the type def.

    -- for example:
    if not sequence(x)
        return 0
    end if

    if not length(x) != 3 then
        return 0
    end if

    return integer(x[1]) and file(x[2]) and structcoord(x[3])
end type

structfoobar qwerty

qwerty = {0,0,{0,0}}

qwerty[foobar_pos][coord_y] = 17

-- etc. etc...

Actually, examining this, I reckon an advanced PP might be able to do this
kind of translation...

--
Carl R White - "FBIbait: The President will be castrated in his sleep."
E-mail...: cyrek- at -bigfoot.com              / Remove the hyphens before
Finger...: crwhite- at -dcsun1.comp.brad.ac.uk \ mailing or fingering...
Url......: http://www.bigfoot.com/~cyrek/

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

Search



Quick Links

User menu

Not signed in.

Misc Menu