# Crystal has Interfaces in Java sense

**URL:** <https://forum.crystal-lang.org/t/crystal-has-interfaces-in-java-sense/5105>\
**Category:** Crystal Contrib\
**Created:** [November 10, 2022, 8:22am UTC](https://forum.crystal-lang.org/t/crystal-has-interfaces-in-java-sense/5105 "2022-11-10T08:22:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![funny-falcon](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/funny-falcon/32/159_2.png) [@funny-falcon](https://forum.crystal-lang.org/u/funny-falcon)\
**Post date:** [November 10, 2022, 8:22am UTC](https://forum.crystal-lang.org/t/crystal-has-interfaces-in-java-sense/5105/1 "2022-11-10T08:22:59Z")

</div>

During discussion we’ve discovered Crystal has interfaces in Java sense.

Module can have abstract method, and its inclusion demands implementation:  
[https://play.crystal-lang.org/#/r/e1jl](https://play.crystal-lang.org/#/r/e1jl)

```auto
module Moo
  abstract def size : Int
end
 
struct Fix
  include Moo
end
# Error: abstract `def Moo#size()` must be implemented by Fix

```

Containers could be specialized to modules, function/method could have argument restricted to module, and their call is correctly polymorphic:  
[https://play.crystal-lang.org/#/r/e1jq](https://play.crystal-lang.org/#/r/e1jq)

```auto
module Moo
  abstract def size : Int
end
 
class Fix2
  include Moo
  def size : Int; 2; end
end
 
class Fix3
  include Moo
  def size : Int; 3; end
end
 
def say(s : Moo)
  puts "size is #{s.size}"
end
 
say Fix2.new
say Fix3.new
moos = Array(Moo){Fix2.new, Fix3.new}
moos.each{|f| say f}
[Fix2.new, Fix3.new].each{|f| say f}
# size is 2
# size is 3
# size is 2
# size is 3
# size is 2
# size is 3

```

---

<div class="post-metadata">

**Author:** ![funny-falcon](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/funny-falcon/32/159_2.png) [@funny-falcon](https://forum.crystal-lang.org/u/funny-falcon)\
**Post date:** [November 10, 2022, 8:39am UTC](https://forum.crystal-lang.org/t/crystal-has-interfaces-in-java-sense/5105/2 "2022-11-10T08:39:40Z")

</div>

I think it should be mentioned in tutorial/reference.

---

<div class="post-metadata">

**Author:** ![funny-falcon](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/funny-falcon/32/159_2.png) [@funny-falcon](https://forum.crystal-lang.org/u/funny-falcon)\
**Post date:** [November 10, 2022, 8:45am UTC](https://forum.crystal-lang.org/t/crystal-has-interfaces-in-java-sense/5105/3 "2022-11-10T08:45:31Z")

</div>

Related Github discussion [Go Language like interfaces? · Issue #3489 · crystal-lang/crystal · GitHub](https://github.com/crystal-lang/crystal/issues/3489)

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [November 10, 2022, 10:36am UTC](https://forum.crystal-lang.org/t/crystal-has-interfaces-in-java-sense/5105/4 "2022-11-10T10:36:23Z")

</div>

Yeah, I suppose this specific use case should be explicit on [Modules - Crystal](https://crystal-lang.org/reference/1.6/syntax_and_semantics/modules.html)

Currently there is just this sentence at the end of [Virtual and abstract types - Crystal](https://crystal-lang.org/reference/1.6/syntax_and_semantics/virtual_and_abstract_types.html)

> Abstract methods can also be defined in modules, and the compiler will check that including types implement them.

---

<div class="post-metadata">

**Author:** ![funny-falcon](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/funny-falcon/32/159_2.png) [@funny-falcon](https://forum.crystal-lang.org/u/funny-falcon)\
**Post date:** [November 10, 2022, 4:01pm UTC](https://forum.crystal-lang.org/t/crystal-has-interfaces-in-java-sense/5105/5 "2022-11-10T16:01:41Z")

</div>

Here’s some “Structural” interface typing: almost as in Go, but with explicit interface initiation  
[https://play.crystal-lang.org/#/r/e1rt](https://play.crystal-lang.org/#/r/e1rt)

Generator for wrapped methods should be extended for splat, kwargs and block.  
And I don’t know how to properly hide Wrapper from occasional usage.  
But otherwise looks promising.

---

<div class="post-metadata">

**Author:** ![funny-falcon](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/funny-falcon/32/159_2.png) [@funny-falcon](https://forum.crystal-lang.org/u/funny-falcon)\
**Post date:** [November 10, 2022, 5:20pm UTC](https://forum.crystal-lang.org/t/crystal-has-interfaces-in-java-sense/5105/6 "2022-11-10T17:20:21Z")

</div>

And a bit “improved” version (ie with macro “interface”)
