optional variable with ternary operator


(Suresh Kansujiya) #1

Hey,

i am using ternary operator with optional variable. like below ex.

*var number:Int?**let result = number > 0 ? 1 : 2 *

*here i am getting this waring : comparison operators with optionals were
removed from the Swift Standard Library. Consider refactoring the code to
use the non-optional operators*
Note : i must need to use ternary operator for checking.

Regards
Suresh Kansujiya


(Saagar Jha) #2

Well, you’re not allowed to compare optionals any more. You can try binding the value to an Int, so that it’s not an optional anymore:

if let number = number {
  let result = number > 0 ? 1 : 2
}

Either way, you’ll have to decide what you think should happen when number is nil.

Saagar Jha

···

On May 8, 2017, at 00:36, Suresh Kansujiya via swift-evolution <swift-evolution@swift.org> wrote:

Hey,

i am using ternary operator with optional variable. like below ex.

var number:Int?
let result = number > 0 ? 1 : 2
here i am getting this waring : comparison operators with optionals were removed from the Swift Standard Library. Consider refactoring the code to use the non-optional operators

Note : i must need to use ternary operator for checking.

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


(Erica Sadun) #3

I believe this falls under "future directions" for comparison reform.

Here's an example of a short term solution: https://gist.github.com/erica/77b110e17e51dbea7d6934e6582f627f

-- E, who moved this from SE to Swift Users

···

On May 8, 2017, at 10:13 AM, Saagar Jha via swift-evolution <swift-evolution@swift.org> wrote:

Well, you’re not allowed to compare optionals any more. You can try binding the value to an Int, so that it’s not an optional anymore:

if let number = number {
  let result = number > 0 ? 1 : 2
}

Either way, you’ll have to decide what you think should happen when number is nil.

Saagar Jha

On May 8, 2017, at 00:36, Suresh Kansujiya via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Hey,

i am using ternary operator with optional variable. like below ex.

var number:Int?
let result = number > 0 ? 1 : 2
here i am getting this waring : comparison operators with optionals were removed from the Swift Standard Library. Consider refactoring the code to use the non-optional operators

Note : i must need to use ternary operator for checking.

Regards
Suresh Kansujiya
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto: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


(Zhao Xin) #4

I wonder if it has ever been allowed? I am using Xcode and it never allows
that.
For you specific question, you can use

var number:Int?

let result = (number ?? -1) > 0 ? 1 : 2

Zhaoxin

···

On Tue, May 9, 2017 at 1:39 AM, Erica Sadun via swift-users < swift-users@swift.org> wrote:

I believe this falls under "future directions" for comparison reform.

Here's an example of a short term solution: https://gist.github.com/erica/
77b110e17e51dbea7d6934e6582f627f

-- E, who moved this from SE to Swift Users

On May 8, 2017, at 10:13 AM, Saagar Jha via swift-evolution < > swift-evolution@swift.org> wrote:

Well, you’re not allowed to compare optionals any more. You can try
binding the value to an Int, so that it’s not an optional anymore:

if let number = number {
let result = number > 0 ? 1 : 2
}

Either way, you’ll have to decide what you think should happen when number
is nil.

Saagar Jha

On May 8, 2017, at 00:36, Suresh Kansujiya via swift-evolution < > swift-evolution@swift.org> wrote:

Hey,

i am using ternary operator with optional variable. like below ex.

*var number:Int?**let result = number > 0 ? 1 : 2 *

*here i am getting this waring : comparison operators with optionals were
removed from the Swift Standard Library. Consider refactoring the code to
use the non-optional operators*
Note : i must need to use ternary operator for checking.

Regards
Suresh Kansujiya
_______________________________________________
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

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


(Saagar Jha) #5

This functionality was removed via SE-0121, back in the Swift 3 timeframe.

Saagar Jha

···

On May 8, 2017, at 19:49, Zhao Xin <owenzx@gmail.com> wrote:

I wonder if it has ever been allowed? I am using Xcode and it never allows that.
For you specific question, you can use

var number:Int?
let result = (number ?? -1) > 0 ? 1 : 2

Zhaoxin

On Tue, May 9, 2017 at 1:39 AM, Erica Sadun via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:
I believe this falls under "future directions" for comparison reform.

Here's an example of a short term solution: https://gist.github.com/erica/77b110e17e51dbea7d6934e6582f627f

-- E, who moved this from SE to Swift Users

On May 8, 2017, at 10:13 AM, Saagar Jha via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Well, you’re not allowed to compare optionals any more. You can try binding the value to an Int, so that it’s not an optional anymore:

if let number = number {
  let result = number > 0 ? 1 : 2
}

Either way, you’ll have to decide what you think should happen when number is nil.

Saagar Jha

On May 8, 2017, at 00:36, Suresh Kansujiya via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Hey,

i am using ternary operator with optional variable. like below ex.

var number:Int?
let result = number > 0 ? 1 : 2
here i am getting this waring : comparison operators with optionals were removed from the Swift Standard Library. Consider refactoring the code to use the non-optional operators

Note : i must need to use ternary operator for checking.

Regards
Suresh Kansujiya
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

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

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users