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.

Optional Value Handling in Nim

43 pointsby mikenewover 3 years ago

5 comments

jlokierover 3 years ago
One of the things I don&#x27;t like about Option[T] in Nim is when you are handed an optional large structure as a function argument, such as &quot;Option[seq[Thing]]&quot; or &quot;Option[BigObject]&quot;, or those types as fields of an object passed as argument.<p>It works fine, but the act of unwrapping the value inside with x.get <i>copies</i> the large data structure inside to return it. So it&#x27;s incredibly slow to access the value inside. Even one such access can be unacceptably slow.<p>Receiving the optional itself as a function argument is fast, because Nim passes a pointer for those arguments. The problem is when you need to use the value wrapped inside.<p>Nim encourages &quot;value oriented programming&quot;, where things are written as value types, on the assumption that most of the time Nim is able to use temporary read-only aliases to data instead of copies. So this is the style of passing optionals around that I see in typical code.<p>If you have been handed a mutable option (&quot;var Option[..]&quot;) that&#x27;s different. Then you can unwrap the value inside as a mutable as well, which avoids the copy.<p>But you don&#x27;t tend to be handed a mutable option in a function argument, except from code that was written in an unusual style with performance in mind, and a contract outside the type system: &quot;I&#x27;m giving you a var, but only for performance reasons; don&#x27;t write to it!&quot;.
评论 #28415085 未加载
elcritchover 3 years ago
The comparison of Nim to Ada seems appropriate. I&#x27;ve never used Ada but wrote a paper on it in school and found the concepts intruiging. Pretty interesting that a design by committee language had such rich integer types.<p>Nim looks like Python but really it reminds me more of a nicer looking Pascal. It&#x27;s been great having ranged integers in Nim, especially when doing embedded work, without having the verbosity of Ada.
评论 #28364208 未加载
nine_kover 3 years ago
The key idea: Optional[T] is more ergonomic through pattern matching, because inside the matched case, we definitely know whether it&#x27;s carrying a value or not. The whole problem of nullability gets an elegant solution.<p>The fun premise from the beginning is that booleans do not need their special type, they are effectively Optional[Void], or for FP-heads here, Optional[Unit]. That is, it just does not need to carry a value. But when an Optional <i>does</i> carry a value, a lot of if-then-else logic for handling that value just gets eliminated, replaced by a pattern match that combines both the boolean aspect and the value-handling aspect.
评论 #28366097 未加载
评论 #28364984 未加载
评论 #28366530 未加载
Bromeoover 3 years ago
This reads a lot like Rust does it&#x27;s error-handling too.
评论 #28364329 未加载
michaelcampbellover 3 years ago
&lt;tangent&gt; Article content quality aside, I found the layout absolutely delightful.