Short answer
No, It is Immpossible.
Types and implications are allowed at compile time, while the actual value of your String is a runtime object, that is, it can be different between runs. Therefore, at compile time, it is impossible to know which string value will be passed to the implicit function.
Long answer
It may be possible, but includes a huge amount of type magic, and it is definitely not a good solution in terms of readability and practicality.
Here is the idea: you can create your own type for your string and encode the necessary conditions in this type. For example, you will have AString[String[...]] for a string starting with "a", String[String[String[StringNil]]] for a 3 letter string, etc.
All string conversions will result in the corresponding types, for example, when you add String[...] with the letter A , you get AString[String[...]] , etc.
See dependent types and implementation of HList .
But then again, this is hardly practical in your case.
UPD Also consider the Refined project, which provides type-level predicates.
source share