-
Notifications
You must be signed in to change notification settings - Fork 29
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
Fix syntax #62
Comments
type%import t = Foo.bla [@@deriving sexp,yojson]
type%import t = Foo.bla
@gasche has pointed out here (in 3.) what I meant with "
The end of the sentence captures exactly what I want to say: the expansion of the node spreads onto the rest of the tree. From what I understand, extension nodes are meant to be expanded (as nodes), not to expand the rest of the tree. |
The proposed syntax is okay, and with the |
Outside ppxlib, there are no strict requirements on how extension nodes should be interpreted by processors. There are also several good reasons why the interpretation of an extension node may sometimes want to leak outside its initial AST placement. Consider for example an extension that expands to an expensive computation that does not depend on variables in its lexical scope, and only needs to be done once per program execution. You want
to generate something like
One way to work around this issue is to require that such extension nodes are themselves embedded into larger extensions that span a larger scope than the possible rewriting. So one would have to write
and have the interpretation of the outer extension do the job of lifting the fragments to the toplevel. However this approach:
Remark: this situation I describe is not the situation of ppx_import, for which we may reasonably give a local semantics by just increasing the scope slightly. However it does suggest that supporting non-local expansions is useful/important in the general case. |
Ok, I see your point. Thanks for the example! So I take back my general statement about extension nodes only meant to be local in general. Maybe we can agree on the following? For performance and for predictability, extension nodes ideally don't leak outside their original AST placement unless it's in their nature to do so. It is not in the nature of |
The latter.
That's because if you write |
Yes, that sounds perfectly right to me. (Note that, to someone who is not aware of the current AST structure, it is not clear that the extension handling is non-local: for all they can see, I think that the approach that you propose is nicer than the current one. (Especially now that I'm un-confused about the way attributes would be handled; thanks!). But it is not clear to me that the change is worth breaking all user code. Few things are, and I don't know that this particular case has benefits that outweigh the costs, except of course for the rather directly obvious benefit "it pleases ppxlib people, who maintain the whole ppx infrastructure in practice". |
Another important benefit would be the improvement in performance!
I agree that usually the cost of a breaking change is high and therefore there must be really good reasons to accept that cost. But take into account that in this particular case the cost of the breaking change is quite low since upgrading to the new version would really just be as simple as running a command along the lines
one time. |
IMVHO, the new syntax has several advantages, and given that there are only 10 rev deps in opam for ppx_import I think it'd be reasonable to deprecate it and remove it in a 2.0 version. Personally, I am a fan of this kind of "little" improvements which make maintenance easier. On their own may not seem much, but in the wider context they do really add up. |
With the syntax
type t = [%import: Foo.bla]
, the[%import: Foo.bla]
is written at a place where the OCaml parser expects a core type extension node. Butppx_import
doesn't only want to inject a core type (ptype_manifest
) into the corresponding type declaration. It also wants to inject the concrete type definition (ptype_kind
), which breaks with the idea of an extension node being local.To express
ppx_import
in an OCaml AST conform syntax, it would have to be written as a extension node at the level of a structure item. Derivers such assexp
oryojson
would just be packed into the payload. That would look as follows:or, expressed in a nicer way:
That syntax would be conform with the OCaml AST and, as a nice "side-effect", it would also be convenient from a source-code point of view.
Of course, releasing that syntax change would break all users. But it doesn't break the semantics or features, it only breaks the syntax and so one could use a simple
sed
command on a project in order to upgrade to the newppx_import
. You could also prepare for the breaking change by making a 1.x release allowing both syntaxes, but deprecating the old one.What do you think about the proposed syntax in general? And what do you think about introducing it in a major release?
(cc @NathanReb since we've been talking about this)
The text was updated successfully, but these errors were encountered: