[m-users.] inline / anonymous predicates ... style...

Julien Fischer jfischer at opturion.com
Sun Mar 6 01:21:01 AEDT 2022



On Sat, 5 Mar 2022, Sean Charles (emacstheviking) wrote:

> Having recently seen the `errors.m` contribution to the Mercury extras
> folder, and more reading if compiler and mmc-doc source code, I often
> see this style:
>
> P = (pred (...) .......
>      ),
>      list.take_while(P, ....
> or   list.foldl(P, ...
>
> and I wondered is it a style matter or is the compiler able to do more
> if I define the predicate first ? I ask as my code for my transpiler
> rarely does this, mainly because of my past experience with other
> languages where you just throw the code inline:
>
>    list.take(
> 	(pred(...)
>        ),
> 	..args..
>    )
>
> is there a preferred style and if so, what was the basis for that
> decision ? I ask because once a week I go on a 'code cleaning'
> exercise in my transpiler to help me spot repetition and such like.

Bear in mind, the compiler will internally transform the latter style
into the former anyway.  When it does so, it will need to introduce
a variable. If there are any errors in the closure, those error
messages may contain a reference to that introduced variable.

Obviously, if you use the first form, any error messages will refer to a
variable (e.g. P above) that is actually present in your source code.
So, one reason for preferring the former style is that you will get more
meaningful error messages.

Personally, I also find the second form more difficult to read for all
but the smallest closures.

Julien.


More information about the users mailing list