Scala has two types that can be used to express types constraints between two types:
<:<. The former is used to check equality between two types, and the later that a type A is a subtype of B.
There is some interesting topic on StackOverflow demonstrating how these can be used.
Just as an example, here are a few things you might be able to do:
You can also check using abstract types:
Alternatively, when limiting the usage of a method for just a given(s) type, you might use the same mechanism used on Scala’s collections, i.e.
sum’s signature is the following:
With that, a specific implicit
Numeric implementation for the desired type is injected. If there is no implementation for a given type, it will cause a compilation error. On the other hand, whenever you want to support a new type, you just need to expose a new implicit implementation for it. For instance:
- Boilerplate-free Typed Settings generation with Scala Macros
- O(1) LFU Cache in Scala
- Efficient Timer using a Circular Buffer
- Proxy annotated objects with Guice
- Using Dialyzer with an Elixir Mix Project
- Circuit Breakers and retries in Scala with autobreaker
- Services Version Lock with Docker and Jenkins
- Erlang, Elixir and a simple Supervisor
- Problems with Branches per Environment
- Automatically Build and Deploy the Blog