Crystal: Extending a module doesn't work for generics

Created on 9 Dec 2018  路  4Comments  路  Source: crystal-lang/crystal

This works fine (https://carc.in/#/r/5qsl):

module Foo
  def foo
  end
end

class String
  extend Foo
end

pp! String.foo # => nil

But this fails (https://carc.in/#/r/5qsm):

module Foo
  def foo
  end
end

class Array(T)
  extend Foo
end

pp! Array(Int32).foo
# undefined method 'foo' for Array(Int32).class

And also this (https://carc.in/#/r/5qsn):

module Foo(T)
  def foo
  end
end

class Array(T)
  extend Foo(T)
end

pp! Array(Int32).foo
# undefined method 'foo' for Array(Int32).class
bug compiler

Most helpful comment

All 4 comments

It "works", you just cannot specify a specific type when calling:

module Foo
  def foo
  end
end

class Array(T)
  extend Foo
end

pp! Array.foo #=> nil

In other words, the compiler error message is correct - there is no foo for Array(Int32)

Maybe the compiler should just ignore T, but it could be a breaking change if methods on specific generics were ever added (or similar mechanism).. I don't know what the plans are for generics though

Still a bug even with self:

module Foo(T)
  extend self

  def foo
    puts "foo"
  end
end

Foo(Int32).foo
undefined method 'foo' for Foo(Int32).class
Was this page helpful?
0 / 5 - 0 ratings