<< Previous | Next >>

Hotel and Objects

I'm getting closer to finally implementing and expirementing with the features that set hotel apart: its objects, modules and closures with importing. Just today I made the following Queue implementation work:

fun Queue() {
    var _content
    
    fun fragment(v, prev) {
        return self
    }

    fun is_empty() { 
        return _content == null
    }
    
    fun size() {
        fun size(c) {
            if (c == null) return 0
            return 1 + size(c.prev)
        }
        return size(_content)
    }
    
    fun push(v) {
        _content = fragment(v, _content)
    }
    
    fun pop() {
        var r = _content
        _content = r.prev
        return r.v
    }
    return self
}

var q = Queue()

q.push("world")
q.push("hello")
print(q.size())
print(q.pop(), q.pop())
print(q.is_empty())

A few things that are next on the list:

def Queue() {
    def _content
    def _fragment(v, prev)

    def push(v) _content = _fragment v, _content
    def pop() {
        if content == null: return null

        def r = _content; _content = r.prev
        r.v
     }
     def size() {
        fun size(c) if not c: null else 1 + size(c.prev)
        size _content
     }
     def empty() _content == null

     self
}

Most notably: implicit returns; a colon after if and other such statements; and optional braces and curly braces for things like function calling, function body definition and if statements and others.

Also, not so clear here, is the semicolon, which is not a statement separator, but a statement continuation. Which is almost the same, except a semicolon turns two statements into one larger statement. A newline is a statement separator.

Notice the upgrade of newline to a full statement separator, which is my evil plan for forcing curly braces to be on the same line as the function declaration or if statement!

Last modified: 2007-11-19 20:18 GMT