Using Swift Optionals with ++ -- Operators

Originator:florian.p37
Number:rdar://17216338 Date Originated:07-Jun-2014
Status:Open Resolved:
Product:Swift Product Version:swift-600.0.34.4.5
Classification: Reproducible:Always
 
Summary:
Hi, 
I'm filling this bug report for what may look like bugs in Swift language.
I'm not the original person that have found this issue but I came up with a workaround. 

To give you more context about these issues here are original thread posted to the devForums.
 https://devforums.apple.com/message/978757#978757

Steps to Reproduce:
Using Swift language define an optional variable, use operators like ++, -- etc...:
var aNum: Int? = 3
++aNum 

Expected Results:
The expected result should change the value of the variable according to the operator used, with the provided steps aNum should have an int value of 4

Actual Results:
// Generates a compile error: could not find an overload for '++' that accepts the supplied arguments

++ operator does not seem to accept optionals as arguments.


Version:
Xcode 6.0 6A215l

Notes:
I've redefined the ++ operator as a workaround

operator prefix ++ {  }
@prefix @assignment func ++ (inout value: Int?) -> Int {
  if var n : Int = value {
    value = ++n
    return value!
  }
  else {
    return 0 // discussion about returning 0 for an empty optional
  }
}


var aNum: Int? = 3
++aNum // 4
++aNum // 5

var aNum2: Int? // nil
++aNum2 //discussion : 0

var a = 1
++a // 2

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!