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.
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.
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)
10 comments
[ 3.2 ms ] story [ 28.2 ms ] threadhttp://names.mongabay.com/most_common_surnames.htm
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:
and The latter is more general and useful than the former.Should be easy to do the same in swift. Let me take a stab at it.
https://gist.github.com/nottombrown/c6585cad9588a9dc5383
In either language, you still signal that the argument could be nil, and you still have to guard against it. Or so I think
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
Does not guard Signals and guards