[proposal] default returns

This proposal consists in adding a functionally that I find useful in VB .net, that lets you set a default return value of a function at the begging rather than at the end

current:
func myComplexOperation() -> Int {
  //Some long complex code that might return some value
       //Default return at the very end
       return 0
}

proposal:
func myComplexOperation() -> Int {
  //set the default return at the begging using the ‘default’ keyword or some other keyword
  default 0
  //Some long complex code that might return a value
  //if the end of the function is reached, return the default
}

The other alternative will be to allow naming the return value
proposal2:
func myComplexOperation() -> result:Int {
        //set the default return at the begging using the output variable name
  result = 0
  //Some long complex code that might return some value
}

Another alternative will be to set the default value directly like and optional parameter
proposal3:
func myComplexOperation() -> Int = 0 {
  //Some long complex code that might return some value
}

In all of the cases the compiler will check that a default return is set before exiting the function
It could be some combination of the 3 proposals

Thanks

Is this really that much more convenient than:

func myComplexOperation() -> Int {
    let default = 0

    // do something

    return default
}

to justify a whole language feature?

Charles

···

On May 10, 2016, at 7:11 PM, Eduardo Mourey Lopez Ne via swift-evolution <swift-evolution@swift.org> wrote:

current:
func myComplexOperation() -> Int {
  //Some long complex code that might return some value
      //Default return at the very end
      return 0
}

proposal:
func myComplexOperation() -> Int {
  //set the default return at the begging using the ‘default’ keyword or some other keyword
  default 0
  //Some long complex code that might return a value
  //if the end of the function is reached, return the default
}

The other alternative will be to allow naming the return value
proposal2:
func myComplexOperation() -> result:Int {
       //set the default return at the begging using the output variable name
  result = 0
  //Some long complex code that might return some value
}

Another alternative will be to set the default value directly like and optional parameter
proposal3:
func myComplexOperation() -> Int = 0 {
  //Some long complex code that might return some value
}

This feels a little bit like back when I began studying Pascal where every function has an implicit Result variable to which we were to set the return value. I'm not particularly against Pascal or this syntax but it may lead to programmers scratching their heads to find out where a certain value is being defined to be returned from a function.

Additionally I'm not very fond of a language having two different syntaxes for returning a value. What should happen if, using the second proposal, the function reaches a return instruction with a constant or different variable? In my understanding it should be an error since I have already declared a variable for returning. It would also demand us to use two lines to return a different (error?) value from a function (result = 1; return;)

···

On 10 May 2016, at 9:11 pm, Eduardo Mourey Lopez Ne via swift-evolution <swift-evolution@swift.org> wrote:

This proposal consists in adding a functionally that I find useful in VB .net, that lets you set a default return value of a function at the begging rather than at the end

current:
func myComplexOperation() -> Int {
   //Some long complex code that might return some value
      //Default return at the very end
      return 0
}

proposal:
func myComplexOperation() -> Int {
   //set the default return at the begging using the ‘default’ keyword or some other keyword
   default 0
   //Some long complex code that might return a value
   //if the end of the function is reached, return the default
}

The other alternative will be to allow naming the return value
proposal2:
func myComplexOperation() -> result:Int {
       //set the default return at the begging using the output variable name
   result = 0
   //Some long complex code that might return some value
}

Another alternative will be to set the default value directly like and optional parameter
proposal3:
func myComplexOperation() -> Int = 0 {
   //Some long complex code that might return some value
}

In all of the cases the compiler will check that a default return is set before exiting the function
It could be some combination of the 3 proposals

Thanks
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

in proposal2:

If the function reaches a return instruction with a constant or a different variable, then it will return that value

example:
func myComplexOperation -> result:Int {
  result = 0;
  var a = 10;

  return 4; //this is ok
  return a; //this is ok, result will be replaced with the value of a
  return result; //this is ok, only one line is needed
  return; //fail, without argument its not valid
  return default; //this could also work (proposal1)
}

···

On May 10, 2016, at 7:33 PM, Leonardo Pessoa via swift-evolution <swift-evolution@swift.org> wrote:

This feels a little bit like back when I began studying Pascal where every function has an implicit Result variable to which we were to set the return value. I'm not particularly against Pascal or this syntax but it may lead to programmers scratching their heads to find out where a certain value is being defined to be returned from a function.

Additionally I'm not very fond of a language having two different syntaxes for returning a value. What should happen if, using the second proposal, the function reaches a return instruction with a constant or different variable? In my understanding it should be an error since I have already declared a variable for returning. It would also demand us to use two lines to return a different (error?) value from a function (result = 1; return;)

On 10 May 2016, at 9:11 pm, Eduardo Mourey Lopez Ne via swift-evolution <swift-evolution@swift.org> wrote:

This proposal consists in adding a functionally that I find useful in VB .net, that lets you set a default return value of a function at the begging rather than at the end

current:
func myComplexOperation() -> Int {
  //Some long complex code that might return some value
     //Default return at the very end
     return 0
}

proposal:
func myComplexOperation() -> Int {
  //set the default return at the begging using the ‘default’ keyword or some other keyword
  default 0
  //Some long complex code that might return a value
  //if the end of the function is reached, return the default
}

The other alternative will be to allow naming the return value
proposal2:
func myComplexOperation() -> result:Int {
      //set the default return at the begging using the output variable name
  result = 0
  //Some long complex code that might return some value
}

Another alternative will be to set the default value directly like and optional parameter
proposal3:
func myComplexOperation() -> Int = 0 {
  //Some long complex code that might return some value
}

In all of the cases the compiler will check that a default return is set before exiting the function
It could be some combination of the 3 proposals

Thanks
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution