Erdi İzgi
Erdi İzgi

Reputation: 1322

Creating a constructor, adding attributes and methods in the Io language

I was practicing on the Io language. Finding sources is so hard. As you know there is no class in the Io language. But we can create our classes with our needings. Anyway, let's say I want to be able to run this and create an Animal class.

Animal := Class new("Animal",
    withConstructor(nameParam,
        name = nameParam
    )
    withInstanceAttribute("name", "")
    withInstanceAttribute("foodItemsEaten", 0)
    withStaticAttribute("allFoodItemsEaten", 0)
    withMethod("feed", howMuchFood,
        foodItemsEaten = foodItemsEaten + howMuchFood
        class allFoodItemsEaten = allFoodItemsEaten + howMuchFood
        "#{name} has just eaten #{howMuchFood} food items." interpolate println
    )
    withMethod("feedSummary",
        "So far #{foodItemsEaten} food items eaten by #{name} and #{class allFoodItemsEaten} in total by all dogs." interpolate println
    )
)

So I have this class structure:

Class := Object clone

Class new := method(name,                 // Meta-class constructor
    cls := Class clone
    cls __name := name                    // Name of the class
    cls __instanceProto := Object clone   // Prototype of instances - hold instance field along with their initial values
    cls __instanceProto class := cls

    call argAt(1) doInContext(cls)
    cls
)

I'm trying to add withConstructor method for example, but I can't even read the parameters.

Class withConstructor := method(nameParam
  self
)

I just couldn't handle it, even the constructor. It says "Class doesn't respond nameParam". The syntax is easy, but I guess I still didn't figure out the structure of the language. Any idea, similar sources, or someone who can explain it?

Upvotes: 2

Views: 274

Answers (1)

jer
jer

Reputation: 20236

To answer your question, you'll want to try: call argAt(1) doInContext(cos) where the 2nd argument you pass into that method is a do() wrapper.

Now, ignoring that, let me address the real problem here. Classes and Objects are the same thing. The only difference is that usually compilers limit what you can do with classes, and limit what you can do with instances. Io doesn't do that, it says you can treat everything as an object and use it however you want. So with this in mind, consider how Io handles inheritance. It's not how languages like Ruby would, where they inject certain methods into new objects so they can function in isolation. Io uses a method of inheritance called "differential inheritance". The way this was explained to me in 2004 was roughly this:

"Think in terms of elephants. Everyone knows that an elephant is large, grey, has 4 legs, ears, and a trunk. So if someone asks you what Dumbo looks like, you might say, 'Dumbo is a short elephant, with big floppy ears that wears a bow and can fly.' You don't have to tell them everything an elephant is made of to describe Dumbo, we know that because we know Dumbo is an elephant and Dumbo's own attributes override what an elephant has"

This means, your static variable you can simply treat it like a normal "instance variable", and just don't write to it in any instances. If you do, it'll keep that overwritten value. There's no such thing as private scope in Io, but you can somewhat fake it with external storage if you absolutely need things to be private. Io falls more on the "if you know what you're doing, feel free to shoot yourself in the foot" side of programming languages.

Upvotes: 3

Related Questions