2
Site
100

So-o

Implementing an object-oriented model in a structured programming language.

So-o defines a functional layer which adds an object-oriented programming model to a structured programming language. Inspired by Smalltalk, So-o is complete, simple and light, easy to understand.

So-o has 3 functions: defclass which defines a new class, sendmsg which is systematically used to send a message to a class or an instance, and supersend which runs a method inherited from a superclass. Simply object-oriented!

So-o proposes a standard implementation in several languages. The code in PHP is less than a 1000 lines. The code in C in just about 1500 lines. The code in JavaScript is not even 700 lines.


Example
namespace Hello;

require_once 'So-o.php';

defclass('Hello', null, 1, null, null, null, array('hello'));

function i_hello($self) {
    echo 'Hello from So-o!', PHP_EOL;

    return $self;
}

Defines the class Hello which inherits from the default class Object. Its revision number is 1. It adds no class or instance properties. It adds no class methods. It adds an instance method called hello.

The function i_hello defined in the Hello namespace implements the instance method called hello. $self is the instance which has received the hello message.

$ php -a
php > require_once 'Hello.php';
php > $hello=sendmsg($Hello, 'new');
php > sendmsg($hello, 'hello');
Hello from So-o!

Loads the code for the Hello class. Sends the message new to the Hello class. The new method is implemented by the Object class. It returns a new instance of the class. Sends the message hello to the $hello instance.


Example
#include "So-o.h"

class Hello;

static instance i_hello(instance self) {
    printf( "Hello from So-o!\n" );

    return self;
}

void defclassHello() {
    selector _i_messages[] = {
        "hello",    METHOD(i_hello),
        0, 0
    };

    Hello = defclass("Hello", 0, 1, 0, 0, 0, _i_messages);
}

A simple main to illustrate how the Hello class is used:

#include "So-o.h"

#include <stdlib.h>

extern class Hello;

extern void defclassHello();

int main( int argc, char *argv[] ) {
    instance hello;

    defclassHello();

    hello = (instance)sendmsg(Hello, "new").p;

    sendmsg(hello, "hello");

    sendmsg(hello, "free");

    exit( 0 );
}

Compilation with the library libso-o.a and execution:

$ gcc -O -c test-Hello.c
$ gcc -O -c Hello.c
$ gcc test-Hello.o Hello.o libso-o.a -o test-Hello
$ test-Hello
Hello from So-o!


Example
import { defclass } from 'So-o';

defclass('Hello', null, 1,
    null,
    null,
    null,
    {   'hello':    (self) => {
                        console.log('Hello from So-o!');
                       
                        return self;
                    }
    }
);

A few lines of code to illustrate how the Hello class is used:

import { sendmsg } from 'So-o';

import 'Hello';

var hello = sendmsg(Hello, 'new');

sendmsg(hello, 'hello');

Execution with Node.js:

$ ln Hello.js node_modules/Hello.mjs
$ ln testHello.js testHello.mjs
$ nodejs --experimental-modules testHello
Hello from So-o!

Visit so-o.org.

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [url] [email] strip help 2000

Improve the presentation of your text with the following formatting tags:

[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]commande[/code], [code=langage]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].

Enter a maximum of 2000 characters.

By