steveh2009
2019-6-9 16:11:03

If I have a structure, pass an instance of it to a function, is there a macro to destructure it into its named elements with a series of let assignments? For example, (struct abc (a b c)). An instance of that passed to a function, gets destructured so I can access let local names a, b and c? I’ve got very long names and that would help the readability of the code rather than the usual structure element dereferencing way.


soegaard2
2019-6-9 16:12:27

Use match or match-define to destructure (match (abc 1 2 3) [(abc a b c) (list a b c)])


steveh2009
2019-6-9 16:27:05

So to automatically do that where I could take any structure, not have to name the individual elements to assign to, that’s a macro I would have to write. In other words, automatic introspection of the structure passed in, macro generates the let local names and I just use them.


soegaard2
2019-6-9 16:30:59

steveh2009
2019-6-9 16:35:56

That’s it. Thanks!


dave
2019-6-9 18:52:51

So, having done http://pasterack.org/pastes/31923 yesterday, I can now convert a data structure in the source code into a srcloc-annotated version of that structure. I’ve played with it a bit, and it’s nice to get good errors, but the data structure is also hard to use when you’re trying to manipulate the expr. Destructuring binds all over the place to pull the structs apart, match patterns are cluttered…


dave
2019-6-9 18:54:09

Is there a Racket facility I don’t know of that would help with this? If I could have a pony, I’d have expressions that look and feel like plain old strings, lists, numbers… But with an additional magic accessor that lets me pull out the source location I stashed away. Is there… something shaped like that?


dave
2019-6-9 18:54:39

or am I stuck with a choice between good errors or easy data manipulation, pick one?


dave
2019-6-9 18:55:12

(or go deeper into the language rabbit hole and reimplement all the basic toolkit of map/fold/etc. but on this whacky datatype)


soegaard2
2019-6-9 18:59:56

This destructuring - is this during parsing? Or afterwards when you need to use the annotated version?


dave
2019-6-9 19:04:31

Afterwards. When you’re writing this code, you’re defining some structured data, and then manipulating it in various ways. Ideally, basic manipulation tools like map or fold would keep working, in addition to custom functions I’m writing.


dave
2019-6-9 19:05:18

And they kinda do - except the altered shape of data means you have to add a bunch of boilerplate everywhere to get back to “that expr that was in the source code”


soegaard2
2019-6-9 19:12:41

Nothing comes to mind. Except for writing some helper functions to manipulate the source locations.


dave
2019-6-9 19:17:43

yeah, barring language features I don’t know about, my only idea was to add a few macros to make the constant destructuring/restructuring a bit easier on the eyes


soegaard2
2019-6-9 19:18:17

dave
2019-6-9 19:48:12

yeah, quite possibly that’s good enough. I’ll write some code and see how it comes out. Thanks!


notjack
2019-6-9 19:58:09

@dave Sounds like a lens-y problem


dave
2019-6-9 20:03:26

ooh, another thing I know nothing about! Awesome, I’m off to do some reading.


notjack
2019-6-9 20:07:15

@dave Disclaimer: I wrote a lens library, so my suggestion might be biased :p https://github.com/jackfirth/lens


dave
2019-6-9 20:12:35

hah. Reading the intro now. I can see the potential, although possibly a catch is that the setters might be tricky. A lens getter for “give me just the expression” makes a lot of sense, but when setting or updating, you really want to be able to provide a new srcloc as well. I’m not quite sure how I’d set that up elegantly yet.


dave
2019-6-9 20:12:46

but, I still have a bunch of reading to do.


notjack
2019-6-9 20:13:36

Good luck :)