{"id":207480,"date":"2025-07-08T11:43:22","date_gmt":"2025-07-08T03:43:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207480\/"},"modified":"2025-07-08T11:43:22","modified_gmt":"2025-07-08T03:43:22","slug":"%e5%a6%82%e4%bd%95%e5%a3%b0%e6%98%8e%e5%bf%85%e9%a1%bb%e8%bf%94%e5%9b%9e%e5%85%b6%e5%8f%82%e6%95%b0%e4%b9%8b%e4%b8%80%e7%9a%84%e5%87%bd%e6%95%b0%e7%9a%84%e7%ad%be%e5%90%8d%ef%bc%9f-%ef%bc%88%e4%bb%bb","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207480\/","title":{"rendered":"\u5982\u4f55\u58f0\u660e\u5fc5\u987b\u8fd4\u56de\u5176\u53c2\u6570\u4e4b\u4e00\u7684\u51fd\u6570\u7684\u7b7e\u540d\uff1f \uff08\u4efb\u4f55\u8bed\u8a00*\uff09"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5982\u4f55\u58f0\u660e\u5fc5\u987b\u8fd4\u56de\u5176\u53c2\u6570\u4e4b\u4e00\u7684\u51fd\u6570\u7684\u7b7e\u540d\uff1f \uff08\u4efb\u4f55\u8bed\u8a00*\uff09<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 22:36:36<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300a\u5982\u4f55\u58f0\u660e\u5fc5\u987b\u8fd4\u56de\u5176\u53c2\u6570\u4e4b\u4e00\u7684\u51fd\u6570\u7684\u7b7e\u540d\uff1f \uff08\u4efb\u4f55\u8bed\u8a00*\uff09\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5982\u4f55\u8868\u8fbe <code>function<\/code> \u7684\u7b7e\u540d\uff0c\u8be5\u7b7e\u540d<strong>\u5fc5\u987b<\/strong>\u8fd4\u56de\u5b83\u63a5\u6536\uff08\u8c03\u7528\uff09\u7684\u53c2\u6570\uff08\u6216 <code>this<\/code>\uff09\uff0c\u5728 typescript \u4e2d\uff1f <em>\u662f\u5426\u6709\u4e00\u79cd\u7f16\u7a0b\u8bed\u8a00\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\uff1f*<\/em><\/p>\n<pre>\/\/ in typescript (or consider it pseudo-code)\nclass c {\n  \/\/ example 1 \u2013 not polymorphic\n  chainable(x): this                 \/\/ must not only return some c,\n  {}                                 \/\/ but the same instance it was called on\n}\n\/\/ example 2\nfunction mutate&lt;t&gt;(a: t[], x): t[]   \/\/ must return a, not a new array\n{\n  \/* so that this doesn't compile *\/ return array.from(a);\n  \/* but this is ok *\/               return a;\n}\n<\/pre>\n<p>\u76f8\u53cd\uff0c\u5982\u679c <code>function<\/code> <strong>\u5fc5\u987b<\/strong>\u8fd4\u56de\u4e00\u4e2a\u65b0\u5b9e\u4f8b\u5462\uff1f<\/p>\n<pre>\/\/ example 3\nfunction slice&lt;t&gt;(a: t[], x, y): t[] \/\/ must return a new array\n<\/pre>\n<h3>typescript<\/h3>\n<h3>\u53bb2\uff1f<\/h3>\n<p>\u4ee5\u4e0b <code>contract<\/code> \u80fd\u5426\u5b9e\u73b0\u4e0a\u8ff0\u76ee\u7684\uff1f<\/p>\n<pre>contract referentiallyIdentical(f F, p P) {\n  f(p) == p\n  v := *p\n}\ntype returnsSameIntSlice(type T, *[]int referentiallyIdentical) T\nfunc main() {\n  var mutate returnsSameIntSlice = func(a *[]int) *[]int {\n    b := []int{2}\n    \/* Would this compile? *\/ return &amp;b\n    \/* This should *\/         return a\n  }\n}  \n<\/pre>\n<h3>c++20\uff1f<\/h3>\n<p>\u4e0a\u9762\u7684\u5185\u5bb9\u53ef\u4ee5\u8868\u793a\u4e3a c++ <code>concept<\/code> \u5417\uff1f<\/p>\n<h3>scala<\/h3>\n<p>*\u6700\u521d\uff0c\u95ee\u9898\u662f\u5173\u4e8e\u5728 typescript \u4e2d\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u4f46\u7531\u4e8e\u8fd9\u662f\u4e0d\u53ef\u80fd\u7684\uff0c\u6211\u5f88\u597d\u5947\u5b83\u662f\u5426\u4f7f\u7528\u5176\u4ed6\u8bed\u8a00\u3002<\/p>\n<p><em>\u5982\u679c\u8be5\u8bed\u8a00\u7684\u7c7b\u578b\u7cfb\u7edf\u65e0\u6cd5\u8868\u8fbe\u8fd9\u4e00\u70b9\uff0c\u8bf7\u968f\u610f\u5220\u9664\u6807\u7b7e<\/em><\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u4f60\u53ef\u4ee5 &#8211; \u5728 scala \u4e2d\u3002<\/p>\n<p>\u5177\u6709\u8fd4\u56de <code>this.type<\/code> \u65b9\u6cd5\u7684\u7c7b\uff1a<\/p>\n<pre>class c {\n  var x = 0 \n\n  \/** sets `x` to new value `i`, returns the same instance. *\/\n  def with_x(i: int): this.type = {\n    x = i\n    this   \/\/ must be `this`, can't be arbitrary `c`\n  } \n}<\/pre>\n<p>\u4fdd\u8bc1\u8fd4\u56de\u5b8c\u5168\u76f8\u540c\u7684\u6570\u7ec4\u7684\u5c31\u5730\u6392\u5e8f\uff08\u6b64\u5904\u5e76\u4e0d\u771f\u6b63\u5bf9\u4efb\u4f55\u5185\u5bb9\u8fdb\u884c\u6392\u5e8f\uff09\uff1a<\/p>\n<pre>def sortinplace[a: ordered](arr: array[a]): arr.type = {\n  \/* do fancy stuff with indices etc. *\/\n  arr\n}<\/pre>\n<p>\u5982\u679c\u60a8\u5c1d\u8bd5\u8fd4\u56de\u4e0d\u540c\u7684\u6570\u7ec4\uff0c<\/p>\n<pre>def badsortinplace(arr: array[int]): arr.type = array(1, 2, 3) \/\/ won't compile<\/pre>\n<p>\u4f60\u4f1a\u5728\u7f16\u8bd1\u65f6\u5f97\u5230\u4e00\u4e2a\u9519\u8bef\uff1a<\/p>\n<pre>error: type mismatch;\nfound   : array[int]\nrequired: arr.type\n      def badsortinplace(arr: array[int]): arr.type = array(1, 2, 3)\n                                                           ^<\/pre>\n<p>\u8fd9\u79f0\u4e3a \u3002<\/p>\n<p>\u5728\u5177\u6709\u53c2\u6570\u591a\u6001\u6027\u7684\u8bed\u8a00\u4e2d\uff0c\u4efb\u4f55\u7c7b\u578b\u7684\u51fd\u6570<\/p>\n<pre>a \u2192 a<\/pre>\n<p><em>\u5fc5\u987b<\/em>\u662f\u6052\u7b49\u51fd\u6570\uff1a\u7531\u4e8e\u8be5\u51fd\u6570\u5728 <code>a<\/code> \u4e2d\u662f\u591a\u6001\u7684\uff0c\u56e0\u6b64\u5b83\u4e0d\u53ef\u80fd\u77e5\u9053\u6709\u5173 <code>a<\/code> \u7684\u4efb\u4f55\u4fe1\u606f\uff0c\u7279\u522b\u662f\u5b83\u4e0d\u53ef\u80fd\u77e5\u9053\u5982\u4f55\u6784\u9020 <code>a<\/code>\u3002\u7531\u4e8e\u5b83\u4e5f\u4e0d\u91c7\u7528\u4e16\u754c\u503c\u6216 <code>io<\/code> monad \u6216\u7b49\u6548\u7684\u4e1c\u897f\uff0c\u56e0\u6b64\u5b83\u65e0\u6cd5\u4ece\u5168\u5c40\u72b6\u6001\u3001\u6570\u636e\u5e93\u3001\u7f51\u7edc\u3001\u5b58\u50a8\u6216\u7ec8\u7aef\u83b7\u53d6\u503c\u3002\u5b83\u4e5f\u4e0d\u80fd\u5220\u9664\u8be5\u503c\uff0c\u56e0\u4e3a\u5b83\u5fc5\u987b\u8fd4\u56de <code>a<\/code>\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u5b83\u552f\u4e00\u80fd\u505a\u7684\u5c31\u662f\u8fd4\u56de\u4f20\u5165\u7684 <code>a<\/code>\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207480","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207480","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207480"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207480\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}