mfichman / jogo

High(er) performance compiled programming language with clean, powerful syntax
MIT License
6 stars 1 forks source link

Functors #49

Closed mfichman closed 11 years ago

mfichman commented 11 years ago

Basic idea:

JsonOutput < Functor {
    @dispatch(obj Starship)
    @dispatch(obj Asteroid)
}

Render < Functor {
    @dispatch(obj Mesh)
    @dispatch(obj Particles)
    @dispatch(obj Quads)
    @dispatch(obj Material)
    @dispatch(obj Light)
}
mfichman commented 11 years ago

To make this work, a special "hidden" interface must be created by the compiler:

Render < Functor {
    @case(obj Mesh)
    @case(obj Particles)
    @call(intf _RenderIntf)
    # Anything that doesn't match is ignored
}

_RenderIntf < Interface {
    Render__apply(f Render)
    # In this case, 'self' actually referse to the RenderIntf so we will need to 
    # swap 'self' and 'obj'
}

V-Tables are extended w/ the apply function:

Mesh_vtable:
   Mesh_Render__apply # Found at the index for Render__apply
mfichman commented 11 years ago

As an optimization, the Render__call function can use a switch-based if-else chain instead of doing an indirection through the object's vtable. This may actually be good enough for an initial implementation.

mfichman commented 11 years ago

Functors should be "open" types, i.e., the following should be allowed:

Render < Functor {
   @case(obj Mesh)
   @case(obj Particles)
}

... different source file ...

Render < Functor {
    @case(obj Material)
}

Under this implementation, the @call method of the functor must be generated in the main executable, b/c only then will all the @case methods be known.