[pre-proposal] Automatic unwrapper boolean properties using unless keyword


(Guilherme Torres Castro) #1

Hello,

This is a Idea I got from Objetive-C, where nil is evaluate no false, this
feature can keep the code clean. For example:

if (![myList isEmpty]) { ... }

Will evaluate true or false even if myList is nil.
I know that Swift is not Design to work like Objetive-C, but sometimes this
make the code cleaner.

Example:

if !myList?.isEmpty { print("Ok") }

This is much more cleaner and easy to read than:

if !(myList != nill && mylist!.isEmpty) { print("Ok") }

Moreover the tip from the Xcode (when you type the lead user to generate
wrong code when you type this code :

if !((myList?.isEmpty)) != nil { print("Ok") }

As you can see the suggestion made by the Xcode will not work, because
event if the list is empty the expression will return true.

My ideia is to use some special syntax that permit optional to be evaluate
to boolean expression. Those syntax may be like in ruby
<http://www.tutorialspoint.com/ruby/ruby_if_else.htm>.

code if condition

So the code from the example will be:

print ("Ok") if !myList?.isEmpty

Or using the unless keyword:

unless myList?.isEmpty print("not empty") else print "empty"

I didn't spent much time thinking about the syntax keyword, because I want
to know first if you will consider a great addition to the language, if so,
we can work on details and a formal proposal.


(Charles Srstka) #2

What I often do is use ?? to provide a default (usually false).

if myList?.isEmpty ?? false {
    print(“Empty”)
} else {
    print(“Not empty”)
}

The other thing you can do, of course, is to use a ‘where’ statement:

if let list = myList where list.isEmpty {
    print(“Empty”)
} else {
    print(“Not empty”)
}

Charles

···

On Mar 31, 2016, at 4:54 PM, Guilherme Torres Castro via swift-evolution <swift-evolution@swift.org> wrote:

Hello,

This is a Idea I got from Objetive-C, where nil is evaluate no false, this feature can keep the code clean. For example:

if (![myList isEmpty]) { ... }
Will evaluate true or false even if myList is nil.
I know that Swift is not Design to work like Objetive-C, but sometimes this make the code cleaner.

Example:
if !myList?.isEmpty { print("Ok") }
This is much more cleaner and easy to read than:

if !(myList != nill && mylist!.isEmpty) { print("Ok") }
Moreover the tip from the Xcode (when you type the lead user to generate wrong code when you type this code :

if !((myList?.isEmpty)) != nil { print("Ok") }
As you can see the suggestion made by the Xcode will not work, because event if the list is empty the expression will return true.

My ideia is to use some special syntax that permit optional to be evaluate to boolean expression. Those syntax may be like in ruby <http://www.tutorialspoint.com/ruby/ruby_if_else.htm>.

code if condition
So the code from the example will be:

print ("Ok") if !myList?.isEmpty
Or using the unless keyword:

unless myList?.isEmpty print("not empty") else print "empty"

I didn't spent much time thinking about the syntax keyword, because I want to know first if you will consider a great addition to the language, if so, we can work on details and a formal proposal.

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


(David Waite) #3

Swift originally had Optionals be boolean evaluable. Unfortunately, nil does not always mean false, and assuming so by default can mask issues. I believe there may have also been an issue with types being promoted to optional and then evaluated as true.

One option here is to use a new custom prefix operator to indicate you desire optional == false behavior. I chose “??” in the code below:

prefix operator ?? {}

prefix func ??(arg:Bool?) -> Bool {
  return arg ?? false
}

let data:[String:Bool?] = ["nil": nil, "true": true, "false": false]

for (key, value) in data {
  if ??value {
    print(key)
  }
} # prints true

I don’t think a negated form is as clear, since this is a shortcut for two checks. However, I wrote one for completeness:

prefix operator ‽‽ {}
prefix func ‽‽(arg:Bool?) -> Bool {
  return !(arg ?? false)
}

-DW

···

On Mar 31, 2016, at 3:54 PM, Guilherme Torres Castro via swift-evolution <swift-evolution@swift.org> wrote:

Example:
if !myList?.isEmpty { print("Ok") }


(Charles Srstka) #4

That’s what I get for writing a reply quickly; in this particular case the default should probably be true, since you probably want to treat a nil list as empty.

if myList?.isEmpty ?? true {
    print(“Empty”)
} else {
    print(“Not empty”)
}

Charles

···

On Mar 31, 2016, at 5:04 PM, Charles Srstka via swift-evolution <swift-evolution@swift.org> wrote:

What I often do is use ?? to provide a default (usually false).

if myList?.isEmpty ?? false {
    print(“Empty”)
} else {
    print(“Not empty”)
}