Apple Swift Optionals Syntax Rethought

NOTE: This is pretty stream of consciousness stuff. I’m just throwing out ideas.

In the Swift language an optional is a variable that can have an unknown value.  Let’s start with that. I’m not happy with the syntax of dealing with optionals, not that they exist since they have a real purpose.

First, what are the ways in which a variable can become unknown?

• Programming errors, such as forgetting to initialize an optional before trying to use it,

• Code setting it to an unknown value due to:

  • Bad code/memory access
  • External inputs
  • A real life representation of an unknown value (such as an unknown age).

This last really represents the interesting case since it moves us from binary logic to trinary:

  • Yes
  • No
  • I do’t know

Trinary logic lets code deal with the real world in more realistic ways and is similar to NULL values in SQL that can be queried. But, that’s beside the point.

Now lets look at some simple Swift code dealing with optionals:

Screen Shot 2015-08-28 at 9.23.56 AM

This is ok and people like this but I find it feels clunky having to test for unknowns everywhere with the if let idiom when the runtime should be able to do this. There is a lot of inline “boilerplate” that interrupts the logic flow just to handle the exceptional case. Handling unknown optionals inline breaks up the intent of the code.

What if we pulled handling unknown optionals into their own scope?  Let’s steal an idea from COBOL (yes!) and invent a new construct and call it wheneverUnknown.  wheneverUnknown would be invoked automatically by the runtime whenever [no pun intended] it detected an unknown value in an optional.  Think of this as try-catch routing except for unknowns and they are handled out of line from the main logic, which actually helps factor the code.

So what would the new code look like?  Here is a mockup.

Screen Shot 2015-08-28 at 9.21.39 AM

This, to me, seems more streamlined, simpler to “grok”, and is not inline.  A couple of things to note here.  The code after line 49 is only executed if the runtime encounters an unknown valued optional, so for all intents and purposes the logic ends at line 45.

The next thing to note is that we never handled the exception at line 45. This would be an example of forgetting to initialize an optional (bug) as opposed to the other two cases where we may not know the names.  How do we handle this bug? Let’s expand our wheneverUnknown  scope capabilities.  The wheneverUnknown examples in this function are scoped to the function.  Let’s do the same thing at the class level.

Screen Shot 2015-08-28 at 9.30.56 AM

Lines 33-36 is a similar wheneverUnknown that adds an any keyword which would trap any unhandled unknown and is class-scoped.  Actually, this could be moved into the function itself and would work similarly, but would be scoped to the function alone.

No, I have no plans to implement this.  The idea came up looking at a Twitter post and I got into a short interchange about this idea and really couldn’t get the idea across in tweets.  I just wanted to expand on the idea and clarify my thoughts a bit.

Enblue Apple Watch Stand Review

I have an Apple Watch. I needed something better to charge it than just the provided mag charger on a string that Apple provides. After looking around a bit I found a Kickstarter, Enblue http://enbluetec.com .

I ordered the Premium W3 http://enbluetec.com/premiumonew3/ which is a charging station for not only the Apple Watch, but the iPhone and iPad as well.  Today, I just got the box and set it up.

The box came with the stand and no real instructions.  This is what is included:

 

IMG_3075

This is the main stand with room and connectors for all three devices.  Nice brushed aluminum, and rubber-padded connectors.  The stand has a pre-wired Lightning to USB connector for the iPhone and iPad.  The Lightning connectors are on a “floating” bar that lets your device adjust nicely.  The USB ends are labelled with a red and green dot. I haven’t figured out which goes to which device.  I don’t think it’s important.

You use and provide the Apple Watch MagSafe™ charger for the Apple Watch.

The other main component that is included is a USB charger (to the right in the picture above) with 4 USB-3 sockets.  Between the two provided USB cables and the MagSafe’s cable this leaves one empty USB socket free. Nice!

The one thing that confused me “out of the box” is that there was a rattling noise from inside the stand–as if there were loose parts.  Sure enough with enough shaking I retrived 4 small screws and two tiny cross-panels.  I think these were supposed to be in the two small plastic bags (empty) that you can see in the picture.

Setup is straight forward–mostly.  It wan’t obvious how the Apple Watch cord was to be routed. It was obvious the MagSafe went into the provided plastic grommet.  This has a notch, and I guessed the cord went there.  But neither the USB nor the MagSafe connectors fit in the slot in the stand.  The arm that holds the Apple Watch looked like it had a panel but there was no obvious way to remove it.  I decided to gamble and potentially mar or damage the stand and used a screwdriver to pry the panel off.

Success! It turns out it is magnetically attached for the most part.  With the panel open, it was a simple process of snaking the USB connector of the Apple Watch charger through the stand and aligning the cord with the notch in the grommet.  Here is a picture of the open panel and cord.

 

IMG_3076

The magnetic latch is on the bottom of the arm in this picture. You can also see the extra parts in this picture.

Once I reseated the panel back into the arm, I wondered what to do with the extra screws and cross panels.  The only place that made sense was securing one of the cross panels on the bottom.

IMG_3077

I tried once but lost a screw inside the stand (which I had to shake out again).  It wasn’t worth the effort and I didn’t see the panel really securing anything anyway. So I just left that bit off. It would have made more sense to have the cross panel across the wire channel.

Now all that was left was plugging all of the USB connectors into the power supply.  Here is the finished product.

 

IMG_3081

This is a really nice charging solution.  It is a bit pricey but it is solid aluminum.  The only way to improve this would be to have an option that worked as a MacBook docking station.  Or to include a simple instruction sheet.