Skip to content

Changing order of labels in a function call does not change order of evaluation. #5272

@hayleigh-dot-dev

Description

@hayleigh-dot-dev

I've noticed that when using labels in a function call, the order arguments are evaulated in does not correspond to the order written in the source code. Instead, the arguments are evaluated in the order they are defined in the function itself.

I have this function:

pub fn fit(into viewport: Bounds, box box: Bounds) -> Transform

You can see it has two arguments, labelled into: and box: respectively. If we take this call:

transform.fit(
  into: echo bounds.new(..) as "viewport",
  box: echo graph.fold(..) as "box",
)

and look at the console output, we can see:

[Log] dev/experiments/layout.gleam:40 viewport (layout.mjs, line 246)
#(0, 0, 300, 300)

[Log] dev/experiments/layout.gleam:41 box (layout.mjs, line 246)
#(0, 0, 293.48392537260776, 291.62981208738245)

All is expected. "viewport" is logged first, then "box".

However, if we flip the order we provide the labels, so now the function call is:

transform.fit(
  box: echo graph.fold(..) as "box",
  into: echo bounds.new(..) as "viewport",
)

We can see the that the logs have not changed, and "viewport" is still logged first.

[Log] dev/experiments/layout.gleam:51 viewport (layout.mjs, line 246)
#(0, 0, 300, 300)

[Log] dev/experiments/layout.gleam:40 box (layout.mjs, line 246)
#(0, 0, 293.48392537260776, 291.62981208738245)

Is this a bug? It is very surprising that side effects can happen in a different order to the one provided in the source code!

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions