1

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Erdi İzgi
  • 1,262
  • 2
  • 15
  • 33

1 Answers1

3

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.

jer
  • 20,094
  • 5
  • 45
  • 69
  • I knew that this example was good to understand the language structure. I'll make more readings on that. I really liked IO, but there aren't so many resources, unfortunately. – Erdi İzgi Jan 30 '17 at 18:23
  • Well, I have created the withConstructor part. How can I add the instance attributes? Any tips? – Erdi İzgi Jan 30 '17 at 18:48
  • That's the thing, there's no distinction between "instance" and "class/static" attributes. The only difference is how you use them. You'd add `allFoodItemsEaten` to `Animal` directly, and if you want to set it or update it, tell Animal to update it, not self. – jer Jan 31 '17 at 18:08
  • @jer It's funny how long it took for me to understand that if I change attributes of the type (Elephant), those changes are also going to affect the instances (Dumbo, PinkElephant, or anything else that results from `Elephant clone`) – treaz Aug 25 '19 at 13:08