Skip to content

Instantly share code, notes, and snippets.

@eignnx
Created April 9, 2020 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eignnx/2677205ed4ad77601de66f2d4cd227f0 to your computer and use it in GitHub Desktop.
Save eignnx/2677205ed4ad77601de66f2d4cd227f0 to your computer and use it in GitHub Desktop.
-- # Beginners Tutorial: Lesson 4
-- ### Data Variant Aliases
-- In this lesson you'll learn how to display data variants in user-friendly ways.
-- Say you wanted to write a story about a character.
data Character = val | sam | penny
rule story = name.Character "went to the store."
rule name.Character =
.val -> "val"
.sam -> "sam"
.penny -> "penny"
-- This will work, but it's kinda annoying that we have to write a whole separate rule just to be able to show a character's name.
-- Here's a more concise way to do it:
rule story2 = @Character "went to the store."
-- A data variant variable can be displayed by putting an `@` sign in front. The `story2` rule produces sentences like:
-- > "penny went to the store."
--
-- > "val went to the store."
--
-- > "sam went to the store."
-- By default, `@`-displaying a data variant displays the variants programmatic name, which must be all lower-case alphanumeric characters (A through Z). This means if one of your characters is called Björk, you would have to enter a data variant name like `bjork` (dropping the umlaut). So when `bjork` is `@`-displayed,
-- 1. the name wouldn't be capitalized, and
-- 2. the umlaut wouln't be displayed.
--
-- That's no good.
-- ## The Solution
-- You can tell the system how a data variant should be `@`-displayed by putting it's human-readable form in parentheses after the data variant declaration:
rule AnotherCharacter
= penny ("Penny")
| sam ("Sam")
| bjork ("Björk")
| val ("Val" | "Valarie" | "Val," val_title)
rule val_title = "Destroyer of Worlds" | "Ruler of this Hamlet" | "(may they live forever)"
-- Also notice that `val` can be given nicknames as well! You can even reference rules inside these custom display sentences!
-- Finally, note that you can print out a specific data variant just by putting an `@` in front of it's name:
rule specific_data_variant = "The artist formerly known as" @penny "(who now goes by Persephone)."
rule start
= "story:" story
| "story2:" story2
| "AnotherCharacter:" @AnotherCharacter
| "specific_data_variant:" specific_data_variant
-- # Next Lesson
-- [Click here to move on to Lesson 5](#)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment