bigbiodata
bigbiodata

Reputation: 103

Is there any way possible where a variable length of arguments can be accepted by a netlogo function?

I have some code that is repeated multiple times and I am trying to make a function so that I just call that function with args, but I wonder if a variable number of args is accepted by the same function in netlogo?

For example:

to set-attributes [ x-occupiedepitopes x-size x-exempt_from_immobilisation x-aggregated ]
  set heading random 360
  set occupiedepitopes x-occupiedepitopes
  set size x-size
  set leader self
  set exempt_from_immobilisation x-exempt_from_immobilisation
  set aggregated x-aggregated
end

to set-attributes2 [ x-occupiedepitopes x-size x-exempt_from_immobilisation x-aggregated x-color ]
  set heading random 360
  set occupiedepitopes x-occupiedepitopes
  set size x-size
  set leader self
  set exempt_from_immobilisation x-exempt_from_immobilisation
  set aggregated x-aggregated
  set color x-color
end

The above two functions are identical apart from one extra arg #x-color

Thank you

Upvotes: 1

Views: 37

Answers (1)

Nicolas Payette
Nicolas Payette

Reputation: 14972

NetLogo doesn't support variable argument numbers (at least not in user procedures), nor does it support overloading.

You could still make your code less repetitive by doing something like:

to set-attributes [ x-occupiedepitopes x-size x-exempt_from_immobilisation x-aggregated ]
  set heading random 360
  set occupiedepitopes x-occupiedepitopes
  set size x-size
  set leader self
  set exempt_from_immobilisation x-exempt_from_immobilisation
  set aggregated x-aggregated
end

to set-attributes2 [ x-occupiedepitopes x-size x-exempt_from_immobilisation x-aggregated x-color ]
  set-attributes x-occupiedepitopes x-size x-exempt_from_immobilisation x-aggregated
  set color x-color
end

Another approach would be to accept a list as an argument and then have your procedure behave differently depending on the number of items in the list, but that might be more complicated than it's worth.

Upvotes: 2

Related Questions