Difference between val and def for scenario definition

Hi,

I am defining a Gatling scenario and I would like to separate the http calls from the scenario. Instead of coding the whole definition in my scenario, I want to create a package containing all my http calls…
Instead of :
val myScenario = scenario(“Test”)
.exec(
http(“transaction 1”).post("/bla/bla").headers(…).formParam(…)…
)
.exec(
http(“transaction 2”).post("/bla/bla").headers(…).formParam(…)…
)
etc…
I want to define 1 object per http call and use them as I want in my future scenarios, like this:
val myScenario = scenario(“Test”)
.exec(myTransaction1) //defined in another object
.exec(myTransaction2) // same

The question is: what is the best way to declare my object containing the http call ?
object GetIAMToken {
def myTransaction1: HttpRequestBuilder = http(“transaction 1”).post…
}

OR

object GetIAMToken {
val myTransaction1: HttpRequestBuilder = http(“transaction 1”).post…
}

I’m quite new to Scala and I’ve read that val is evaluated when defined, and def is evaluated on call.
Also, I’ve seen this example on internet (for use of val):
Capture.PNG

Does val have the same behaviour if I define an HTTP call using it ? For example if I want to extract an ID in the json response from the server, randomly, I don’t want it to be always the same.

Thanks for your help !

Terry

This e-mail and any attachment are confidential and intended solely for the use of the individual to whom it is addressed. If you are not the intended recipient, please telephone or email the sender and delete this message and any attachment from your system. Unauthorized publication, use, dissemination, forwarding, printing or copying of this e-mail and its associated attachments is strictly prohibited.

Let’s respect the environment together. Only print this message if necessary.

Hi,

I can understand your confusion, more with the sample you provided.

The sample really defines a function with a return value computed only once.

But as suggested by the type: HttpRequestBuilder (a builder, not a final request), if the code does not depend on external values, you should be good to create a val.
More, exec accepts values as parameters (not functions), it can be a clue that you can use val safely.

But as you said, this is a question about scala itself and not specific to gatling.
Any tutorial or course on scala will you explain it better than I can.

Cheers!

Capture.PNG

Again thank you for your answer !

Terry