Shaco
Shaco

Reputation: 1

Io Language Create Class

How can I create a "Class" in Io language?

For example, I want to try the following:

Dog pd

barry := Dog new("Barry")
    
barry pd 

barry name println                // Attribute of the class 
barry allFoodItemsEaten println   // Attribute of the class 
     

lisa := Dog new("Lisa")

barry feed(12)                    // A Method
   

lisa feedSummary                 // A Method         

I know there are no classes in Io but I want to implement one. Any suggestions of how to implement that?

Upvotes: 0

Views: 84

Answers (2)

Peter Kofler
Peter Kofler

Reputation: 9440

You can simulate that behaviour with a method on Dog:

Dog := Object clone do(
    init := method(
       // init attributes here, e.g.
       self name := ""
    )
    new := method(name,
        result := self clone
        // init constructor values here
        result name = name
        result
    )
)
barry := Dog new("Barry")
barry name println // will print Barry

Upvotes: 0

Lucas
Lucas

Reputation: 382

I know there are no classes in Io but I want to implement one.

There's your issue. You cannot create classes, you can only clone existing objects. In your case, one solution could be

Dog := Object clone  // Create a Dog object
Dog name := nil  // Abstract name attribute

You can create "instances" of this "class" by cloning it further

barry := Dog clone
barry name = "barry"

etc.

Upvotes: 1

Related Questions