{"id":883,"date":"2025-08-31T16:57:04","date_gmt":"2025-08-31T16:57:04","guid":{"rendered":"https:\/\/nurlandroid.com\/?p=883"},"modified":"2025-08-31T16:57:05","modified_gmt":"2025-08-31T16:57:05","slug":"kotlin-contracts-%d0%b7%d0%b0%d1%87%d0%b5%d0%bc-%d0%bd%d1%83%d0%b6%d0%bd%d1%8b-%d0%b3%d0%b4%d0%b5-%d0%b8%d1%81%d0%bf%d0%be%d0%bb%d1%8c%d0%b7%d0%be%d0%b2%d0%b0%d1%82%d1%8c","status":"publish","type":"post","link":"https:\/\/nurlandroid.com\/?p=883","title":{"rendered":"Kotlin Contracts \u0437\u0430\u0447\u0435\u043c \u043d\u0443\u0436\u043d\u044b? \u0413\u0434\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c?"},"content":{"rendered":"\n<p><strong>Contracts<\/strong> \u0432 Kotlin \u2014 \u044d\u0442\u043e \u0441\u043f\u043e\u0441\u043e\u0431 <strong>\u0441\u043e\u043e\u0431\u0449\u0438\u0442\u044c \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0443<\/strong> \u043e \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 <strong>\u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0440\u0430\u0437\u0438\u0442\u044c \u0442\u0438\u043f\u0430\u043c\u0438<\/strong>.<\/p>\n\n\n\n<p>\u041e\u0431\u044b\u0447\u043d\u043e \u044d\u0442\u043e:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043b\u0438 \u043b\u044f\u043c\u0431\u0434\u0430<\/li>\n\n\n\n<li>\u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043b\u0438 return<\/li>\n\n\n\n<li>\u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u0441\u0434\u0435\u043b\u0430\u0442\u044c smart cast \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u0417\u0430\u0447\u0435\u043c \u043d\u0443\u0436\u043d\u044b Kotlin Contracts?<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u0423\u043b\u0443\u0447\u0448\u0438\u0442\u044c <strong>smart cast&#8217;\u044b<\/strong><\/li>\n\n\n\n<li>\u0413\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0447\u0442\u043e \u043b\u044f\u043c\u0431\u0434\u0430 \u0432\u044b\u0437\u043e\u0432\u0435\u0442\u0441\u044f <strong>\u0440\u043e\u0432\u043d\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437<\/strong><\/li>\n\n\n\n<li>\u0423\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0447\u0442\u0435\u043d\u0438\u0435 \u043a\u043e\u0434\u0430 \u0438 \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 warning-\u043e\u0432<\/li>\n\n\n\n<li>\u041f\u043e\u0432\u044b\u0441\u0438\u0442\u044c <strong>\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c<\/strong> \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 <code>check()<\/code>, <code>require()<\/code>, <code>assert()<\/code>, <code>ifNull()<\/code><\/li>\n<\/ol>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u041f\u0440\u0438\u043c\u0435\u0440 1: Smart cast \u0431\u0435\u0437 \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442\u0430<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfun isString(obj: Any?): Boolean = obj is String\n\nfun test(x: Any?) {\n    if (isString(x)) {\n        println(x.length) \/\/ \u041e\u0448\u0438\u0431\u043a\u0430: x \u043d\u0435 smart cast-\u0438\u0442\u0441\u044f \u0432 String\n    }\n}\n<\/pre><\/div>\n\n\n<p>\u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 <strong>\u043d\u0435 \u0437\u043d\u0430\u0435\u0442<\/strong>, \u0447\u0442\u043e <code>isString(x)<\/code>==<code>true<\/code>, \u0442\u043e \u0435\u0441\u0442\u044c <code><strong>x<\/strong><\/code> \u0442\u043e\u0447\u043d\u043e <code>String<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u041f\u0440\u0438\u043c\u0435\u0440 2: \u0421 \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442\u043e\u043c<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ninline fun &amp;lt;T&gt; Any?.isNotNullAndCast(): Boolean {\n    contract {\n        returns(true) implies (this@isNotNullAndCast is T)\n    }\n    return this is T\n}\n\n\/\/\/....\nif (x.isNotNullAndCast&amp;lt;String&gt;()) {\n    println(x.length) \/\/  OK \u2014 smart cast \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442\n}\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Contracts \u0432 Kotlin \u2014 \u044d\u0442\u043e \u0441\u043f\u043e\u0441\u043e\u0431 \u0441\u043e\u043e\u0431\u0449\u0438\u0442\u044c \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0443 \u043e \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0440\u0430\u0437\u0438\u0442\u044c \u0442\u0438\u043f\u0430\u043c\u0438. \u041e\u0431\u044b\u0447\u043d\u043e \u044d\u0442\u043e: \u0417\u0430\u0447\u0435\u043c \u043d\u0443\u0436\u043d\u044b Kotlin Contracts? \u041f\u0440\u0438\u043c\u0435\u0440 1: Smart cast \u0431\u0435\u0437 \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442\u0430 \u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043d\u0435 \u0437\u043d\u0430\u0435\u0442, \u0447\u0442\u043e isString(x)==true, \u0442\u043e \u0435\u0441\u0442\u044c x \u0442\u043e\u0447\u043d\u043e String. \u041f\u0440\u0438\u043c\u0435\u0440 2: \u0421 \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442\u043e\u043c<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,31],"tags":[],"class_list":["post-883","post","type-post","status-publish","format-standard","hentry","category-kotlin","category-questions"],"_links":{"self":[{"href":"https:\/\/nurlandroid.com\/index.php?rest_route=\/wp\/v2\/posts\/883","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nurlandroid.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nurlandroid.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nurlandroid.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nurlandroid.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=883"}],"version-history":[{"count":1,"href":"https:\/\/nurlandroid.com\/index.php?rest_route=\/wp\/v2\/posts\/883\/revisions"}],"predecessor-version":[{"id":884,"href":"https:\/\/nurlandroid.com\/index.php?rest_route=\/wp\/v2\/posts\/883\/revisions\/884"}],"wp:attachment":[{"href":"https:\/\/nurlandroid.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nurlandroid.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nurlandroid.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}