Shared Parameters Values

Is it possible for a functionality where parameters can be used within parameters?

lets say I have

struct TwoDPoint { 
   var x : Int 
   var y : Int

   init(x : Int, y : Int) {
      self.x = x 
      self.y = y
   }
}

static func someFuncThatPrintsSquaresNTimes( point: TwoDPoint , N : Int ) {
   .....
   .....
   ....
}

// using another parameters values
someFuncThatPrintsSquares( point : TwoDPoint(x : 5, y: 3), N : point.x * 5)

I know the function I am using is not a concrete example but nonetheless it demonstrates the point. If this is also not possible to include in Swift at a compiler level, please explain reason why. Im not a compiler expert.

See Compile-Time Constant Expressions for Swift.

[edit]

Actually, what you're asking doesn't fit in compile-time expressions, either. The short answer is that it doesn't even make sense. A function parameter only has a value within the body of the function. It doesn't exist at the call-site, where it's only a label.

How is this different from the following?

let point = TwoDPoint(x : 5, y: 3)
someFuncThatPrintsSquares(point : point, N : point.x * 5)
1 Like

Why do you want to solve the problem in such a complex way?
What @rguerreiro showed is the right way to do it here. The pitched feature and call-site that was shown in the original post hurts code readability a lot and does not solve a real world issue in my eyes. Furthermore it won't work when parameter labels are missing (e.g. if it was _ point: instead).

Aside: Please don't tell people to fix their coding style, except in a thread on coding style. It makes the forums less of a welcoming environment.

7 Likes

Fixed, thx. ;)

2 Likes