

Swift for Rubyists: Optionals - nottombrown
http://www.nottombrown.com/blog/2014/08/14/swift-for-rubyists-optionals/

======
anorwell
You don't touch on the fact that options act can act Monads (i.e., that you
can invoke an operation on the inner value, conditional on whether or not the
optional value is present). For example, an optional user translates naturally
to an optional user.name.

I haven't tried swift, and it's not obvious to me from the swift documentation
if their options support arbitrary transformations, or just method chaining.
In ruby (with ActiveSupport) this is the difference between:

    
    
          might_be_nil.try(:method)
    

and

    
    
          might_be_nil.try { |value| my_function(value) }
    

The latter is more general and useful than the former.

~~~
nottombrown
Here's the source of try from active_support

    
    
        def try(*a, &b)
          if a.empty? && block_given?
            yield self
          else
            public_send(*a, &b) if respond_to?(a.first)
          end
        end
    

[https://github.com/rails/rails/blob/d68419a88e424e588c2d8dec...](https://github.com/rails/rails/blob/d68419a88e424e588c2d8decc69874bd00588766/activesupport/lib/active_support/core_ext/object/try.rb)

Should be easy to do the same in swift. Let me take a stab at it.

------
krisdol
Is this different than setting a default value of nil to user?

    
    
        def print_favorite_dinosaur(user: nil)

or

    
    
        def print_favorite_dinosaur(user = nil)
    

Or is it effectively a different syntax for the same concept?

In either language, you still signal that the argument could be nil, and you
still have to guard against it. Or so I think

~~~
nottombrown
In Swift, you _have to_ signal and guard against the nil.

In ruby, you choose whether to signal and you choose whether to guard. Here
are some ruby examples that may be more clear:

 _Does not signal_

    
    
        user = User.find_by_favorite_dinosaur("Dromiceiomimus")
    

_Does not guard_

    
    
        def print_favorite_dinosaur(user)
            p "User's favorite dinosaur is #{user.dino})"
        end
    

_Signals and guards_

    
    
        def print_favorite_dinosaur(user)
            if user
                p "User's favorite dinosaur is #{user.dino})"
            else
                p "No user. (Let's assume she likes T-rexs)"
            end
        end
    
        possible_user = User.find_by_favorite_dinosaur("Dromiceiomimus")
    
        print_favorite_dinosaur(possible_user)

~~~
krisdol
Thanks, I didn't know that swift requires the optional to be guarded against.

------
nottombrown
Author here. Let me know your thoughts, concerns, and favorite dinosaur.

~~~
notduncansmith
Nothing to do with the article, but I think it's interesting to see someone
else with the "not#{first_name}#{last_name}" pattern. I didn't know that was a
common thing.

~~~
nottombrown
Totally trending. All of us Smiths, Browns, and Johnsons have to do something
to claim our handles.

[http://names.mongabay.com/most_common_surnames.htm](http://names.mongabay.com/most_common_surnames.htm)

