runtime value pattern matching

HuskyNator huskynator at protonmail.ch
Fri Oct 11 17:29:37 UTC 2024


On Tuesday, 10 September 2024 at 21:10:49 UTC, monkyyy wrote:
> ```d
> switch(x,y){
>   case x=0 ..20: return 30;
>   case x=40..60,y=40..80: return 50;
>   case x=60..80,y=40..80: return 80;
>   case x=60..100: return 30;
>   default: return 0;
> }
> ```
>
> (note I understand theres better solution because toy problems 
> should avoid deeply entangled messes, the real world is less 
> kind; what if the `80` was `100`)
>
> You could *also* get some of the value of type pattern matching 
> with value pattern matching
>
> ```d
> void foo(nullable!int a,nullable!int b){
>   switch(a.isnull,b.isnull,a.forceget,b.forceget){
>     case a.isnull: throw(...);
>     case b.isnull: throw(...);
>     ...
> ```

I am aware this is somewhat different, but this reminds me a lot 
of the scope guards in scala's pattern matching (which is pure 
gold). I'd really advise anyone to dive into scala's case-class 
pattern matching: 
https://docs.scala-lang.org/tour/pattern-matching.html#matching-on-case-classes

I would be a serious proponent of adding language support for 
scala-like pattern matching on structs/"tuples".


More information about the dip.ideas mailing list