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

Record types #245

Open
andrewbaxter opened this issue Mar 6, 2024 · 4 comments
Open

Record types #245

andrewbaxter opened this issue Mar 6, 2024 · 4 comments

Comments

@andrewbaxter
Copy link

Using tuples everywhere relies on positioning to correlate symbols which is risky (mix up the order of two symbols and you can end up with strange results).

Relations kind of support a record syntax, but this isn't supported anywhere else.

I'd like:

  • Record rules, like ?{something: a, something_else: b} := .... (Also this would solve the issue that right now there's no way to rename results other than writing a new rule that just forwards the elements with a new name, which is excessive and makes queries more fragile)
  • Record matching for non-relations, like := my_rule{a: a, b: b}

I assume the framework for this is already there, since results already have headers etc.

And maybe

  • Record values, like y = {a: 4, b: something}
  • Record methods, like get(y, "a")
@creatorrr
Copy link
Contributor

Curious. What do you mean by:

Record matching for non-relations, like := my_rule{a: a, b: b}

isn't this already supported? Also, could you use json objects in place of records? It won't be pretty but I guess it'll have similar semantics.

@andrewbaxter
Copy link
Author

My brain cache has been purged, but I think it was something like this:

{
stuff[x,y] <- [[1, 2]]
?[y] := stuff{y: 2, x: y}
}

This gives a syntax error. It would be equivalent to stuff[y, 2] but more explicit.

  × The query parser has encountered unexpected input / end of input at 38..38
   ╭─[2:1]
 2 │ stuff[x,y] <- [[1, 2]]
 3 │ ?[y] := stuff{y: 2, x: y}
   ·              ▲
 4 │ }
   ╰────

@andrewbaxter
Copy link
Author

You're right, JSON might work. This works for instance:

{
stuff[x] <- [[json({"x": 1, "y": 2})]]
?[y] := stuff[x], y = get(x, "x"), get(x, "y") == 2
}

And you can effectively name output parameters

{
stuff[x] <- [[json({"x": 1, "y": 2})]]
?[res] := stuff[x], y = get(x, "x"), get(x, "y") == 2, res = json({"y": get(x, "x")})
}

which I think checks all my boxes above.

If I were to push for having this natively instead of via json, I think the reasons are:

  • It makes the existing syntax more consistent ({} is supported in some places but not others)
  • Verbosity
  • Overhead of json conversions, json queries?

@creatorrr
Copy link
Contributor

There's definitely an overhead of json conversions and it can be error-prone for no real benefit other than syntax niceties. I do second this on second thought, it may make sense that records -> json mapping is easy and simply calling json({...}) on them works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants