Search code examples
modulestatic-methodscrystal-lang

Has Crystal got static methods?


Is it possible to do static methods in modules as in Ruby ?

module Test
    self.def test
        puts "test"
    end
end
Test::test

I get a expecting token 'EOF', not 'end' if the call is in the same file ( as shown in the exemple ) and a expecting token 'CONST', not 'test' if I place the call in a different file.

What am I doig wrong ? Is there static methods in modules in Crystal ?


Solution

  • The correct syntax for class methods is def self.test, not self.def test. Class methods are called using Test.test, not Test::test.