Skip to content

Instantly share code, notes, and snippets.

@copygirl
Created March 10, 2018 21:59
Show Gist options
  • Select an option

  • Save copygirl/cd6a0209e3baf13629c47766a555d5d5 to your computer and use it in GitHub Desktop.

Select an option

Save copygirl/cd6a0209e3baf13629c47766a555d5d5 to your computer and use it in GitHub Desktop.
// In theory, it may be possible to define the ->
// "operator" using the language's existing features:
`->` :: argument -> body -> {{
it :: {argument}
{body}
}}
// # Explanation #
//
// Creates an "incomplete" function, which takes `argument` and returns
// another function which takes `body` that creates a new scope with an
// "it" constant set to the argument (or a value copy thereof), followed
// by the body.
//
// - `` `->` `` means "the symbol `->`". Special characters
// shouldn't be valid on their own in that location.
//
// - `::` means "define constant".
//
// - `{{}}` is templating syntax or something.
//
// - `{}` is syntax for inserting the source code, AST
// node or similar of the arguments into the template.
//
//
// ## Other notes ##
//
// - The body of the function would inherit `it` into its scope, though since
// it can have its own scope, it could be hidden using a re-declaration.
//
// - I suppose it makes sense to have special symbol functions (as opposed to
// regular named functions) be postfix by default rather than prefix.
// But a compiler directory such as `{.prefix.}` could affect this.
// Similarly, something like `{.precedence = x.}` might be required.
//
// - With this setup, even `::` could be represented in-language as some
// magic function:
`::` :: (symbol: ASTSymbol) -> (value: ASTValue) -> ...
foo :: a -> ...
// ..expands into..
foo :: a -> body -> {{
it :: a
{body}
}} ...
// ..which expands into..
foo :: a -> (
it :: a
...
)
// ..and if "a" is a constant, it could even be simplified into:
foo :: (
it :: a
...
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment