TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Swift for Rubyists: Optionals

17 pointsby nottombrownalmost 11 years ago

3 comments

anorwellalmost 11 years ago
You don&#x27;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.<p>I haven&#x27;t tried swift, and it&#x27;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:<p><pre><code> might_be_nil.try(:method) </code></pre> and<p><pre><code> might_be_nil.try { |value| my_function(value) } </code></pre> The latter is more general and useful than the former.
评论 #8180078 未加载
评论 #8180261 未加载
krisdolalmost 11 years ago
Is this different than setting a default value of nil to user?<p><pre><code> def print_favorite_dinosaur(user: nil)</code></pre> or<p><pre><code> def print_favorite_dinosaur(user = nil) </code></pre> Or is it effectively a different syntax for the same concept?<p>In either language, you still signal that the argument could be nil, and you still have to guard against it. Or so I think
评论 #8180135 未加载
nottombrownalmost 11 years ago
Author here. Let me know your thoughts, concerns, and favorite dinosaur.
评论 #8180142 未加载