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
- Proxy annotated objects with Guice
- Using Dialyzer with an Elixir Mix Project
- Efficient Timer using a Circular Buffer
- Circuit Breakers and retries in Scala with autobreaker
- Scala Projects and Integration Tests
- Secure Configuration Management for Microservices
- Blockchains as Part of the IoT Architecture
- Services Version Lock with Docker and Jenkins