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
Extend record punning to allow destructuring. #3
Conversation
I like the proposal. This syntax change is non-intrusive, intuitive, and would help writing complex pattern matching rules. I sure have some code that could benefit from it. |
Would be nice if this syntax was also able to fix cases such: type t = { x: int; y: int }
let f { x as t } = t.y So would be nice if we can rewrite your example to something which looks like: fun { { x ; y as p } ; q } -> e |
I have wanted this or something similar in the past when using nested records. If you change |
On 30 Jan 2014, at 14:14 pm, Thomas Gazagnaire notifications@github.com wrote:
I can see that it saves a few characters but it’s not worth it in my opinion. |
@hcarty: yes, location information and error messages are preserved.
|
@samoht: not bad, but you'd only save having to type an extra pair of parentheses then. The "as" syntax as such already saves you having to duplicate a (potentially long) field name. The "x as t" looks a little strange to me. I think I'd prefer having the binding outside of the enclosing structure as in the first proposal. |
Regarding my proposition, it's exactly the same as for tuples (which already works): let f (x, _ as t) = snd t |
@samoht: true, but that's because the tuple syntax, unlike records, allows you to drop parentheses in the first place. The enclosing parentheses here aren't required by the tuple, only by the "as"-binding. Update: to be precise, the tuple does require the parentheses here, because it's a function definition, but not e.g. in: let x, _ as t = ... |
@samoht wrote:
But that’s between parenthesis, not curly braces. # let f = function (`A | `B (_, _) as t) -> t;;
val f : [< `A | `B of 'a * 'b ] -> [> `A | `B of 'a * 'b ] = <fun> the "as t" takes everything of its left) |
I'm just proposing to have a consistent notation for tuple and records. But I see your point, so I stop arguing :-) |
I believe I’ve found a better and stronger reason: |
Another useful extension of the syntax for record patterns would be: { x.a = pa; x.b = pb; x.c; y.t = pt } equivalent to: { x = {a = pa; b = pb; c}; y = {t = pt} }; This would also be useful for record override: { r with x.a = ea; x.b = eb; x.c; y.t = et } equivalent to: {r with x = {r.x with a = ea; b = eb; c}; y = {r.y with t = et}} |
I'm on @pw374 's side. I don't like
It will be great if 2nd line will be parsed as equivalent to |
If I can indulge in a small meta note, I think community review works best for proposal that have a good chance of being consensual. As soon as something touches the language definition, the design space explodes and it's very hard to reach agreement (whether in small or larger groups) -- it's one of the reason why maintainers are generally so reluctant with syntax changes. While I don't think Jeremy's initial suggestion was unreasonable, I'll personally prudently avoid trying to obtain an actual decision on this, at least for now. |
On 31 Jan 2014, at 16:50, @gasche wrote:
I do like @yallop's initial suggestion. I believe no one on this thread has shown to be against it. It’s a tiny patch (only 2 reasonable new lines in |
Let me add some support for the first proposal, too. I think it at least deserves a short discussion. |
I kind of wonder whether this change (the initial proposal) is worthwhile. |
@garrigue : couldn't we fix this by interpreting |
@gasche I've always wanted to write |
git-svn-id: http://caml.inria.fr/svn/ocaml/version/4.02@15985 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
(Merge of branch cmm-mach-types and PR#115.) - Register type "Addr" is split into . "Val" (well-formed OCaml values, appropriate as GC roots) . "Addr" (derived pointers within the heap, must not survive a GC) - memory_chunk "Word" is split into . "Word_val" (OCaml value) . "Word_int" (native-sized integer, not a pointer into the heap) Cmmgen was updated to use Word_val or Word_int as appropriate. Application #1: fail at compile-time if a derived pointer within the heap survives a GC point (cf. PR#6484). Application #2: CSE can do a better job across allocation points (keep factoring expressions of type Int, Val, Float, but not Addr). Application #3: slightly fewer roots given to the GC (e.g. pointers into bigarray data). git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@16269 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
Fix assertion failure for 'effect E _ ->'
Add Gc.print_stat hook to std_exit.ml
We discussed this PR at the developer meeting yesterday, and there was opposition (not from me) to including it without a patch allowing to write at least (We have no guarantees, of course, that a proposal including this richer syntax would be accepted. I would personally support¹ ¹: another thing I would like to have is for the individual patterns in |
Thanks for the feedback. I'll investigate supporting the more general syntax.
I'd like to have that as well. The old patterns Camlp4 extension used to support that behaviour, IIRC. Even more ambitiously, it'd be good to have something similar for GADTs, if they're ever updated to support |
fun { { x ; y } as p; q } -> e is equivalent to fun { p = { x ; y } as p; q } -> e
Avoid deleting instrument.mlp
Use the new file location for emit.mlp files
* Code compiles and links successfully
add internationalisation support
Embed HTML files to serve the documentation
This patch extends the record punning syntax (b01621e) to allow simultaneous label punning and destructuring. Variables bound using
as
at the top level of a field pattern are treated as labels. For example, it allows you to writewhich is equivalent to