Skip to content

Should it be possible to deconstruct structs? #346

@popematt

Description

@popematt

There is no way to deconstruct a struct value or access the field names of a struct, which means that there is no way for a user to create a macro that can do something like this:

(:foo 1 2 3 { 'api-version':2, 'client-request-id': "yF8q3pZKRZ3dDfzaNy6N" })
=>
{
  foo: 1,
  bar: 2,
  baz: 3,
  'X-api-version': 2,
  'X-client-request-id': "yF8q3pZKRZ3dDfzaNy6N"
}

Hypothetically, one might be able to allow flatten to flatten a struct into pairs of field names and values, and then add some syntax to for that can bind to both the field name and the value, enabling the creation of a macro like this:

(macro foo (foo bar baz additional_properties)
       (make_struct
         ((.literal foo) foo)
         ((.literal bar) bar)
         ((.literal baz) baz)
         (.for (( [name, value] (.flatten additional_properties)) 
           (.field (make_string "X-" name) value)))

However, this is not necessarily the only or optimal solution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions