Questions about Session.update

I’m reading the Gatling source, in particular Session.scala. In part because I’m trying to extract enough of it to test my development of a session management DSL, and in part to just understand Scala a little better…

The code looks like this:

def update(updates: Iterable[Session => Session]): Session = updates.foldLeft(this) { (session, update) => update(session) }

What I’m seeing, in English, is this:

Takes in an object which is an Iterable (e.g. list, seq, etc.) of functions that take in a Session and return a Session. The implementation uses foldLeft to iterate over the list, calling the function in that position in the list using the current session, which returns a new session which is then passed in to the next function, and so on down the list. End result is a new session object. It took a little time to parse it and understand it, but I got the gist of it.

But why does this method exist? What is the use case for it? Why isn’t there an update that takes in a single function, instead of a list (iterable) of functions?

Internal. Not made private[gatling] only to be third party friendly.

Because when you perform checks on resources, you don’t want to apply the result (like saving something) on spot (remember, Sessions are immutable): you want to pile up the modifications and apply them altogether once you’re done. Same thing goes with WebSockets.

Makes sense, I think. So, internally, things like .check() are building up a list of functions to be called against the session in order to modify it?

Not modify it, but create the new version, but yes.