Function Return Type

I sometimes write code like this:

func something() -> [Something: SomethingElse] {
  var result =  [Something: SomethingElse]()
  // ... stuff here
  return result
}

Wouldn't it be nice to have a Type for return values of the function? Then I could perhaps write:

func something() -> [Something: SomethingElse] {
  var result =  ReturnType()
  // ... stuff here
  return result
}

just to save some repetition, especially if the return types get more complex. (Ok could arguably use a typedef then.)

EDIT: I was just old that Delphi does it similarly but even goes a step further: https://www.oreilly.com/library/view/delphi-in-a/1565926595/re276.html

6 Likes

Please allow me to be the first to bike shed! Other Swift types, even ones dependent on where they are used don't have the word "type" in them, they rely on the leading uppercase letter, for example "Self" v. "self".

So it might be better spelled "Return", or "Result".

(and I like this, it is a small quality of life improvement...)

3 Likes

We already have precedent for accessing some of these source details in the form of #something, for example #function or #line.

To have access to this it would be more appropriate for #return or similar

1 Like

Aren't those usually strings though, for loggin purposes? Excepting #selector.

The following gives an error in current Swift, but the idea is to use a "non-generic" parameter and a where clause.

// error: Same-type requirement makes generic parameter 'T' non-generic
func something<T>() -> T where T == [Something: SomethingElse] {
  var result = T()
  // ... stuff here
  return result
}
Terms of Service

Privacy Policy

Cookie Policy