

Jensen's Device - grhmc
https://en.wikipedia.org/wiki/Jensen%27s_Device

======
antimagic
OK, I give in - I can't for the life of me think of why this is interesting. I
mean, it's the equivalent of passing a pointer to a value rather than the
value itself in C, and is the bog standard techniques to use in this type of
language to achieve multiple return values. But I just don't get why that is
interesting - it's a bit more like programming 101...

~~~
victorNicollet
This is not passing-by-pointer. If you look at the example `swap(i,A[i])` in
the "Criticism" section, you'll see that it is not the address of `A[i]`(as
evaluated at call time) that is passed, but the entire expression `A[i]` to be
re-evaluated every time `i` changes.

There isn't a lot of support for this kind of thing in modern languages.
Lambdas do come to mind, e.g. `swap(() => i, () => A[i])`, but few languages
support returning lvalues from closures without significant boilerplate.

~~~
FrankenPC
Thanks for the explanation. In general, is this considered an anti-pattern? In
other words, It's interesting but not advisable to consider in production
programming?

~~~
victorNicollet
It's mostly a language-evolutionary dead end that gave us some good ideas.

The original implementations were very bad: pass-by-name was not visible at
the calling point, so any function call could suddenly break due to aliasing
issues (or change the value of a local variable, which is also the case with
pass-by-reference). I am not aware of any widely used modern languages that
implement it outside of macros (including C preprocessor macros, LISP, etc.)

The original concept has been refined over time and made its way into other
languages though, in the form of callbacks, continuations, etc.

~~~
kybernetikos
> The original implementations were very bad: pass-by-name was not visible at
> the calling point, so any function call could suddenly break due to aliasing
> issues (or change the value of a local variable, which is also the case with
> pass-by-reference). I am not aware of any widely used modern languages that
> implement it outside of macros (including C preprocessor macros, LISP, etc.)

Scala. Call-by-name that is invisible at the call point. It doesn't cause
aliasing issues because it's treated like getting passed a lambda, but if any
of the code has side effects you can be very surprised about when they occur.

~~~
karmakaze
I've used Scala's call-by-name, but what I really wanted was call-by-let
taking the argument as pure and evaluating at most once in the called
function.

~~~
kybernetikos
scala's call-by-name is often used for implementing control-flow like
structures, so you can understand why they would want to be able to evaluate
it more than once. Having said that, there are so many situations where it can
be surprising, I'd probably have had a rule that you have to use {} to
indicate call-by-name arguments at call site.

------
patsplat
Somewhere in here is a pop culture reference that I can't quite put together.

~~~
d23
I thought it was a "Jepsen" device at first.

------
IshKebab
This seems like a terrible idea.

