Proposal: Do not allow redundant unwrapping.


(Amir Michail) #1

The following should result in a compile error:

var x:Int? = 5
var y:Int?

y = x! // compiler error, unwrapping is redundant

The unnecessary unwrapping gives the wrong impression about the type of y — namely that it is Int.


(Joe Groff) #2

The following should result in a compile error:

var x:Int? = 5
var y:Int?

y = x! // compiler error, unwrapping is redundant

The unnecessary unwrapping gives the wrong impression about the type of y — namely that it is Int.

The compiler doesn't know this. You gave x type 'Int?', not 'Int'.

-Joe

···

On Dec 9, 2015, at 6:31 AM, Amir Michail via swift-evolution <swift-evolution@swift.org> wrote:

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


(Amir Michail) #3

The following should result in a compile error:

var x:Int? = 5
var y:Int?

y = x! // compiler error, unwrapping is redundant

The unnecessary unwrapping gives the wrong impression about the type of y — namely that it is Int.

The compiler doesn't know this. You gave x type 'Int?', not 'Int’.

It knows this because x! has type Int and y has type Int?. So the unwrapping is unnecessary prior to the assignment to y. You should have just written “y = x”.

···

On Dec 9, 2015, at 12:13 PM, Joe Groff <jgroff@apple.com> wrote:

On Dec 9, 2015, at 6:31 AM, Amir Michail via swift-evolution <swift-evolution@swift.org> wrote:

-Joe

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


(Joe Groff) #4

Ah, I see what you're saying. This seems like a reasonable warning to add to the compiler; I don't think it requires a formal language change. Got time to file a bug requesting this?

-Joe

···

On Dec 9, 2015, at 3:20 PM, Amir Michail <a.michail@me.com> wrote:

On Dec 9, 2015, at 12:13 PM, Joe Groff <jgroff@apple.com> wrote:

On Dec 9, 2015, at 6:31 AM, Amir Michail via swift-evolution <swift-evolution@swift.org> wrote:

The following should result in a compile error:

var x:Int? = 5
var y:Int?

y = x! // compiler error, unwrapping is redundant

The unnecessary unwrapping gives the wrong impression about the type of y — namely that it is Int.

The compiler doesn't know this. You gave x type 'Int?', not 'Int’.

It knows this because x! has type Int and y has type Int?. So the unwrapping is unnecessary prior to the assignment to y. You should have just written “y = x”.


(Amir Michail) #5

The following should result in a compile error:

var x:Int? = 5
var y:Int?

y = x! // compiler error, unwrapping is redundant

The unnecessary unwrapping gives the wrong impression about the type of y — namely that it is Int.

The compiler doesn't know this. You gave x type 'Int?', not 'Int’.

It knows this because x! has type Int and y has type Int?. So the unwrapping is unnecessary prior to the assignment to y. You should have just written “y = x”.

Ah, I see what you're saying. This seems like a reasonable warning to add to the compiler; I don't think it requires a formal language change. Got time to file a bug requesting this?

Done. It’s SR-167.

···

On Dec 9, 2015, at 6:30 PM, Joe Groff <jgroff@apple.com> wrote:

On Dec 9, 2015, at 3:20 PM, Amir Michail <a.michail@me.com> wrote:

On Dec 9, 2015, at 12:13 PM, Joe Groff <jgroff@apple.com> wrote:

On Dec 9, 2015, at 6:31 AM, Amir Michail via swift-evolution <swift-evolution@swift.org> wrote:

-Joe


(Joe Groff) #6

Thanks!

···

On Dec 9, 2015, at 3:41 PM, Amir Michail <a.michail@me.com> wrote:

On Dec 9, 2015, at 6:30 PM, Joe Groff <jgroff@apple.com <mailto:jgroff@apple.com>> wrote:

On Dec 9, 2015, at 3:20 PM, Amir Michail <a.michail@me.com <mailto:a.michail@me.com>> wrote:

On Dec 9, 2015, at 12:13 PM, Joe Groff <jgroff@apple.com <mailto:jgroff@apple.com>> wrote:

On Dec 9, 2015, at 6:31 AM, Amir Michail via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

The following should result in a compile error:

var x:Int? = 5
var y:Int?

y = x! // compiler error, unwrapping is redundant

The unnecessary unwrapping gives the wrong impression about the type of y — namely that it is Int.

The compiler doesn't know this. You gave x type 'Int?', not 'Int’.

It knows this because x! has type Int and y has type Int?. So the unwrapping is unnecessary prior to the assignment to y. You should have just written “y = x”.

Ah, I see what you're saying. This seems like a reasonable warning to add to the compiler; I don't think it requires a formal language change. Got time to file a bug requesting this?

Done. It’s SR-167.

-Joe