1

Taking inspiration from Javascript prototypes, I had the idea of a language where every instance can be used as a class.

Before I potentially reinvent the wheel, I would like to ask if there is a language already using this concept:

//To declare a Class, extend the base class (in this case, Type)
Type(Weapon,{price:0});

//Same syntax to inherit; simply extend the parent:
Weapon(Sword,{price:3});
Weapon(Axe,{price:4});

Sword(Katana,{price:7});
Sword(Dagger,{price:3});

//And the same to create an instance:
Katana(myKatana,{nickname:"Leon"});
myKatana.price; // 7
myKatana.nickname; // Leon

// An operator to return children of a class;
Sword_; // [Katana, Dagger]

// An operator to return array of descendants;
Sword__; // [Katana, Dagger, myKatana]

// An operator to return array of parents;
Sword^; // Weapon

// Arrays can be used as elements
Sword__.price += 1; //increases price of Sword's descendants by 1
mySword.price; //8

// And to access specific element (using its name instead of index)
var name = "mySword"
Katana_[name]; // [mySword]
Katana_[name].nickname; // Leon

Has this kind of approach been already studied/implemented?

5

JavaScript can already do this without the syntax sugar.

Live Example

Note the syntax is significantly uglier. It should be possible to write a DSL on top of JavaScript that uses your syntax

//To declare a Class, extend the base class (in this case, Type)
var Weapon = create(Object.prototype, { 
    price: 0,
    name: "Weapon"
})

//Same syntax to inherit; simply extend the parent:
var Sword = create(Weapon, { 
    price: 3,
    name: "Sword"
})
var Axe = create(Weapon, { 
    price: 4,
    name: "Axe"
})

var Katana = create(Sword, { 
    price: 7,
    name: "Katana"
})
var Dagger = create(Sword, { 
    price: 3,
    name: "Dagger"
})

//And the same to create an instance:
var myKatana = create(Katana, { 
    nickname: "Leon",
    name: "myKatana"
})
console.log(myKatana.price) // 7
console.log(myKatana.nickname) // Leon

// An operator to return children of a class;
console.log(children(Sword)) // [Katana, Dagger]

// An operator to return array of descendants;
console.log(descendants(Sword)) // [Katana, Dagger, myKatana]

// An operator to return array of parents;
console.log(Object.getPrototypeOf(Sword)); // Weapon

// Arrays can be used as elements
descendants(Sword).forEach(function (proto) {
    if (proto.hasOwnProperty("price")) proto.price += 1
})
console.log(myKatana.price) //8

// And to access specific element (using its name instead of index)
var name = "myKatana"
console.log(children(Katana)[name]) // [mySword]
console.log(children(Katana)[name].nickname) // Leon

function create(proto, props) {
    Object.keys(props).forEach(function (key) {
        props[key] = { 
            value: props[key],
            writable: true,
            configurable: true,
            enumerable: true
        }
    })
    var instance = Object.create(proto, props)
    if (!proto.hasOwnProperty("__children__")) {
        proto.__children__ = []
    }
    proto.__children__.push(instance)
    proto.__children__[props.name.value] = instance
    return instance
}

function children(proto) {
    return proto.__children__
}

function descendants(proto) {
    return proto.__children__.reduce(function (memo, value) {
        memo.push(value)
        if (value.hasOwnProperty("__children__")) {
            memo = memo.concat(descendants(value))
        }
        return memo
    }, [])
}​
0

Well I haven't seen a language exactly like yours, but prototype inheritance exists in JavaScript, for example. Which seems to me to satisfy your "every class instance is a class too". Unless your requirements are more specific than that.

0

I'm not 100% sure it's an exact match for your description, but based on your examples this seems fairly close to the io language.

More specifically, since you always create new "subclasses" in io by cloning an existing object, you're always able to treat any "subclass" as a distinct type upon which to base instances.

For some additional, hopefully clarifying, information, this section of the docs is probably the most relevant:

http://www.iolanguage.com/scm/io/docs/IoGuide.html#Objects-Inheritance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.