SPACESUITS: SATELLITE OBJECTS

Numbers, strings and lists are types that satellite gives you. A spacesuit is a type you make yourself. It bundles some variables, called its fields, together with the capsules that work on them, and your program then makes objects of it. Other languages call this a class, and satellite accepts satellite.class as a second spelling of satellite.spacesuit. The two words mean exactly the same thing.

The parts of a spacesuit

A spacesuit is written at the top of a file, outside every capsule, and it has up to three sections side by side between its braces:

  • satellite.protected { } holds the fields: a type, a name, and a first value if you give one. It can also hold capsules that only the spacesuit’s own capsules may call.
  • satellite.public { } holds the capsules that anyone with an object may call.
  • satellite.constructor(...) { } runs once when an object is made, after every field has its first value. It is how an object gets the values it was made with.

Here is a piggy bank. It knows its owner and how many pennies are inside, and it keeps a note each time money goes in.

piggy_bank.satlSATELLITE 004 Download
A spacesuit with fields, a constructor and capsules
satellite.include(satellite)

satellite.spacesuit piggy_bank()
{
    satellite.protected
    {
        satellite.variable.string owner = ""
        satellite.variable.number pennies = 0

        satellite.capsule note(satellite.variable.string what)
        {
            satellite.console.display(owner + ": " + what)
        }
    }

    satellite.constructor(satellite.variable.string owner_input)
    {
        owner = owner_input
    }

    satellite.public
    {
        satellite.capsule put_in(satellite.variable.number amount)
        {
            pennies = pennies + amount
            note("put in " + amount.to_string())
        }

        satellite.capsule call_pennies()
        {
            satellite.return(pennies)
        }
    }
}

satellite.capsule satellite.main()
{
    piggy_bank jar("sam")
    jar.put_in(25)
    jar.put_in(10)
    satellite.console.display(jar.call_pennies())

    piggy_bank tin("alex")
    tin.put_in(5)
    satellite.console.display(tin.call_pennies())
    satellite.return(satellite)
}
OUTPUT
sam: put in 25
sam: put in 10
35
alex: put in 5
5

Some things to notice:

  • You make an object by declaring it. piggy_bank jar("sam") is the spacesuit’s name, then the object’s name, then what the constructor takes. jar and tin are two separate piggy banks, and each has its own pennies.
  • Inside the spacesuit, fields and capsules are just their bare names. put_in writes pennies and calls note(...) directly. There is no this to write.
  • From outside, you call public capsules on the object: jar.put_in(25). A capsule hands a value back with satellite.return(...).

Fields stay inside

A field is reached from inside its spacesuit only, never from outside. jar.pennies is refused, and so is a call to a protected capsule such as jar.note("hi"). To let the rest of the program read a field, write a public capsule that answers it, as call_pennies does. satl catches this before anything runs:

peek.satlSATELLITE 004 Download
Reading a field from outside is refused
satellite.include(satellite)

satellite.spacesuit piggy_bank()
{
    satellite.protected
    {
        satellite.variable.number pennies = 0
    }
}

satellite.capsule satellite.main()
{
    piggy_bank jar
    satellite.console.display(jar.pennies)
    satellite.return(satellite)
}
OUTPUT
--------------------------------------------------------------------------------
SATELLITE CRITICAL ERROR REPORT
--------------------------------------------------------------------------------
S230: MEMBER_IS_PROTECTED
satl(check): in satellite.main, pennies is a field of piggy_bank and fields are
reached from inside the spacesuit only -- write a capsule in piggy_bank that
answers it and call that instead

directory: peek.satl:14
syntax: satellite.console.display(jar.pennies)
        /\

this is inside a spacesuit, where only its own capsules can reach it -- every
field is, and so is a capsule written in satellite.protected. A capsule in
satellite.public that answers it is the way in from outside.
machine code 52 member_is_protected -- satl exits with this.

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

This piggy bank has no constructor, so its object is made with just piggy_bank jar, and no brackets.

An object is a reference

A number or a list is copied when you give it to another name. An object is not. lamp same = desk gives the same lamp a second name, and a capsule that is handed an object changes the caller’s object, not a copy.

lamps.satlSATELLITE 004 Download
Objects are shared, not copied
satellite.include(satellite)

satellite.class lamp()
{
    satellite.protected
    {
        satellite.variable.bool on = satellite.bool.false
    }

    satellite.public
    {
        satellite.capsule switch_it()
        {
            on = !on
        }

        satellite.capsule call_state()
        {
            satellite.statement.if (on)
            {
                satellite.return("on")
            }
            satellite.return("off")
        }
    }
}

satellite.capsule press(lamp which)
{
    which.switch_it()
}

satellite.capsule satellite.main()
{
    lamp desk
    press(desk)
    satellite.console.display(desk.call_state())

    lamp same = desk
    same.switch_it()
    satellite.console.display(desk.call_state())

    satellite.container.list<lamp> room = {}
    room.append(desk)
    lamp ceiling
    room.append(ceiling)
    room[2].switch_it()
    satellite.console.display(room.size)
    satellite.console.display(ceiling.call_state())
    satellite.return(satellite)
}
OUTPUT
on
off
2
on

This lamp is written with satellite.class, the second spelling, and it has no constructor, so lamp desk makes one. press(desk) switched the real desk lamp on. same is the desk lamp under another name, so switching same switched it off again. A list of lamps, satellite.container.list<lamp>, holds the lamps themselves, so room[2].switch_it() turned on ceiling. A spacesuit’s name works as a type anywhere a type goes: a capsule’s parameter, what a list holds, or what a capsule answers.

A few rules satl checks for you

Every field is protected wherever you write it, and so is a capsule written outside any section. Beyond that, satl refuses each of these before anything runs, with a report that says which rule and where:

  • A field’s first value cannot use another field. Set it in the constructor instead.
  • An object must be given exactly what its constructor takes.
  • A spacesuit cannot be declared inside a capsule.

Where next

The FULL REFERENCE lists every spacesuit word. To build one spacesuit on top of another, or put one spacesuit inside another, read ONE SPACESUIT EXTENDING ANOTHER. Capsules themselves are covered in CAPSULES.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *