Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate string format with scala in compile time

Tags:

scala

My function has 1 parameter and type is string but lenght is 4, Can I validate this parameter in compile time?

In haskell and F# have type level and it can validation in compile time, like nonEmptyList.

How to make it in scala. I think shapless can do this but I don't understand

Thank you for advance suggestion

like image 769
Veerapat Boonvanich Avatar asked Dec 03 '25 08:12

Veerapat Boonvanich


1 Answers

Yes, Shapeless can do this. Perhaps something like this:

def f(s: Sized[IndexedSeq[Char], Nat._4]): ...

You wouldn't be able to pass strings directly to this, though. You'd have to do something like f(Sized('a', 'b', 'c', 'd'))

like image 127
Joe K Avatar answered Dec 05 '25 23:12

Joe K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!