greg
2022-2-28 14:17:50

I just got one a couple hours ago.



christos.perivolaropo
2022-2-28 17:42:56

I find myself writing this pattern very often (let ([ret (make-mutable-structure ...)]) ,@mutate-ret ret) , is there an idiomatic way to do it?


sorawee
2022-2-28 17:44:56

What is “,@mutate-ret”?


sorawee
2022-2-28 17:45:33

It looks weird because it seems like you are not in a quasiquoting context, so it doesn’t make sense to unquote


sorawee
2022-2-28 17:46:13

do you meant to say

(let ([ret (make-mutable-structure ...)]) <expression that mutates ret> … ret) ?


sorawee
2022-2-28 17:49:29

There’s an idiom if <expression> ... doesn’t mention ret: (begin0 <e1> <e2> <e3>) is translated to:

(let ([ret <e1>]) <e2> <e3> ret) except that, as mentioned above <e2> and <e3> doesn’t have a way to refer to ret.


sorawee
2022-2-28 17:51:18

You are free to define your own macro however:

(define-syntax-rule (let0 x e es ...) (let ([x e]) es ... x)) (let0 ret (make-mutable-structure) (set-field! ret 1))


sorawee
2022-2-28 17:53:20

If you want even more magic, you can make ret to be a “magic keyword” similar to this in Java. This can be done by either using syntax parameter or unhygienic macro.


sorawee
2022-2-28 17:53:58

So the usage might look like:

(let0 (make-mutable-structure) (set-field! ret 1))


sschwarzer
2022-2-28 22:54:25

@pocmatos Thank you! :slightly_smiling_face:


spdegabrielle
2022-2-28 23:22:06

So I think I’ve successfully enabled GitHub login for discourse. If anyone can give it a go I’d appreciate some testing


ben.knoble
2022-2-28 23:27:39

Clojure has a doto form that is similar, something like (transliterated) (doto ([ret (make-…)]) (exps with ret)) Though now I can’t remember if doto returns ret or not. It was intended primarily for use with Java objects and methods, I think, but could be the basis for a useful macro.