Skip to content

Comment on Bog – small, strongly typed, embeddable languageparent

Comments

"Obtuse" is maybe not the word you were looking for. Opaque perhaps?

This kind of destructuring syntax is common in many newish languages -- Rust, Clojure, newer flavors of JS, Typescript, etc -- and becomes second nature pretty quickly. What you're seeing is really two things: first that you can pull apart a map as part of assignment, and second that there's sugar to name the variable after the map key.

In JS, you could write:

     const { foo: bar } = something
     
And that's sugar for
     const bar = something.foo
At the same time, JS many other languages have pervasive support for the idea that often you want the same name for a variable as the map key where you're going to use it. So, e.g.
     const mapOfFoo = { foo }
is equivalent to
     const mapOfFoo = { foo: foo }
Combining these things, you get:
    const { foo } = something
Equivalent to
    const foo = something.foo
i.e. the variable/property auto-aliasing works in the destructure too. When you consider that you are importing a big object with a bunch of fields and functions hung off of it, it becomes natural for that to work the same way:
    import { foo } from "someplace"
I'd argue that it's not actually opaque, let alone obtuse; it's merely unfamiliar because it's (I'm guessing) a combination of unfamiliar syntactical motifs. But as you get more familiar with that kind of syntax, you'll find it natural, and would be surprised if, say, it worked for regular objects but not in imports. When I picked up Rust, for example, I found myself right at home with it (same for the pattern-matching assignment stuff, which feels very similar to Clojure's).

As for errors, you'd get the same error as in the desugared version: ptint is not a property on the object it's pulling apart.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.