How to paramaterize Int as Ordered in scala

后端 未结 2 1541
旧巷少年郎
旧巷少年郎 2021-02-04 01:30

I have a class with a parameterized type that I want to do comparison operators on. I assmue I need to use the Ordered trait to achieve this but the compiler doesn\'t like my us

2条回答
  •  醉梦人生
    2021-02-04 02:26

    This occurs because Int is not a subclass of Ordered[Int] (see here why).

    However, there is an implicit coercion from Int to RichInt which is a subclass of Ordered[Int], but it isn't triggered for lower bounds. Use <% (view bounds) instead which will consider implicit coercions:

    class Test[T <% Ordered[T]]
    

提交回复
热议问题