Home > other >  swift when should return have parenthesis and when does it not matter?
swift when should return have parenthesis and when does it not matter?

Time:01-19

Is there ever a case where "return n" will cause a problem due to something within ". . ." ?

func foo() -> Int
{
    . . .
    return n
}

Or, should I always use return( n ) ?

func foo() -> Int
{
    . . .
    return( n)
}

CodePudding user response:

I don't think the parentheses are needed unless you declare your function to return a tuple.

As gnasher says in their answer, the parens in the return are weird.

I seem to remember that a function result in Swift is always considered to be a tuple, where Void is a special case empty tuple. If my (vague) memory is correct that might explain why the parens are valid. I need to see if I can dig that up.

CodePudding user response:

The parentheses are absolutely weird. They are weird in C, in Swift they are bizarre.

And since space is relevant in Swift, your weird spacing makes it even weirder.

As a rule, you don’t write anything unnecessary in Swift.

  •  Tags:  
  • Related