Crystal: abstract static method
Is it possible to define abstract static methods? I'm trying: abstract struct MyStruct abstract def self.myfun # ERR abstract def MyStruct::myfun # ERR end
Abstract class methods don't appear to be a language feature: abstract class Abstract abstract self.doit end # => Syntax error in /home/bmiller/test.cr:23: unexpected token: self However you could always delegate to an instance: abstract class Parent def self.instance ##instance ||= new end def self.doit instance.doit end abstract def doit end class Child < Parent def doit "ok" end end p Parent.doit # => can't instantiate abstract class Parent p Child.doit # => "ok"
I faced the same problem and figured out a (in my opinion) nicer solution: abstract class Something module ClassMethods abstract def some_abstract_class_method end extend ClassMethods abstract def some_abstract_instance_method end The documentation mentions that module methods can be made abstract as well, so this builds on top of that. Implementing this class without implementing the class method some_abstract_class_method will raise an error, as expected.
Type checking in Crystal
Condition over object's type
Crystal C bindings: argument const unsigned char **
Crystal C bindings: return type char*
Make Class member attribute of type Array(T) accept 2D arrays of T
JSON.mapping if “root” attribute has inherit keys
Crystal C bindings: using fopen
Define Class object validations
Set attribute of type “Class” to define it as Int32, String, Float64
Set both default attribute and type of attribute
How can I test private methods?
Write binary data to stdout in Crystal
Unable to compile after upgrade from crystal 0.19.4 to 0.20
Application server for Crystal web app / Kemal
Crystal Fiber error - “Unhandled exception in spawn: SSL_shutdown: Operation now in progress (Errno)”
Convert hex string to int in Crystal