reasonml / reasonml/reason

Syntax Proposal: Hash Tags - a way to think about polymorphic variants.

Open
#31 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

RFC
Dominant language
OCaml
Stars
10.3k
Forks
438
PR merge metrics
No merged PRs in 30d

Description

Okay, this admittedly of little consequence because I wouldn't really encourage the use of polymorphic variants for anyone but experts, but it might be a good idea, so I'll write it down:

First, some background on polymorphic variants.

  1. Both regular variants and polymorphic variants are both considered "tagged variants". Their names are kind of like name tags, and this is well established terminology.
  2. Polymorphic variants do not require type definitions even though you can chose to define types for them. Otherwise, you are free to just start using them like myVar = (HeyThere, Goodbye); without any type definition.
  3. They are still checked against pattern matching just like regular variants, so they are very type safe.

Now, much later on, I would also later suggest the following: Change the back tick to # and introduce them as "hash tags".

type regularVariant = {
   | CaseOne of argOne argTwo
   | Case Two
};

type someHashTags = {
   | #Yolo
   | #NoFilter of url
};

Now hear me out. I'm genuinely not trolling you all, and it's not just a random cheeky topical reference to social media. The way polymorphic variants work, is that their runtime representation is literally a hash of their tag text - this enables efficient separate compilation among other things. But there is also a parallel to common notion of "hash tags" today. Here's a summary of my justification.

  • Polymorphic variants are literally hashed tags. On that basis alone, it would not be a horrible name for them.
  • The single backtick is troublesome because in github/phabricator source comments, you can't express a single backtick without googling the special convention (I had to in order to figure out how to give the backtick examples at the very top).
  • They do have a conceptual link and similarity to "Hash tags" as used in social media, in that they aren't things that need to be predefined anywhere - they're both symbols that gain meaning simply by the act of being expressed. The fact that there is a conceptual parallel to a familiar concept, as well as a familiar visual/syntactical style makes them easy to remember and easier to introduce to people.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The proposal names no files, tests, or entry points. Review the existing polymorphic-variant syntax and compiler design before deciding whether replacing the backtick with # is accepted; done would require an agreed language change and corresponding implementation and tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
ocaml
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.