Module as a type?

While searching for information on the notion of interface in Crystal Language, I came across this post in which @sija quotes the following example as the right way to define an interface in Crystal :

module RedisProtocol
  abstract def publish(channel, data)
end

class Redis
  include RedisProtocol
  # ...
end

class FakeRedis
  include RedisProtocol
  # ...
end

class MyClass
  @publisher : RedisProtocol
  # ...
end

I have a problem with the @publisher : RedisProtocol line in class MyClass: AFAIK, a module cannot be instantiated, and here it seems to define a type.

I would be very happy to have explanations about this syntax, which I don’t understand either the meaning or the way to use it.
Thank you.

1 Like

If you think of it like an interface in other langs, which you can’t instantiate either, it’s basically saying @publisher allows anything that implements (includes) that interface (module). E.g. a Redis or FakeRedis instance.

2 Likes

Ah, got it!
Thanks

https://crystal-lang.org/reference/syntax_and_semantics/modules.html