TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: Any OOP language which enforces / encourages purity / immutability?

2 点作者 nawfalhasan超过 8 年前
I&#x27;m a c# developer and working on large projects made me realise how hard is it to maintain projects with side effects everywhere.<p>Which made me wonder if there are any OOP languages out there which has a strong emphasis on immutability. For e.g. syntax that lets user construct an object only on instantiation, syntax to easily clone an object, syntax to differentiate between a pure function and an impure one etc.<p>Note:<p>1. I do realise many people define OOP as stateful and what I am describing above is fundamentally against such OOP, and also this problem is solved by functional languages. But what I mean by object oriented is a paradigm that keeps classes&#x2F;types&#x2F;structs as fundamental building blocks to model problem domain, which has behaviour (methods) on state, and one which has essentially &#x27;.&#x27; (dot) syntax. For e.g. a language where I can do:<p>class Person (name, bday) { ...<p><pre><code> func int age() { &#x2F;&#x2F; calculated from bday state }</code></pre> }<p>&#x2F;&#x2F; And later var p = new Person (...) var age = p.age()<p>2. By mutability I mean mutating an object (changing its state), not reinitializing the variable. I&#x27;m OK with latter.<p>First time on HN, hopefully my question is within the rules. Thanks everyone!

2 条评论

brudgers超过 8 年前
Some random internet remarks:<p>0. This looks like the why in &#x27;why are there new languages?&#x27;<p>1. Objects are a place based abstraction that provides the ability to distinguish between &#x27;this&#x27; and &#x27;that&#x27; in the same way that two identically optioned BMW 325i&#x27;s may be distinguished, e.g. mine and yours.<p>2. Immutability is a value based abstraction in which two identically optioned BMW&#x27;s are fungible (e.g. mine and yours while they are sitting on the dealer&#x27;s lot awaiting sale).<p>3. The simplest object like data structure is a hash&#x2F;hash map&#x2F;hash table. An immutable hash table can probably provide all of the semantics you describe.<p>4. There are lots of languages with immutable hash tables.<p>5. Ruby is one of them and:<p><pre><code> a. Ruby uses the . syntax. b. Everything in Ruby is an object. c. To a first approximation, any object in Ruby may have any of its methods over-ridden&#x2F;shadowed&#x2F;redefined d. The Ruby ecosystem supports writing new embedded languages, e.g. Rails. c. The downside is that, historically, a first class Windows experience has not been a priority of the Ruby community. </code></pre> My random advice is to not get hung up on the dot syntax until it is clear that the semantics actually do what you want and therefore to mock up the semantics using hash-tables in a language that expects them to be immutable such as Clojure...hash-tables are more or less why Clojure does not have an object system.<p>Good luck.
评论 #13808088 未加载
ankurdhama超过 8 年前
Have a look at Swift.
评论 #13802610 未加载