Thursday, July 26, 2012

More on Parameters

We have a lot of Suneido code where the class constructor simply sets members (i.e. instance variables or fields) from its parameters:
New(foo, bar)
    {
    .foo = foo
    .bar = bar
which is similar to what you'd do in Java:
class C {
    int foo;
    String bar;
    C(int foo, String bar) {
        this.foo = foo;
        this.bar = bar;
Scala has a nice shortcut for this:
class(foo: Int, bar: String) { ... }
For Suneido, I decided to allow:
New(.foo, .bar)
This would be equivalent to the first example. (with foo and bar still available as regular parameters as well)

You can also combine this with implicit dynamic parameters:
New(._foo)
So you'd end up with a foo member in the instance, that could either be passed explicitly, or could come from a dynamic variable set in one of the callers.

The way this is implemented, it will also work on regular methods, not just the constructor. I can't see a lot of use for that other than setters, but it was easier than restricting it.

I implemented this (along with dynamic implicit parameters) first in jSuneido, which was fairly straightforward, and then in cSuneido which was a little trickier but not too bad. (I have to say I prefer working on the Java version of Suneido these days. The code is cleaner, and the tools are better.) The changes are in version control on SourceForge.

No comments: