operator overloads in types #583

Closed
nddrylliog opened this Issue Feb 14, 2013 · 2 comments

Projects

None yet

2 participants

@nddrylliog
Member

ie. be able to write that:

MyArray: cover template <T> {
    length: Int
    data: T*

    init: func@ (=length) {
        data = gc_malloc(T size * length)
    }

    get: func (index: Int) -> T {
        _checkIndex(index)
        data[index]
    }

    set: func@ (index: Int, value: T) {
        _checkIndex(index)
        data[index] = value
    }

    _checkIndex: func (index: Int) {
        if (index < 0 || index >= length) {
            Exception new("Out of bounds array access: %d should be in %d..%d" \
                format(index, 0, length)) throw()
        }
    }

    operator [] (i: Int) -> T {
        get(i)
    }

    operator []= (i: Int, v: T) {
        set(i, v)
    }
}

It'll also fix weird things like: using a lib that returns a List, and then you try to index it with [] but it doesn't work because you don't import ArrayList/List yourself.

@shamanas
Collaborator

Cough cough #33 cough cough

@nddrylliog
Member

Closed in 6ec80f5

@nddrylliog nddrylliog closed this Feb 14, 2013
@nddrylliog nddrylliog added a commit that referenced this issue Feb 14, 2013
@nddrylliog nddrylliog Make all other operators work in-class. cf. #583 (or #33 for that mat…
…ter). Still no inheritance.. will see about that laterz.
ba0432d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment