Skip to content

Feature request: { foo bar } style records syntax #1766

Description

@taoeffect

In Janet, it can be very verbose to call functions that have a large number of keyword-based arguments. In JS this is somewhat simplified by the ability to write objects like so: { foo, bar, baz: 5 } which gets expanded to { foo: foo, bar: bar, baz: 5 }

It would be awesome if we had this for tables and structs in Janet too so that we could write { x y z } instead of { :x x :y y :z z }

In the meantime I'm living with this $: macro:

(defmacro $: [& args]
  (var pairs @[])
  (var i 0)

  (while (< i (length args))
    (let [x (args i)]
      (if (keyword? x)
        # Explicit key/value: :baz bazz
        (do
          (when (>= (+ i 1) (length args))
            (error (string/format "$: missing value for keyword %j" x)))
          (array/push pairs x)
          (array/push pairs (args (+ i 1)))
          (set i (+ i 2)))

        # Shorthand symbol: foo => :foo foo
        (do
          (when (not (symbol? x))
            (error (string/format "$: expected symbol or keyword, got %j" x)))
          (array/push pairs (keyword x))
          (array/push pairs x)
          (set i (+ i 1))))))

  ~(table ,;pairs))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions