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

69 views
Skip to first unread message

Thomas Wetmore

unread,
Aug 4, 2014, 9:27:05 PM8/4/14
to swift-l...@googlegroups.com
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.

Tom Lieber

unread,
Aug 4, 2014, 9:45:02 PM8/4/14
to Thomas Wetmore, swift-l...@googlegroups.com
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!) {



--
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 swift-languag...@googlegroups.com.
To post to this group, send email to swift-l...@googlegroups.com.
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/

Thomas Wetmore

unread,
Aug 4, 2014, 9:50:36 PM8/4/14
to swift-l...@googlegroups.com, ttwet...@gmail.com, all...@gmail.com
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.
Reply all
Reply to author
Forward
0 new messages