{"id":7,"date":"2023-10-08T11:23:40","date_gmt":"2023-10-08T11:23:40","guid":{"rendered":"https:\/\/tutorialsexample.com\/?p=7"},"modified":"2023-10-08T12:18:42","modified_gmt":"2023-10-08T12:18:42","slug":"kotlin-scope-function","status":"publish","type":"post","link":"https:\/\/tutorialsexample.com\/index.php\/2023\/10\/08\/kotlin-scope-function\/","title":{"rendered":"Kotlin \u2013 Scope Function"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"7\" class=\"elementor elementor-7\">\n\t\t\t\t\t\t<div class=\"elementor-inner\">\n\t\t\t\t<div class=\"elementor-section-wrap\">\n\t\t\t\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-12af099 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"12af099\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t\t\t<div class=\"elementor-row\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-a5fad55\" data-id=\"a5fad55\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-column-wrap elementor-element-populated\">\n\t\t\t\t\t\t\t<div class=\"elementor-widget-wrap\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-32c17b8 elementor-widget elementor-widget-heading\" data-id=\"32c17b8\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Kotlin: Scope Functions<\/h2>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-80e455f elementor-widget elementor-widget-text-editor\" data-id=\"80e455f\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-text-editor elementor-clearfix\">\n\t\t\t\t<p id=\"292b\" class=\"pw-post-body-paragraph ls lt ge lu b lv lw lx ly lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp fi bj\" data-selectable-paragraph=\"\">Scope functions are functions that execute a block of code within the context of an object.<\/p>\n<p id=\"959a\" class=\"pw-post-body-paragraph ls lt ge lu b lv lw lx ly lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp fi bj\" data-selectable-paragraph=\"\">Scope functions provides a way to <strong class=\"lu gf\">give temporary scope<\/strong> to the object, where <strong class=\"lu gf\">specific operations can be applied<\/strong> to the object within the block of code.<\/p>\n<p id=\"3acc\" class=\"pw-post-body-paragraph ls lt ge lu b lv lw lx ly lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp fi bj\" data-selectable-paragraph=\"\">Scope functions make your code more concise and readable.<\/p>\n<p><br><\/p>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-d794299 elementor-widget elementor-widget-text-editor\" data-id=\"d794299\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-text-editor elementor-clearfix\">\n\t\t\t\t<p>In Kotlin, scope functions are a set of functions that are used to perform operations on objects within a specific context, often referred to as the receiver object. These scope functions allow you to execute a block of code while providing a concise and expressive way to work with objects, set properties, or perform operations on them. They are particularly useful for reducing redundancy, improving code readability, and managing variable scopes.<\/p>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-cb570ac elementor-widget elementor-widget-text-editor\" data-id=\"cb570ac\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-text-editor elementor-clearfix\">\n\t\t\t\t<p><strong>Example: Without using scope function<br \/><br \/>class KRDCompany() { <br \/>\u00a0\u00a0 \u00a0lateinit var name: String <br \/>\u00a0\u00a0 \u00a0lateinit var objective: String <br \/>\u00a0\u00a0 \u00a0lateinit var founder: String <br \/>} <br \/><br \/>fun main() { <br \/>\u00a0\u00a0 \u00a0\/\/ without using scope function <br \/><br \/>\u00a0\u00a0 \u00a0\/\/ creating instance of KRDCompany Class <br \/>\u00a0\u00a0 \u00a0val krd = KRDCompany() <br \/><br \/>\u00a0\u00a0 \u00a0\/\/ initializing members of the class <br \/>\u00a0\u00a0\u00a0 krd.name = &#8220;TutorialsExample&#8221;<br \/>\u00a0\u00a0\u00a0 krd.objective = &#8220;A tutorials portal&#8221;<br \/>\u00a0\u00a0\u00a0 krd.founder = &#8220;Rajnish &#8220;<br \/><br \/>\u00a0\u00a0 \u00a0println(krd.name) <br \/>}<br \/><\/strong><\/p><p>\u00a0<\/p><p><strong>Output:<\/strong><\/p><pre>TutorialsExample<\/pre><div><div><div><ol><li><p>It&#8217;s apparent that, in the absence of scope functions, we are required to include the object name each time we wish to access class members. Conversely, when employing scope functions, direct reference to members can be made without the need for the object name. This represents one of the practical applications of scope functions, which we will explore further in this article.<\/p><\/li><li><p>You might have observed that, without the use of scope functions, repetitive object naming is essential for accessing class members. In contrast, scope functions enable us to access members directly, eliminating the need for object names. This constitutes one of the methods for leveraging scope functions, and we&#8217;ll delve deeper into their usage throughout this article.<\/p><\/li><li><p>It&#8217;s worth noting that, in the absence of scope functions, it&#8217;s obligatory to specify the object name whenever we want to access class members. Conversely, with the use of scope functions, we have the liberty to reference members directly, sans the object name. This is one of the practical applications of scope functions, and we&#8217;ll delve into them further in this article.<\/p><\/li><\/ol><\/div><\/div><\/div>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-465a66a elementor-widget elementor-widget-text-editor\" data-id=\"465a66a\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-text-editor elementor-clearfix\">\n\t\t\t\t<h3>Types of scope functions<\/h3><p>There are five types of scope functions:<\/p><ol><li>let<\/li><li>run<\/li><li>with<\/li><li>apply<\/li><li>also<\/li><\/ol><div><p><strong>Differences in these functions:<\/strong><\/p><p>There are mainly two differences among these functions:<\/p><ol><li><strong>Way of referring to a context object<\/strong> (i.e. using either \u2018this\u2019 or \u2018it\u2019 keyword)<\/li><li><strong>return value<\/strong> (i.e. returns either \u2018context object\u2019 or \u2018lambda result\u2019)<\/li><\/ol><\/div>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-634e4a2 elementor-widget elementor-widget-text-editor\" data-id=\"634e4a2\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-text-editor elementor-clearfix\">\n\t\t\t\t<p>These scope functions provide different ways to work with objects, manage variable scopes, <\/p>\n<table>\n<thead>\n<tr>\n<th>Scope Function<\/th>\n<th>Primary Use Case<\/th>\n<th>Return Value<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>let<\/code><\/td>\n<td>Perform operations on nullable objects and transform them.<\/td>\n<td>Result of the lambda expression.<\/td>\n<\/tr>\n<tr>\n<td><code>run<\/code><\/td>\n<td>Execute code on non-nullable objects and access their properties\/methods.<\/td>\n<td>Result of the lambda expression.<\/td>\n<\/tr>\n<tr>\n<td><code>with<\/code><\/td>\n<td>Simplify access to properties\/methods of an object (non-extension function).<\/td>\n<td>Result of the lambda expression.<\/td>\n<\/tr>\n<tr>\n<td><code>apply<\/code><\/td>\n<td>Initialize or configure an object (non-extension function).<\/td>\n<td>The modified object itself.<\/td>\n<\/tr>\n<tr>\n<td><code>also<\/code><\/td>\n<td>Perform side effects or logging on an object (non-extension function).<\/td>\n<td>The original object itself.<\/td>\n<\/tr>\n<\/tbody>\n<\/table><div><br><\/div><div>\n<figure>\n<table>\n<thead>\n<tr>\n<th>\n<p style=\"text-align:center\">Function<\/p>\n<\/th>\n<th>\n<p style=\"text-align:center\">Object Reference<\/p>\n<\/th>\n<th>\n<p style=\"text-align:center\">Return Value<\/p>\n<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<th>\n<p style=\"text-align:center\">let<\/p>\n<\/th>\n<td>\n<p style=\"text-align:center\">it<\/p>\n<\/td>\n<td>\n<p style=\"text-align:center\">Lambda result<\/p>\n<\/td>\n<\/tr>\n<tr>\n<th>\n<p style=\"text-align:center\">run<\/p>\n<\/th>\n<td>\n<p style=\"text-align:center\">this<\/p>\n<\/td>\n<td>\n<p style=\"text-align:center\">Lambda result<\/p>\n<\/td>\n<\/tr>\n<tr>\n<th>\n<p style=\"text-align:center\">with<\/p>\n<\/th>\n<td>\n<p style=\"text-align:center\">this<\/p>\n<\/td>\n<td>\n<p style=\"text-align:center\">Lambda result<\/p>\n<\/td>\n<\/tr>\n<tr>\n<th>\n<p style=\"text-align:center\">apply<\/p>\n<\/th>\n<td>\n<p style=\"text-align:center\">this<\/p>\n<\/td>\n<td>\n<p style=\"text-align:center\">Context object<\/p>\n<\/td>\n<\/tr>\n<tr>\n<th>\n<p style=\"text-align:center\">also<\/p>\n<\/th>\n<td>\n<p style=\"text-align:center\">it<\/p>\n<\/td>\n<td>\n<p style=\"text-align:center\">Context object<\/p><\/td><\/tr><\/tbody><\/table><\/figure><\/div>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-f1956b0 elementor-widget elementor-widget-text-editor\" data-id=\"f1956b0\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-text-editor elementor-clearfix\">\n\t\t\t\t<h3>1. let function<br \/><br \/><\/h3>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c029d01 elementor-widget elementor-widget-text-editor\" data-id=\"c029d01\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-text-editor elementor-clearfix\">\n\t\t\t\t<h6><span style=\"font-weight: normal;\">let function is often used to provide null safety calls. Use safe call operator(?.) with \u2018let\u2019 for null safety. It executes the block only with the non-null value.<\/span><\/h6>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-acb3ff7 elementor-widget elementor-widget-text-editor\" data-id=\"acb3ff7\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-text-editor elementor-clearfix\">\n\t\t\t\t<p>fun main() {<br \/>\u00a0\u00a0\u00a0 \/\/ Declare a nullable variable &#8216;number&#8217; with an initial value of null.<br \/>\u00a0\u00a0\u00a0 var number: Int? = null<br \/><br \/>\u00a0\u00a0\u00a0 \/\/ Use the &#8216;let&#8217; function to perform operations when &#8216;number&#8217; is not null.<br \/>\u00a0\u00a0\u00a0 number?.let {<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ These statements will not execute because &#8216;number&#8217; is null.<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(it)<br \/>\u00a0\u00a0\u00a0 }<br \/><br \/>\u00a0\u00a0\u00a0 \/\/ Re-initialize the value of &#8216;number&#8217; to 2.<br \/>\u00a0\u00a0\u00a0 number = 2<br \/><br \/>\u00a0\u00a0\u00a0 \/\/ Use the &#8216;let&#8217; function again to perform operations when &#8216;number&#8217; is not null.<br \/>\u00a0\u00a0\u00a0 number?.let {<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ These statements will execute because &#8216;number&#8217; is not null.<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(number)<br \/>\u00a0\u00a0\u00a0 }<br \/>}<br \/><br \/><\/p>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>Kotlin: Scope Functions Scope functions are functions that execute a block of code within the context of an object. Scope functions provides a way to give temporary scope to the object, where specific operations can be applied to the object within the block of code. Scope functions make your code more concise and readable. In &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/tutorialsexample.com\/index.php\/2023\/10\/08\/kotlin-scope-function\/\"> <span class=\"screen-reader-text\">Kotlin \u2013 Scope Function<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","footnotes":""},"categories":[7],"tags":[10,11,9,8],"_links":{"self":[{"href":"https:\/\/tutorialsexample.com\/index.php\/wp-json\/wp\/v2\/posts\/7"}],"collection":[{"href":"https:\/\/tutorialsexample.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tutorialsexample.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tutorialsexample.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tutorialsexample.com\/index.php\/wp-json\/wp\/v2\/comments?post=7"}],"version-history":[{"count":4,"href":"https:\/\/tutorialsexample.com\/index.php\/wp-json\/wp\/v2\/posts\/7\/revisions"}],"predecessor-version":[{"id":13,"href":"https:\/\/tutorialsexample.com\/index.php\/wp-json\/wp\/v2\/posts\/7\/revisions\/13"}],"wp:attachment":[{"href":"https:\/\/tutorialsexample.com\/index.php\/wp-json\/wp\/v2\/media?parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tutorialsexample.com\/index.php\/wp-json\/wp\/v2\/categories?post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tutorialsexample.com\/index.php\/wp-json\/wp\/v2\/tags?post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}