List Info

Thread: causing side effects in user-defined functions




causing side effects in user-defined functions
user name
2006-11-27 07:48:57
I'd like a user-defined function to say x="M"$(N),
increment N, and
return x. I know how to write user-defined functions, but I
don't
understand how they can cause side effects like the
incrementing of the
variable N. In case it isn't clear, this function would
return M0 the
first time, M1 the second time, etc. User-defined functions
in make seem
functional. Maybe I can use semicolons? Is the value of a
function the
value of its last expression? Thanks.


_______________________________________________
Help-make mailing list
Help-makegnu.org
http:
//lists.gnu.org/mailman/listinfo/help-make
causing side effects in user-defined functions
user name
2006-11-27 07:56:15
On Mon, 27 Nov 2006 01:48:57 -0600
Brian Lewis <bsl04uark.edu> wrote:

> I'd like a user-defined function to say
x="M"$(N), increment N, and
> return x. I know how to write user-defined functions,
but I don't
> understand how they can cause side effects like the
incrementing of
> the variable N. In case it isn't clear, this function
would return M0
> the first time, M1 the second time, etc. User-defined
functions in
> make seem functional. Maybe I can use semicolons? Is
the value of a
> function the value of its last expression? Thanks.

This is what I came up with. I'm surprised it worked. Sorry
for the noise.

generate_module_name = 
    x = M$(shell printf "%02u"
$(CURRENT_MODULE_NUM)) 
    $(eval CURRENT_MODULE_NUM = $(shell expr
$(CURRENT_MODULE_NUM) + 1)) 
    $(x)


_______________________________________________
Help-make mailing list
Help-makegnu.org
http:
//lists.gnu.org/mailman/listinfo/help-make
causing side effects in user-defined functions
user name
2006-11-27 17:40:14
On 11/26/06, Brian Lewis <bsl04uark.edu> wrote:
> I'd like a user-defined function to say
x="M"$(N), increment N, and
> return x. I know how to write user-defined functions,
but I don't
> understand how they can cause side effects like the
incrementing of the
> variable N. In case it isn't clear, this function would
return M0 the
> first time, M1 the second time, etc. User-defined
functions in make seem
> functional. Maybe I can use semicolons? Is the value of
a function the
> value of its last expression? Thanks.

Here are two alternatives to what you came up with if you
are still
interested.  The first one requires Lua support which is
provided by
the patches posted here.

  http://lists.gnu.org/archive/html/help-make/200
6-10/msg00018.html

The second requires GNU bc.  The advantage of the first is
that there
will be no fork/exec for the shell process.  This may matter
if you
call the macro repeatedly.  The advantage of the second is
that it
will work with unpatched GNU make.

First Solution
x := 1
N := 1
$(info x = $(x))
$(info N = $(N))

generate-module-name = 
$(strip 
  $(eval x := M$(N)) 
  $(eval N := $(lua printmake($(N) + 1))) 
  $(x) 
 )

$(info generate-module-name result =
$(generate-module-name))
$(info x = $(x))
$(info N = $(N))


Second Solution
x := 1
N := 1
$(info x = $(x))
$(info N = $(N))

generate-module-name = 
$(strip 
  $(eval x := M$(N)) 
  $(eval N := $(shell echo $(N) + 1|bc)) 
  $(x) 
 )

$(info generate-module-name result =
$(generate-module-name))
$(info x = $(x))
$(info N = $(N))


  Ken Smith


_______________________________________________
Help-make mailing list
Help-makegnu.org
http:
//lists.gnu.org/mailman/listinfo/help-make
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )