Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extensible Matching via Implicits #529

Open
TimWhiting opened this issue May 21, 2024 · 0 comments
Open

Extensible Matching via Implicits #529

TimWhiting opened this issue May 21, 2024 · 0 comments

Comments

@TimWhiting
Copy link
Collaborator

TimWhiting commented May 21, 2024

It might be occasionally useful to extend the matching of Koka. Specifically I'm thinking about for slices. There is no need to convert the slice to a string to see if a slice matches a string, but currently that is what you need to do.

Instead it would be nice to do something like:

slice/match someSlice
  "" -> "empty"
  "hello" -> "world!"

Where slice match is defined as

pub extern slice/match(sl: sslice, s: string): bool
   ... Extern code to match a slice to a string

It basically would desugar to a sequence of if/elif, with else for a catch-all, just like the current match & since the pattern language doesn't change it still allows for exhaustiveness checking.

Of course this might be tricky when dealing with nested patterns (such as when one of your constructors contains a slice). Maybe instead of explicitly calling slice/match Koka just looks up a match function with the appropriate arguments:

match listofslice
  Cons("hello", Cons("world")) -> "Hi!"
  _ -> "Whatever"

In this case the match desugars to:

if listofslice.is-cons && slice/match(listofslice.head, "hello") && 
      listofslice.tail.is-cons &&  slice/match(listofslice.tail.head, "world") then 
  "Hi" 
else "Whatever"

And exhaustiveness checking still does not need to change.

@TimWhiting TimWhiting changed the title Implicit Matching Extensible Matching via Implicits May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant