{"id":284640,"date":"2017-04-08T04:50:02","date_gmt":"2017-04-08T00:50:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=284640"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=284640","title":{"rendered":"Kotlin \u0434\u043b\u044f Android: \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u043c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441\u043e \u0441\u043b\u0430\u0431\u044b\u043c\u0438 \u0441\u0441\u044b\u043b\u043a\u0430\u043c\u0438"},"content":{"rendered":"<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0445\u0430\u0431\u0440! \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0431\u044b \u0445\u043e\u0442\u0435\u043b \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043e\u043f\u044b\u0442\u043e\u043c \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u043e \u0441\u043b\u0430\u0431\u044b\u043c\u0438 \u0441\u0441\u044b\u043b\u043a\u0430\u043c\u0438 \u043f\u0440\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0434\u043b\u044f Android \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0430\u043a\u0442\u0438\u0432\u043d\u043e \u0440\u0430\u0437\u0432\u0438\u0432\u0430\u044e\u0449\u0435\u0433\u043e\u0441\u044f \u0441\u0435\u0439\u0447\u0430\u0441 \u044f\u0437\u044b\u043a\u0430 Kotlin.<\/p>\n<p>  <\/p>\n<p>\u042f \u0443\u0436\u0435 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0434\u043e\u043b\u0433\u043e \u043f\u0438\u0448\u0443 iOS \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Swift. \u0410 \u0435\u0449\u0435 \u0440\u0430\u043d\u044c\u0448\u0435 \u043f\u0438\u0441\u0430\u043b Android \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430 Pure Java 6. \u0418 \u0436\u0435\u043b\u0430\u043d\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u0443 \u043c\u0435\u043d\u044f \u043d\u0435 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u043b\u043e \u043d\u0438 \u043d\u0430 \u0441\u0435\u043a\u0443\u043d\u0434\u0443. \u041d\u043e \u043f\u043e \u0434\u043e\u043b\u0433\u0443 \u0441\u043b\u0443\u0436\u0431\u044b \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f. \u0412 \u044d\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u044f JetBrains \u0443\u0436\u0435 \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u0440\u0435\u043b\u0438\u0437 jvm-\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u043c\u043e\u0433\u043e \u044f\u0437\u044b\u043a\u0430 Kotlin (\u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u044c\u0438 \u2014 \u0432\u0435\u0440\u0441\u0438\u0438 1.1.1). \u0418 \u0442\u043e\u0433\u0434\u0430 \u044f \u0442\u0432\u0435\u0440\u0434\u043e \u0440\u0435\u0448\u0438\u043b, \u0447\u0442\u043e \u043c\u043e\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 \u0431\u0443\u0434\u0435\u0442 \u043d\u0435 \u043d\u0430 Java.<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h2 id=\"java\">Java<\/h2>\n<p>  <\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f, \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u043f\u0440\u043e\u0442\u043e\u0442\u0438\u043f\u0438\u0440\u0443\u0435\u0442\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438 \u0434\u0435\u043b\u0430\u0435\u0442\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043f\u0440\u044f\u043c\u043e \u0438\u0437 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 UI (\u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, Activity):<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">\/\/ MainActivity.java    void loadComments() {         api.getComments(new Api.ApiHandler&lt;Api.Comment&gt;() {             @Override             public void onResult(@Nullable List&lt;Api.Comment&gt; comments, @Nullable Exception exception) {                 if (comments != null) {                     updateUI(comments);                 } else {                     displayError(exception);                 }             }         });     }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u0440\u0438\u043c\u0438\u043d\u0430\u043b \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u0435\u043d. \u0410\u043d\u043e\u043d\u0438\u043c\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u0430 \u0434\u0435\u0440\u0436\u0438\u0442 \u0441\u0438\u043b\u044c\u043d\u0443\u044e \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 (\u043d\u0435\u044f\u0432\u043d\u043e\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e this$0 \u0432 \u0434\u0435\u0431\u0430\u0433\u0433\u0435\u0440\u0435), \u0447\u0442\u043e \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u043e, \u0435\u0441\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0440\u0435\u0448\u0438\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c Activity.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/401\/379\/32b\/40137932b12b4d4f8185956de82a4cb1.png\"\/>  <\/p>\n<p>\u0420\u0435\u0448\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u0443\u044e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u043c\u043e\u0436\u043d\u043e, \u0435\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u0430\u0431\u0443\u044e \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u043d\u0430\u0448\u0435 Activity:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">\/\/ MainActivity.java  void loadCommentsWithWeakReferenceToThis() {         final WeakReference&lt;MainActivity&gt; weakThis = new WeakReference&lt;&gt;(this);         api.getComments(new Api.SimpleApiHandler&lt;Api.Comment&gt;() {             @Override             public void onResult(@Nullable List&lt;Api.Comment&gt; comments, @Nullable Exception exception) {                 MainActivity strongThis = weakThis.get();                 if (strongThis != null)                      if (comments != null)                          strongThis.updateUI(comments);                      else                          strongThis.displayError(exception);             }         });     }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u044d\u0442\u043e \u043d\u0435 \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u041a\u0430\u043a \u0443\u043f\u043e\u043c\u0438\u043d\u0430\u043b\u043e\u0441\u044c \u0440\u0430\u043d\u0435\u0435, \u0430\u043d\u043e\u043d\u0438\u043c\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u0435\u0440\u0436\u0438\u0442 \u0441\u0438\u043b\u044c\u043d\u0443\u044e \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u043e\u0431\u044a\u0435\u043a\u0442, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0431\u044b\u043b \u0441\u043e\u0437\u0434\u0430\u043d.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/3f8\/5ad\/55a\/3f85ad55a8d647a89442e9ae73d77598.png\"\/>  <\/p>\n<p>\u0415\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u043b\u0430\u0431\u0443\u044e \u0441\u0441\u044b\u043b\u043a\u0443 (\u0438\u043b\u0438 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u0438) \u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u043e\u0431\u044a\u0435\u043a\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435 \u043f\u043e\u0434\u0432\u0435\u0440\u0436\u0435\u043d \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u043c\u0443 \u0446\u0438\u043a\u043b\u0443 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 (\u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0431\u044a\u0435\u043a\u0442 \u043a\u043b\u0430\u0441\u0441\u0430 Api):<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">\/\/ MainActivity.java  public class MainActivity extends AppCompatActivity implements Api.ApiHandler&lt;Api.Comment&gt; {  void loadCommentsWithWeakApi() {         api.getCommentsWeak(this);     }      @Override     public void onResult(@Nullable List&lt;Api.Comment&gt; comments, @Nullable Exception exception) {         if (comments != null)             updateUI(comments);         else             displayError(exception);     } <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"java\">\/\/ Api.java  class Api {     void getCommentsWeak(ApiHandler&lt;Comment&gt; handler) {         final WeakReference&lt;ApiHandler&lt;Comment&gt;&gt; weakHandler = new WeakReference&lt;&gt;(handler);         new Thread(new Runnable() {             @Override             public void run() {                 \u2026 \/\/ getting comments                 ApiHandler&lt;Comment&gt; strongHandler = weakHandler.get();                 if (strongHandler != null) {                     strongHandler.onResult(new ArrayList&lt;Comment&gt;(), null);                 }             }         }).start();     }     \u2026 }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u043c\u044b \u0441\u043e\u0432\u0441\u0435\u043c \u0438\u0437\u0431\u0430\u0432\u0438\u043b\u0438\u0441\u044c \u043e\u0442 \u0430\u043d\u043e\u043d\u0438\u043c\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430, \u043d\u0430\u0448\u0435 Activity \u0442\u0435\u043f\u0435\u0440\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u0430 Api \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434. \u0413\u0440\u043e\u043c\u043e\u0437\u0434\u043a\u043e. \u0417\u0430\u0442\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435\u0442 \u0443\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u044f \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 Activity.<\/p>\n<p>  <\/p>\n<p>\u041a\u0430\u043a \u0431\u044b \u044f \u0441\u0434\u0435\u043b\u0430\u043b \u0432 Swift:<\/p>\n<p>  <\/p>\n<pre><code>\/\/ ViewController.swift  func loadComments() {     api.getComments {[weak self] comments, error in        guard let `self` = self else { return }          if let comments = comments {              self.updateUI(comments)          } else {              self.displayError(error)          }     } } <\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0431\u044a\u0435\u043a\u0442 \u0437\u0430 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u043c <code>self<\/code> (\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a\u043e\u0435 \u0436\u0435, \u043a\u0430\u043a <code>this<\/code> \u0432 Java) \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u043b\u044f\u043c\u0431\u0434\u0443 \u043a\u0430\u043a \u0441\u043b\u0430\u0431\u0430\u044f \u0441\u0441\u044b\u043b\u043a\u0430.<\/p>\n<p>  <\/p>\n<p>\u0418 \u043d\u0430 Pure Java \u043c\u043d\u0435 \u0442\u0430\u043a\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0432\u0440\u044f\u0434 \u043b\u0438 \u0443\u0434\u0430\u0441\u0442\u044c\u0441\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c.<\/p>\n<p>  <\/p>\n<h2 id=\"probuem-kotlin\">\u041f\u0440\u043e\u0431\u0443\u0435\u043c Kotlin<\/h2>\n<p>  <\/p>\n<p>\u041f\u0435\u0440\u0435\u043f\u0438\u0448\u0435\u043c \u043d\u0430\u0448 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u043d\u0430 Kotlin:<\/p>\n<p>  <\/p>\n<pre><code>\/\/ MainActivity.kt    internal fun loadComments() {         api.getComments { list, exception -&gt;             if (list != null) {                 updateUI(list)             } else {                 displayError(exception!!)             }         }     } <\/code><\/pre>\n<p>  <\/p>\n<p>\u041b\u044f\u043c\u0431\u0434\u044b \u0432 Kotlin \u0431\u043e\u043b\u0435\u0435 \u0443\u043c\u043d\u044b\u0435 \u0438 \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u044e\u0442 \u0432 \u0441\u0435\u0431\u044f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0432 \u043d\u0435\u043c \u0441\u0430\u043c\u043e\u043c. \u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u043d\u0435\u043b\u044c\u0437\u044f \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u0437\u0430\u0445\u0432\u0430\u0442\u0430 (\u043a\u0430\u043a \u0432 C++ \u0438\u043b\u0438 \u0432 Swift), \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 Activity \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u0441\u0438\u043b\u044c\u043d\u0430\u044f:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/3be\/d22\/970\/3bed22970c074ba8bd4fa1156fb48234.png\"\/>  <\/p>\n<p>(\u0442\u0443\u0442 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u043a\u0430\u043a \u043b\u044f\u043c\u0431\u0434\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0435\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 <code>Function2&lt;T,V&gt;<\/code>)<\/p>\n<p>  <\/p>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e \u0447\u0442\u043e \u043d\u0430\u043c \u043c\u0435\u0448\u0430\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u043b\u0430\u0431\u0443\u044e \u0441\u0441\u044b\u043b\u043a\u0443 \u0432 \u043b\u044f\u043c\u0431\u0434\u0443:<\/p>\n<p>  <\/p>\n<pre><code>\/\/ MainActivity.kt  internal fun loadCommentsWeak() {         val thisRef = WeakReference(this)         api.getComments { list, exception -&gt;             val `this` = thisRef.get()             if (`this` != null)                  if (list != null) {                     `this`.updateUI(list)                 } else {                     `this`.displayError(exception!!)                 }         }     }<\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/4f0\/acf\/704\/4f0acf7047e049ddbb7b45ced45d14bc.png\"\/>  <\/p>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e \u0438\u0437 \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0430 \u0434\u0435\u0431\u0430\u0433\u0433\u0435\u0440\u0430, \u0443 \u043d\u0430\u0448\u0435\u0433\u043e \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435\u0442 \u043f\u0440\u044f\u043c\u043e\u0439 \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 Activity, \u0447\u0442\u043e \u0438 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043b\u043e\u0441\u044c \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f.<\/p>\n<p>  <\/p>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e \u0441\u0430\u0445\u0430\u0440 Kotlin \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u043c\u043d\u0435 \u0435\u0449\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u0440\u0438\u0431\u043b\u0438\u0437\u0438\u0442\u0441\u044f \u043a \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0443 Swift:<\/p>\n<p>  <\/p>\n<pre><code>\/\/ MainActivity.kt  internal fun loadCommentsWithMagic() {         val weakThis by weak(this)         api.getComments { list, exception -&gt;             val `this` = weakThis?.let { it } ?: return@getComments             if (list != null)                 `this`.updateUI(list)              else                 `this`.displayError(exception!!)         }     }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f <code>val A by B<\/code> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 A \u043e\u0431\u044a\u0435\u043a\u0442-\u0434\u0435\u043b\u0435\u0433\u0430\u0442 B, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0443\u0442 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c\u0441\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 A.<br \/>  <code>weak(this)<\/code> \u2014 \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f-\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 WeakRef  <\/p>\n<pre><code>\/\/ WeakRef.kt  class WeakRef&lt;T&gt;(obj: T? = null): ReadWriteProperty&lt;Any?, T?&gt; {      private var wref : WeakReference&lt;T&gt;?      init {         this.wref = obj?.let { WeakReference(it) }     }      override fun getValue(thisRef:Any? , property: KProperty&lt;*&gt;): T? {         return wref?.get()     }      override fun setValue(thisRef: Any?, property: KProperty&lt;*&gt;, value: T?) {         wref = value?.let { WeakReference(value) }     } }  \/\/ \u0422\u0430 \u0441\u0430\u043c\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f-\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 fun &lt;T&gt; weak(obj: T? = null) = WeakRef(obj) <\/code><\/pre>\n<p>  <\/p>\n<p>\u0411\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043f\u0440\u043e \u0434\u0435\u043b\u0435\u0433\u0438\u0440\u043e\u0432\u043d\u0438\u0435 \u0432 Kotlin \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c <a href=\"https:\/\/kotlinlang.org\/docs\/reference\/delegated-properties.html\">\u043d\u0430 \u0441\u0430\u0439\u0442\u0435 \u044f\u0437\u044b\u043a\u0430<\/a>.<\/p>\n<p>  <\/p>\n<h2 id=\"dobavim-esche-saharku\">\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0435\u0449\u0435 \u0441\u0430\u0445\u0430\u0440\u043a\u0443<\/h2>\n<p>  <\/p>\n<pre><code>\/\/ MainActivity.kt      internal fun loadCommentsWithSugar() {         val weakThis by weak(this)         api.getComments { list, exception -&gt; weakThis?.run {             if (list != null)                 updateUI(list)             else                 displayError(exception!!)         }}     }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u043a\u043e\u0434\u0430 \u043c\u044b \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0430\u0448\u0435\u0433\u043e Activity \u0434\u0430\u0436\u0435 \u0431\u0435\u0437 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u044f \u043a\u0430\u043a\u043e\u0433\u043e-\u0442\u043e \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u043a\u0430\u043a \u0431\u0443\u0434\u0442\u043e \u0441\u0441\u044b\u043b\u0430\u0435\u043c\u0441\u044f \u043d\u0430 \u043d\u0430\u0448\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0435 activity, \u0447\u0442\u043e \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0435\u0433\u043e \u0432 \u0445\u0435\u043d\u0434\u043b\u0435\u0440. \u0410 \u043c\u044b \u043e\u0442 \u044d\u0442\u043e\u0433\u043e \u0442\u0430\u043a \u0434\u043e\u043b\u0433\u043e \u043f\u044b\u0442\u0430\u043b\u0438\u0441\u044c \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u0441\u044f.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/019\/110\/5a3\/0191105a3fb3484aad62e5e0463d8c37.png\"\/>  <\/p>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e \u0438\u0437 \u0434\u0435\u0431\u0430\u0433\u0433\u0435\u0440\u0430, \u044d\u0442\u043e\u0433\u043e \u043d\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442.<\/p>\n<p>  <\/p>\n<p>\u0417\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e \u043b\u044f\u043c\u0431\u0434 \u0432 Kotlin \u2014 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432\u043b\u0430\u0434\u0435\u043b\u044c\u0446\u0430 (\u043a\u0430\u043a \u0432 Javascript). \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c <code>this<\/code> \u0432 \u043b\u044f\u043c\u0431\u0434\u0435 \u043f\u043e\u0441\u043b\u0435 <code>weakThis?.run<\/code> \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 Activity, \u043f\u0440\u0438\u0447\u0435\u043c \u0441\u0430\u043c\u0430 \u043b\u044f\u043c\u0431\u0434\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u0433\u0434\u0430, \u043a\u043e\u0433\u0434\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0435\u0449\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0430\u043c\u044f\u0442\u0438. \u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>run()<\/code> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\u043c \u043b\u044e\u0431\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043b\u044f\u043c\u0431\u0434\u0443 \u0441 \u0432\u043b\u0430\u0434\u0435\u043b\u044c\u0446\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043e\u043d\u043e \u0432\u044b\u0437\u0432\u0430\u043d\u043e (\u0430 \u0435\u0449\u0435 \u0435\u0441\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u0435 \u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0442\u0438\u043f\u0430 <code>let()<\/code>, <code>apply()<\/code>, <code>also()<\/code> \u0438 \u0442\u043f).<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/727\/388\/afe\/727388afe07a4636bd50502b42e6f68d.png\"\/>  <\/p>\n<p>\u0412 \u0434\u0435\u0431\u0430\u0433\u0433\u0435\u0440\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e <code>this<\/code> \u0443 \u043b\u044f\u043c\u0431\u0434\u044b \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a <code>$receiver<\/code>.<\/p>\n<p>  <\/p>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043f\u0440\u043e \u043b\u044f\u043c\u0431\u0434\u044b \u0432 Kotlin \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 <a href=\"https:\/\/kotlinlang.org\/docs\/reference\/lambdas.html\">\u043d\u0430 \u0441\u0430\u0439\u0442\u0435 \u044f\u0437\u044b\u043a\u0430<\/a>.<\/p>\n<p>  <\/p>\n<p>\u041f\u043e\u0434 \u043a\u043e\u043d\u0435\u0446 \u043c\u043e\u0436\u043d\u043e \u0435\u0449\u0435 \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0440\u043e\u043a (\u043d\u043e \u043d\u0435 \u0441\u043a\u043e\u0431\u043e\u043a)<\/p>\n<p>  <\/p>\n<pre><code>\/\/ MainActivity.kt  internal fun loadCommentsWithDoubleSugar() = this.weaked().run { api.getComments { list, exception -&gt; this.get()?.run {             if (list != null)                 updateUI(list)             else                 displayError(exception!!)     }}}  \/\/ weakref.kt  fun &lt;T&gt;T.weaked() = WeakReference(this) <\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"vyvody\">\u0412\u044b\u0432\u043e\u0434\u044b<\/h2>\n<p>  <\/p>\n<p>Kotlin \u2014 \u043e\u0442\u043b\u0438\u0447\u043d\u0430\u044f \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0430 Java \u0434\u043b\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0434\u043b\u044f JVM-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439. \u041e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u0432\u044b\u0448\u0435 \u043f\u0440\u0438\u0435\u043c\u044b \u2014 \u043c\u0435\u043d\u044c\u0448\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439 \u044f\u0437\u044b\u043a\u0430.<br \/>  \u041e\u0434\u0438\u043d \u0438\u0437 \u043f\u0440\u0438\u0432\u043b\u0435\u043a\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0444\u0430\u043a\u0442\u043e\u0432 \u0434\u043b\u044f Swift-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u2014 <a href=\"http:\/\/nilhcem.com\/swift-is-like-kotlin\/\">\u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u043e\u0445\u043e\u0436\u0438\u0439 c\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441<\/a>.<br \/>  \u041f\u043e \u0441\u0432\u043e\u0435\u043c\u0443 \u043e\u043f\u044b\u0442\u0443 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043d\u0438\u043c \u043c\u043e\u0433\u0443 \u0443\u0442\u0432\u0435\u0440\u0436\u0434\u0430\u0442\u044c, \u0447\u0442\u043e \u043f\u0440\u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043e\u043d \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u0434\u0430 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 Java (\u043d\u0435 \u0431\u0435\u0440\u0443\u0441\u044c \u0443\u0442\u0432\u0435\u0440\u0436\u0434\u0430\u0442\u044c \u0437\u0430 \u0443\u0442\u0438\u043b\u0438\u0442\u043d\u044b\u0439 \u043a\u043e\u0434).<\/p>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u043b\u0435\u043d\u0438\u044f \u0441\u043e \u0432\u0441\u0435\u043c\u0438 \u0444\u0438\u0447\u0430\u043c\u0438 \u044f\u0437\u044b\u043a\u0430 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c <a href=\"https:\/\/kotlinlang.org\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e<\/a>.<br \/>  \u041a\u0430\u043a \u0438\u043d\u0442\u0435\u0440\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c Kotlin \u0432 Android \u043f\u0440\u043e\u0435\u043a\u0442 \u043c\u043e\u0436\u043d\u043e \u0443\u0437\u043d\u0430\u0442\u044c <a href=\"https:\/\/kotlinlang.org\/docs\/tutorials\/kotlin-android.html\">\u0437\u0434\u0435\u0441\u044c<\/a>.<br \/>  \u0418\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u043d\u0430 <a href=\"https:\/\/github.com\/ivabra\/KotlinWeakReferenceDemo\">git<\/a>.<\/p>\n<\/p>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habrahabr.ru\/post\/325966\/\"> https:\/\/habrahabr.ru\/post\/325966\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0445\u0430\u0431\u0440! \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0431\u044b \u0445\u043e\u0442\u0435\u043b \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043e\u043f\u044b\u0442\u043e\u043c \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u043e \u0441\u043b\u0430\u0431\u044b\u043c\u0438 \u0441\u0441\u044b\u043b\u043a\u0430\u043c\u0438 \u043f\u0440\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0434\u043b\u044f Android \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0430\u043a\u0442\u0438\u0432\u043d\u043e \u0440\u0430\u0437\u0432\u0438\u0432\u0430\u044e\u0449\u0435\u0433\u043e\u0441\u044f \u0441\u0435\u0439\u0447\u0430\u0441 \u044f\u0437\u044b\u043a\u0430 Kotlin.<\/p>\n<p>  <\/p>\n<p>\u042f \u0443\u0436\u0435 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0434\u043e\u043b\u0433\u043e \u043f\u0438\u0448\u0443 iOS \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Swift. \u0410 \u0435\u0449\u0435 \u0440\u0430\u043d\u044c\u0448\u0435 \u043f\u0438\u0441\u0430\u043b Android \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430 Pure Java 6. \u0418 \u0436\u0435\u043b\u0430\u043d\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u0443 \u043c\u0435\u043d\u044f \u043d\u0435 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u043b\u043e \u043d\u0438 \u043d\u0430 \u0441\u0435\u043a\u0443\u043d\u0434\u0443. \u041d\u043e \u043f\u043e \u0434\u043e\u043b\u0433\u0443 \u0441\u043b\u0443\u0436\u0431\u044b \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f. \u0412 \u044d\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u044f JetBrains \u0443\u0436\u0435 \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u0440\u0435\u043b\u0438\u0437 jvm-\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u043c\u043e\u0433\u043e \u044f\u0437\u044b\u043a\u0430 Kotlin (\u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u044c\u0438 \u2014 \u0432\u0435\u0440\u0441\u0438\u0438 1.1.1). \u0418 \u0442\u043e\u0433\u0434\u0430 \u044f \u0442\u0432\u0435\u0440\u0434\u043e \u0440\u0435\u0448\u0438\u043b, \u0447\u0442\u043e \u043c\u043e\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 \u0431\u0443\u0434\u0435\u0442 \u043d\u0435 \u043d\u0430 Java.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-284640","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/284640","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=284640"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/284640\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=284640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=284640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=284640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}