pjs
pjs

Reputation: 19855

Can modules be used as type restrictions?

I'm trying to implement a generic priority queue container class where the elements to be stored must have an ordering property. Right now I have

class PriorityQueue(T)
    ...
end

This works when I create PriorityQueue(Int32), PriorityQueue(String), or PriorityQueue(Foo) where I implement Foo to include Comparable(Foo), but I'm wondering if there's a way for the PriorityQueue class to declare that T's must be Comparable.

Upvotes: 0

Views: 69

Answers (1)

Jonne Haß
Jonne Haß

Reputation: 4857

No, Crystal currently (as of 0.31) does not support restricting the types of free vars. This feature is being discussed at https://github.com/crystal-lang/crystal/issues/934

In the meantime as you noted it still works by using the Ruby style of expecting an interface by just calling the methods you need, it's just that the error messages produced by that can be a bit hard to understand.

Upvotes: 2

Related Questions