Skip to content

Abstract def class methods (e.g. constructors) #5956

Open
@obskyr

Description

At the moment, there seems to be no way to abstract def class methods. This is a bit of a problem, in case you want to guarantee subclasses implement certain constructors! Take this for example:

abstract class Thing
  abstract def self.parse(s : String) : Thing
  abstract def self.construct_somehow(a : Int, b : String, c : Bool) : Thing
end

Right now, that doesn't work, but instead errors out with the error can't define abstract def on metaclass. The best way I can come up with to do something similar in the current version of Crystal is this:

abstract class Thing
  abstract def self.parse(s : String) : Thing
    raise NotImplementedError.new # Or similar
    new
  end

  abstract def self.construct_somehow(a : Int, b : String, c : Bool) : Thing
    raise NotImplementedError.new
    new
  end
end

...But that's runtime (and boilerplate-ey).

I hope there's something I'm missing!

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions