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

Named matching of struct patterns using keyword arguments #3

Closed
sjoelund opened this issue Aug 17, 2018 · 5 comments
Closed

Named matching of struct patterns using keyword arguments #3

sjoelund opened this issue Aug 17, 2018 · 5 comments
Assignees

Comments

@sjoelund
Copy link
Contributor

I would like to perform matching based on the name of a field (because my structs have a very long list of field names and matching the correct name+pattern in much less error-prone especially if a field is added or removed from a large struct). Consider a small example below:

struct A
  a::Int
  b::Int
  c::Int
end

f(a) = @match a begin
  A(_,1,_) => 1
  A(b=2) => 1
end

I believe this should be possible with something like getfield(a, :b) == 2 at runtime; possibly with a check to avoid errors.

@mbravenboer
Copy link
Contributor

Just to confirm, this seems like a useful feature to me and also feasible to implement in the current macro implementation of Rematch.

@sjoelund
Copy link
Contributor Author

sjoelund commented Sep 8, 2018

Yes, it's possible and I also implemented it along with some features that probably shouldn't go into this package. I'm planning to automatically translate code in the MetaModelica language to Julia and introduced the matchcontinue construct (try next pattern on exception) and needed syntax for immutable lists. The code is at:
https://github.com/sjoelund/MetaModelica.jl/blob/master/src/matchcontinue.jl

There is some concern taken to optimize away assertions at compile-time although Julia couldn't quite remove all of them. I didn't have time to fully finish my work before the semester started and I got swamped with teaching.

@raileywild
Copy link
Contributor

@sjoelund I've picked up this task and would like to incorporate your solution. Are you willing to assign the copyright to RelationalAI? Thanks!

@sjoelund
Copy link
Contributor Author

Are you willing to assign the copyright to RelationalAI?

I hereby give you a license to incorporate the code in question as you please, including removing the OSMC copyright notice and making the derived work the copyright of RelationalAI.

@raileywild
Copy link
Contributor

Thanks @sjoelund !

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

3 participants