[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Temporary changing the behavior of a function
From: |
Marcin Borkowski |
Subject: |
Re: Temporary changing the behavior of a function |
Date: |
Fri, 06 Nov 2015 17:05:38 +0100 |
User-agent: |
mu4e 0.9.15; emacs 25.0.50.1 |
On 2015-11-06, at 16:01, Yuri Khan <yuri.v.khan@gmail.com> wrote:
> On Fri, Nov 6, 2015 at 4:23 PM, Marcin Borkowski <mbork@mbork.pl> wrote:
>
>> so there is this function `foo', which calls the function `bar'. The
>> function `bar' is responsible for asking the user for some value and
>> passing that value to the guts of `foo'.
>>
>> Now I want to call `foo' in the Mafia-mode;-), i.e., it should ask no
>> further questions. What do I do? AFAIU, `cl-flet' won't help, since it
>> is lexical. The best I can think of is to temporarily advice `bar' with
>> :override - but then, instead of a `let'-like, local construct, I have
>> to explicitly add and then remove the advice, right?
>>
>> Any other ideas?
>
> Yes. In other languages, we recognize such a need as a “smell”, a sign
> of possibly bad design.
>
> “foo” should have optional arguments that can be passed by the calling
> code, and if they are not set, only then ask “bar” for user-supplied
> parameters.
>
> Alternatively, “foo” should invoke “bar” and pass its result to
> “foo-guts” (we usually call it “foo-impl” or “do-foo”), and
> programmatic callers should call “foo-guts” directly with the right
> arguments.
>
> If “foo” is in a third-party library you cannot or would rather not
> change, only then consider monkey-patching “bar” as a workaround.
All agreed, and that was my first thought, too. Of course, both `foo'
and `bar' are in some library I cannot change ATM. (That will wait
until I sign the FSF papers;-).)
OTOH, I /can/ imagine that the author did not anticipate such use: `foo'
is called only once in the whole (big) library, and then by another
interactive command, for which the user interaction is fine.
Thanks anyway,
--
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University