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

Allow reference to polymorphic variants when possible #1115

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

panglesd
Copy link
Collaborator

A break from the 3.0 design!

Fixes #907 by allowing references to polymorphic variants when the type is given in the reference and the type's manifest is a polymorphic variant.

The ` in the constructor name is allowed and not mandatory.

References without specifying the parent type were not requested in #907 and could be added in another PR.

The last commits illustrate a problem: the polymorphic variants do not have an ID, so the resolved references for polymorphic variants are turned into a normal constructor identifier. This is not ideal.
If needed, the model could be modified to represent for "types whose manifest is a single polymorphic variant" in a way that allows to define ids to the polymorphic variants.
That is a bigger refactoring, but I can do it if you think that is needed.

type switch = [ `On | `Off ]

(**

   References with type as parent work:
   - {!type-switch.On}
   - {!type-switch.`Off}
   - {!type-switch.constructor-On}
   - {!type-switch.constructor-`Off}
   - {!switch.On}
   - {!switch.`Off}
   - {!switch.constructor-On}
   - {!switch.constructor-`Off}

   References in the environment don't work:
   - {!On}
   - {!`On}
   - {!constructor-On}
   - {!constructor-`On}

*)

Signed-off-by: Paul-Elliot <peada@free.fr>
Only when the type declaration has a manifest directly of polymorphic variant
definition:

```
type t = [ `A | `B]
```

References must mention the type, but not necessarily include the `` ` ``.

Signed-off-by: Paul-Elliot <peada@free.fr>
(* This is needed to ensure that references to polymorphic constructors have
links that use the right suffix: those resolved references are turned into
_constructor_ identifiers. *)
let suffix_for_constructor x = x
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Polymorphic constructors do not have an ID. When turning a resolved polymorphic constructor reference to an ID, a normal constructor ID is used.
But then, we need to ensure that this id, when turned to an href with anchor, does correspond link to the right place (that is, the page exists (which is always the case but also the html anchor exists).
Anchors are generated in Url.Anchor.polymorphic_variant, more precisely here. This function just tries to "force" the target anchor in IDs for constructor and the anchor for polymorphic constructor to correspond. I can remove it and replace it by a more detailed comment.

I can also add IDs to polymorphic constructors that can be referenced, that means changes to Lang but maybe it is worth it.

@jonludlam
Copy link
Member

FYI I'm working in this area right now (looking at Names & associated issues), so I'll have a more concrete view on this when I'm done :-)

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

Successfully merging this pull request may close these issues.

Reference a polymorphic variant constructor belonging to a type
2 participants