Skip to content

Add syntax support for static methods

Yorick Peterse requested to merge static-methods into master

This allows one to define a static method using the "static" keyword like so:

object Something {
  static def new -> Something {
    ...
  }
}

While Inko already has module methods, static methods are useful when acting as factory methods: methods that just return an instance of the type they are defined on. For a while I wanted to use module methods for this, but this can lead to unpleasant APIs. For example, the std::time module offers a few module methods like so:

import std::time

time.now
time.utc

I'm not a fan of this setup, as it means having to import both the module (std::time) and the appropriate types (e.g. SystemTime) in certain cases. Static methods allow you to write the following instead:

import std::time::SystemTime

SystemTime.now
SystemTime.utc

The compiler currently does not perform any validation to make sure static methods are called in the right context. This is the result of the Ruby compiler being too much of a hack to implement this properly. Instead, we only add syntax support for now and will add validation for this in the self hosting compiler.

Merge request reports