archives

Namespaces for methods?

I'm working on adding namespaces to messages in my programming language. I've run into a few... strange... side-effects of it that I'd appreciate some feedback on.

The motivation here is that since classes are open to extension, namespaces will let you add methods to existing classes while avoiding collisions. For example:

// In document.mag:
class Document
    // Stuff...
end

// In render.mag:
def Document print()
    // Code to draw the document on-screen...
end

// In printer.mag:
def Document print()
    // Code to send the document to a printer...
end

// Later...
var document = Document new()
document print() // Which one?

Here, the two print methods collide, which is bad. To fix that, you can place the methods in namespaces. Dots are used to separate namespace prefixes (and are not used for method invocation, which just uses juxtaposition). We can disambiguate the above by doing:

// In render.mag:
def Document render.print()
    // Code to draw the document on-screen...
end

// In printer.mag:
def Document printer.print()
    // Code to send the document to a printer...
end

You can select a specific print method by either fully-qualifying it:

document render.print()

Or by importing the namespace:

using render
document print()

When it encounters a message like print it will search all of the namespaces imported with using until it finds a match. Right now, namespaces are searched in the opposite order that they are imported, and it dispatches to the first match. I may make it an error for there to be multiple matches.

So, here's where you come in (I hope). There's two consequences of this that feel wrong:

Dispatching a method is O(n) where n is the number of imported namespaces. Because the language is optionally-typed, there's no way to resolve the fully-qualified name at compile time. That means every dispatch will search the namespaces at runtime. I'm not too worried about performance, but it feels odd that we can't resolve the fully-qualified name once statically and stick to that.

The fully-qualified name that's resolved for a method may be different at runtime depending on the value of the receiver. This is the part that feels really strange. Consider:

class Foo
    def apple.method() "apple"
end

class Bar
    def banana.method() "banana"
end

def callMethod(obj)
    using apple
    using banana
    obj method()
end

callMethod(Foo new()) // "apple"
callMethod(Bar new()) // "banana"

At runtime, an unqualified name may resolve to a different namespace based on the method set of the receiver. That runs counter to my intuition of namespaces. I consider an unqualified name in a scope with an imported namespace to just be a shorthand for the one "real" fully-qualified name that it represents. This implementation doesn't work that way. Instead, an unqualified name with imported namespaces defines a set of possible names, any of which are potentially valid based on the receiver.

That's a valid semantic, but one that feels unusual to me.

So, questions:

  1. If a name is found in multiple namespaces, should it have a resolution order, or should that be an error? I'm leaning towards error, but I'm interested in hearing other opinions.
  2. Does it seem reasonable to have different objects resolve to different methods when given an unqualified name, or is that just a trap for unwary users?
  3. Any other pitfalls or landmines you can see that I'm not aware of?
  4. Any pointers to papers on related systems? I read the classbox paper which was interesting but didn't really go very far down the rabbithole. I'm not an academic so I don't know much about doing research to find papers related to a topic (a skill I'm rapidly needing to acquire). Pointers here would be super awesome.

Thanks!

Schemas for JSON?

I'm seeing a big gap in the world right now in re: schemas for JSON.

In particular, it appears to me that a large fraction of the world (well, okay, of the people who are encoding data in language-neutral ways) has the wrong impression about JSON, believing that its fundamental difference from XML is its simplicity, when in fact (I claim) JSON's raison d'etre is the fact that it didn't start life as a markup language.

If you believe me, then in fact schemas for JSON values are in fact extremely valuable. I see one proposal on the web, but my brief reading of it suggests that we can perhaps do better. Is there a bunch of work on this that I'm just not aware of?

Apologies in advance for offending anyone, or for failing to be aware of obvious answers to my question.

Thanks!