Surprise Discovery -- "if let x = x" Works

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Surprise Discovery -- "if let x = x" Works

Thomas Wetmore
I was just reviewing some of my Swift code after making the changes needed for beta 5. I came across the following:

    gedcomRecord = GedcomRecord.createGedcomRecord(root!)

    if let gedcomRecord = gedcomRecord {

        records.append(gedcomRecord)

        ...


I was surprised to see that I had written "if let gedcomRecord = gedcomRecord" (this was an artifact of an earlier rewrite), and even more surprised to see that it works correctly. Note that createGedcomRecord returns an optional record. On second thought this might not be surprising to those who are smarter than me, since the "inner" gedcomRecord is "just" a new block local variable. In case it isn't obvious, the "outer" gedcomRecord is wrapped, and the "inner" gedcomRecord is unwrapped.


See, I had been worrying about a naming convention to use when handling optionals, wanting to have some pattern that makes sense in all the "if let x = y" expressions I found myself writing. I had toyed with the idea of adding a "q" to the end of every optional variable, and then removing the "q" for the unwrapped variable. An earlier version of this code had been "if let gedcomRecord = gedcomRecordq", but I had decided that that naming convention was crap and had converted. But in this case I forgot to change to the new experimental convention, leading to the surprise.


So now I have to decide whether universally using "if let x = x" is a good idea, or if will it lead to massive confusion down the road. Since the scopes of these "inner x's" are generally small, this might be an okay convention. As someone who cares a lot about good variable names, and spends much too much time of his life worrying about finding good ones, it does feel good to know that I have cut my problems in half for optionals.

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/a43bdcea-d2b8-40d4-8abd-654ac514c708%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Surprise Discovery -- "if let x = x" Works

Tom Lieber
I think it’s a reasonable convention. The type system probably won’t let you do anything you’ll regret (that’s the point of Swift :), and it’s quick to disambiguate: click one instance and references to the same variable will get the little gray outline and the others won’t.

How often does it come up? I’m about 1,000 lines into a Swift project and there’s only one place where I use ‘if let’ on a name in the current scope. Probably because when I’m in a similar situation, I would write it like this instead:

  if let gedcomRecord = GedcomRecord.createGedcomRecord(root!) {



On Mon, Aug 4, 2014 at 6:27 PM, Thomas Wetmore <[hidden email]> wrote:
I was just reviewing some of my Swift code after making the changes needed for beta 5. I came across the following:

    gedcomRecord = GedcomRecord.createGedcomRecord(root!)

    if let gedcomRecord = gedcomRecord {

        records.append(gedcomRecord)

        ...


I was surprised to see that I had written "if let gedcomRecord = gedcomRecord" (this was an artifact of an earlier rewrite), and even more surprised to see that it works correctly. Note that createGedcomRecord returns an optional record. On second thought this might not be surprising to those who are smarter than me, since the "inner" gedcomRecord is "just" a new block local variable. In case it isn't obvious, the "outer" gedcomRecord is wrapped, and the "inner" gedcomRecord is unwrapped.


See, I had been worrying about a naming convention to use when handling optionals, wanting to have some pattern that makes sense in all the "if let x = y" expressions I found myself writing. I had toyed with the idea of adding a "q" to the end of every optional variable, and then removing the "q" for the unwrapped variable. An earlier version of this code had been "if let gedcomRecord = gedcomRecordq", but I had decided that that naming convention was crap and had converted. But in this case I forgot to change to the new experimental convention, leading to the surprise.


So now I have to decide whether universally using "if let x = x" is a good idea, or if will it lead to massive confusion down the road. Since the scopes of these "inner x's" are generally small, this might be an okay convention. As someone who cares a lot about good variable names, and spends much too much time of his life worrying about finding good ones, it does feel good to know that I have cut my problems in half for optionals.

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/a43bdcea-d2b8-40d4-8abd-654ac514c708%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
Tom Lieber
http://AllTom.com/
http://infinite-sketchpad.com/

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/CAProext9a_5OJnQ3aG6NmVwOjA4ROa1MnuahYdn%2BnXmzXQvF-w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Surprise Discovery -- "if let x = x" Works

Thomas Wetmore
Thanks and good point. I agree that in most cases you don't need an optional variable, and I use your approach most of the time. But in a very small number of cases I have found it useful to hold onto the optional value in its own variable.

On Monday, August 4, 2014 9:45:02 PM UTC-4, Tom Lieber wrote:
I think it’s a reasonable convention. The type system probably won’t let you do anything you’ll regret (that’s the point of Swift :), and it’s quick to disambiguate: click one instance and references to the same variable will get the little gray outline and the others won’t.

How often does it come up? I’m about 1,000 lines into a Swift project and there’s only one place where I use ‘if let’ on a name in the current scope. Probably because when I’m in a similar situation, I would write it like this instead:

  if let gedcomRecord = GedcomRecord.createGedcomRecord(root!) {



On Mon, Aug 4, 2014 at 6:27 PM, Thomas Wetmore <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="rurTqL10rXAJ" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">ttwet...@...> wrote:
I was just reviewing some of my Swift code after making the changes needed for beta 5. I came across the following:

    gedcomRecord = GedcomRecord.createGedcomRecord(root!)

    if let gedcomRecord = gedcomRecord {

        records.append(gedcomRecord)

        ...


I was surprised to see that I had written "if let gedcomRecord = gedcomRecord" (this was an artifact of an earlier rewrite), and even more surprised to see that it works correctly. Note that createGedcomRecord returns an optional record. On second thought this might not be surprising to those who are smarter than me, since the "inner" gedcomRecord is "just" a new block local variable. In case it isn't obvious, the "outer" gedcomRecord is wrapped, and the "inner" gedcomRecord is unwrapped.


See, I had been worrying about a naming convention to use when handling optionals, wanting to have some pattern that makes sense in all the "if let x = y" expressions I found myself writing. I had toyed with the idea of adding a "q" to the end of every optional variable, and then removing the "q" for the unwrapped variable. An earlier version of this code had been "if let gedcomRecord = gedcomRecordq", but I had decided that that naming convention was crap and had converted. But in this case I forgot to change to the new experimental convention, leading to the surprise.


So now I have to decide whether universally using "if let x = x" is a good idea, or if will it lead to massive confusion down the road. Since the scopes of these "inner x's" are generally small, this might be an okay convention. As someone who cares a lot about good variable names, and spends much too much time of his life worrying about finding good ones, it does feel good to know that I have cut my problems in half for optionals.

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="rurTqL10rXAJ" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">swift-languag...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="rurTqL10rXAJ" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">swift-l...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/swift-language/a43bdcea-d2b8-40d4-8abd-654ac514c708%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" onmousedown="this.href='https://groups.google.com/d/msgid/swift-language/a43bdcea-d2b8-40d4-8abd-654ac514c708%40googlegroups.com?utm_medium\75email\46utm_source\75footer';return true;" onclick="this.href='https://groups.google.com/d/msgid/swift-language/a43bdcea-d2b8-40d4-8abd-654ac514c708%40googlegroups.com?utm_medium\75email\46utm_source\75footer';return true;">https://groups.google.com/d/msgid/swift-language/a43bdcea-d2b8-40d4-8abd-654ac514c708%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" onmousedown="this.href='https://groups.google.com/d/optout';return true;" onclick="this.href='https://groups.google.com/d/optout';return true;">https://groups.google.com/d/optout.

--
Tom Lieber
<a href="http://AllTom.com/" target="_blank" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2FAllTom.com%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNFNQPyxA5OAReFbg0dYl6iAhMD3OA';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2FAllTom.com%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNFNQPyxA5OAReFbg0dYl6iAhMD3OA';return true;">http://AllTom.com/
<a href="http://infinite-sketchpad.com/" target="_blank" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2Finfinite-sketchpad.com%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNHcY9aWcUXn9zuhTwwb4ktn1teTjA';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2Finfinite-sketchpad.com%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNHcY9aWcUXn9zuhTwwb4ktn1teTjA';return true;">http://infinite-sketchpad.com/

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/f7fdcf6f-5a7b-4a1d-95df-0863a3307ca6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.