{"id":1483,"date":"2022-03-11T17:10:50","date_gmt":"2022-03-11T11:40:50","guid":{"rendered":"https:\/\/www.whizcamp.tech\/blog\/?p=1483"},"modified":"2022-03-17T11:08:40","modified_gmt":"2022-03-17T05:38:40","slug":"kotlin-scope-functions-explained-and-their-use","status":"publish","type":"post","link":"https:\/\/www.whizcamp.tech\/blog\/kotlin-scope-functions-explained-and-their-use\/","title":{"rendered":"Kotlin Scope Functions Explained and their use"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"1483\" class=\"elementor elementor-1483\">\n\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-6356322 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"6356322\" 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<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-7fbf58c\" data-id=\"7fbf58c\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-dd74a22 elementor-widget elementor-widget-heading\" data-id=\"dd74a22\" data-element_type=\"widget\" id=\"section1\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Kotlin Scoped functions<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-1399841 elementor-widget elementor-widget-text-editor\" data-id=\"1399841\" 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\t<p>Almost all the Android developers either have moved or are moved from using Java to Kotlin and are experiencing how clean, simple, and concise Kotlin is when contrasted to Java. Kotlin brings in the market a bunch of developer-friendly features for less code, thereby resulting in fewer bugs.<\/p><p>Among the many developer-friendly features, Kotlin brings, &#8220;scope functions&#8221; are one of the most crucial features. If you have adequate hands-on experience, you might be already using scope functions. Or, if you find this topic totally new and want to understand these scoped functions? Well, in that case, let&#8217;s dive into the article.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-24c286e elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"24c286e\" 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<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-6bac9e2\" data-id=\"6bac9e2\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-2837e55 elementor-widget elementor-widget-heading\" data-id=\"2837e55\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h3 class=\"elementor-heading-title elementor-size-default\">Scoped Functions in Kotlin\n<\/h3>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-0bd4248 elementor-widget elementor-widget-text-editor\" data-id=\"0bd4248\" 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\t<p>As per standard definition, Scoped functions are functions that execute a block of code within the context of an object.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-c0eb104 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"c0eb104\" 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<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-d740594\" data-id=\"d740594\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-f3cab99 elementor-widget elementor-widget-heading\" data-id=\"f3cab99\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h3 class=\"elementor-heading-title elementor-size-default\">The benefit of using scope functions\u200b<\/h3>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-5f6f3c9 elementor-widget elementor-widget-text-editor\" data-id=\"5f6f3c9\" 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\t<p>Scope functions make code more clear, readable, and concise which are Kotlin language&#8217;s main features. It skips the block if the object is null.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-a4f1d9d elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"a4f1d9d\" 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<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-2f46647\" data-id=\"2f46647\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-81dea24 elementor-widget elementor-widget-heading\" data-id=\"81dea24\" data-element_type=\"widget\" id=\"section2\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h3 class=\"elementor-heading-title elementor-size-default\">Types of scope functions<\/h3>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-b33e0a7 elementor-widget elementor-widget-text-editor\" data-id=\"b33e0a7\" 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\t<p>There are\u00a0five\u00a0scoped functions in Kotlin:\u00a0<\/p><ul><li>Let<\/li><li>Run<\/li><li>With<\/li><li>also<\/li><li>apply<\/li><\/ul><p>Let&#8217;s discuss them one by one with simple examples. These functions provide a context to an object (it or this) and return type (object or <a href=\"https:\/\/kotlinlang.org\/docs\/lambdas.html\">lambda result<\/a>).<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-788250e elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"788250e\" 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<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-2a6625d\" data-id=\"2a6625d\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-4d0f403 elementor-widget elementor-widget-heading\" data-id=\"4d0f403\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h4 class=\"elementor-heading-title elementor-size-default\">What is Lambda Result?<\/h4>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-97af0f1 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"97af0f1\" 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<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-c04cd80\" data-id=\"c04cd80\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-7bee245 elementor-widget elementor-widget-text-editor\" data-id=\"7bee245\" 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\t<p>A lambda function is\u00a0a small function,\u00a0which takes any number of arguments, but can only have one expression.<br \/>But before going through these examples, let&#8217;s consider a class &#8220;Employee&#8221;<br \/><strong><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Class Employee(var empId:Int?=null,\u00a0<\/code><\/strong><br \/><strong><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var empName:String?=null,\u00a0<\/code><\/strong><br \/><strong><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var empDesignation:String?=null)<\/code><\/strong><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-4a090ac elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"4a090ac\" 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<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-81c6c93\" data-id=\"81c6c93\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-f8ee349 elementor-widget elementor-widget-image\" data-id=\"f8ee349\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<a href=\"https:\/\/www.whizcamp.tech\/mobile-app-development\" target=\"_blank\">\n\t\t\t\t\t\t\t<img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"255\" src=\"https:\/\/www.whizcamp.tech\/blog\/wp-content\/uploads\/2022\/03\/Android-Developer-1024x255.png\" class=\"attachment-large size-large wp-image-1542\" alt=\"Android Developer\" srcset=\"https:\/\/www.whizcamp.tech\/blog\/wp-content\/uploads\/2022\/03\/Android-Developer-1024x255.png 1024w, https:\/\/www.whizcamp.tech\/blog\/wp-content\/uploads\/2022\/03\/Android-Developer-300x75.png 300w, https:\/\/www.whizcamp.tech\/blog\/wp-content\/uploads\/2022\/03\/Android-Developer-768x191.png 768w, https:\/\/www.whizcamp.tech\/blog\/wp-content\/uploads\/2022\/03\/Android-Developer.png 1055w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/>\t\t\t\t\t\t\t\t<\/a>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-a41bbdc elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"a41bbdc\" 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<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-324f3f5\" data-id=\"324f3f5\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-650bf7e elementor-widget elementor-widget-heading\" data-id=\"650bf7e\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h4 class=\"elementor-heading-title elementor-size-default\">1. let function<\/h4>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-e1ef91f elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"e1ef91f\" 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<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-9ff7d80\" data-id=\"9ff7d80\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-8da8d7e elementor-widget elementor-widget-text-editor\" data-id=\"8da8d7e\" 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\t<p>Context object\u00a0\u00a0: it (you can rename it) ||\u00a0\u00a0Return value\u00a0 \u00a0\u00a0:\u00a0\u00a0<br \/>lambda result<br \/>When to use:<br \/>let function is used to avoid null calls. Use safe call operator(?.) with &#8216;let&#8217; for null safety. It executes the block only if the object or variable is non-null.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-a1bf85d elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"a1bf85d\" 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<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-9866adf\" data-id=\"9866adf\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-b02b751 elementor-widget elementor-widget-heading\" data-id=\"b02b751\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h4 class=\"elementor-heading-title elementor-size-default\">2. Apply function<\/h4>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-1b1cde6 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"1b1cde6\" 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<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-6df6b6a\" data-id=\"6df6b6a\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-06ab7ce elementor-widget elementor-widget-text-editor\" data-id=\"06ab7ce\" 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\t<p>Context object: this\u00a0<br \/>||\u00a0\u00a0Return value\u00a0 \u00a0\u00a0: context object<br \/>When to use:<br \/>As the name indicates \u2013 &#8220;Apply these to the object.&#8221; It is mainly used to initialize\/assign values to members of an object of the class.<\/p><p><b>Example:<\/b><span style=\"background-color: #ffffff;\"><b><br \/><\/b><\/span><span style=\"background-color: #f9f2f4; color: #c7254e; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 16.2px; white-space: nowrap; font-weight: var( --e-global-typography-text-font-weight );\">Employee().apply{<br \/><\/span><span style=\"background-color: #f9f2f4; color: #c7254e; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 16.2px; white-space: nowrap; font-weight: var( --e-global-typography-text-font-weight );\">this.empId=101,<br \/><\/span><span style=\"background-color: #f9f2f4; color: #c7254e; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 16.2px; white-space: nowrap; font-weight: var( --e-global-typography-text-font-weight );\">empName=\u201dRazia\u201d<br \/><\/span><span style=\"background-color: #f9f2f4; color: #c7254e; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 16.2px; white-space: nowrap; font-weight: var( --e-global-typography-text-font-weight );\">empDesignation=\u201dAndroid Lead\u201d<br \/><\/span><span style=\"background-color: #f9f2f4; color: #c7254e; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 16.2px; white-space: nowrap; font-weight: var( --e-global-typography-text-font-weight );\">}<\/span><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-5c5093f elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"5c5093f\" 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<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-b311b69\" data-id=\"b311b69\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-8eb47ac elementor-widget elementor-widget-heading\" data-id=\"8eb47ac\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h4 class=\"elementor-heading-title elementor-size-default\">3. With function<\/h4>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-f9e2ba8 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"f9e2ba8\" 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<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-bf7732e\" data-id=\"bf7732e\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-e0003f4 elementor-widget elementor-widget-text-editor\" data-id=\"e0003f4\" 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\t<p>Context object\u00a0\u00a0: this\u00a0<br \/>||\u00a0\u00a0Return value\u00a0 \u00a0\u00a0: lambda result<\/p><p><strong>When to use:<\/strong>\u00a0<br \/>The use of &#8220;with&#8221; is similar to the &#8220;let&#8221; and &#8220;run&#8221; function; the difference is: &#8220;with&#8221; does not ignore the null value. Another way of glimpsing it is as logically grouping multiple calls to a given object:<\/p><p><strong>Example:<br \/><\/strong><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">with(employee) {<\/code><br \/><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\u00a0 \u00a0println(\" $empName \")<\/code><br \/><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">printInfo(...)<\/code><br \/><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">calculateSalary(...) }<\/code><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-d591210 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"d591210\" 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<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-b1f077a\" data-id=\"b1f077a\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-b8ce17b elementor-widget elementor-widget-heading\" data-id=\"b8ce17b\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h4 class=\"elementor-heading-title elementor-size-default\">4. Run function<\/h4>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-768c837 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"768c837\" 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<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-c565f98\" data-id=\"c565f98\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-ea1d9ab elementor-widget elementor-widget-text-editor\" data-id=\"ea1d9ab\" 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\t<p>Context object\u00a0\u00a0: this\u00a0<br \/>||\u00a0\u00a0Return value\u00a0 \u00a0\u00a0: lambda result<br \/>The &#8220;run&#8221; function is the combination of &#8216;let&#8217; and &#8216;with&#8217; functions.<\/p><p><strong>When to use:<\/strong><br \/>&#8220;run&#8221; is used when the object lambda contains initialization and the computation of the return value. Using run, we can perform null safety calls. It ignores the block if the object is null.<\/p><p><strong>Example:<br \/><\/strong><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\u00a0 \u00a0val name = Employee().empName?.run {<\/code><br \/><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\u00a0 \u00a0 \u00a0 \u00a0\u00a0\"The name of the Person is: $this\"<\/code><br \/><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\u00a0 \u00a0\u00a0}<\/code><br \/><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\u00a0 \u00a0\u00a0print(name)<\/code><\/p><p><strong>Output:<\/strong><br \/><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">The name of the Person is: Razia<\/code><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-b869d55 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"b869d55\" 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<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-36f012f\" data-id=\"36f012f\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-e03be6d elementor-widget elementor-widget-heading\" data-id=\"e03be6d\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h4 class=\"elementor-heading-title elementor-size-default\">5. Also function<\/h4>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-d48a737 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"d48a737\" 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<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-5bf4c60\" data-id=\"5bf4c60\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-0d87270 elementor-widget elementor-widget-text-editor\" data-id=\"0d87270\" 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\t<p>Context object&nbsp;&nbsp;: it ||&nbsp;<br>Return value&nbsp; &nbsp;&nbsp;:&nbsp;<br>context object&nbsp;<br>When to use:<br>&#8220;also,&#8221; as the name indicates, needs to add some stuff. This is used when you need to perform some additional operations and provide null safety checks:<\/p>\n<p><strong>Example:<\/strong><br><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&nbsp;val name = Employee().also { employee-&gt;<\/code><br><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">print(\"Current name is: ${employee.empName}\\n\")<\/code><br><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">employee.empName = \"Razia Sharma\"<\/code><br><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">}.run {&nbsp;&nbsp;<\/code><br><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\"Modified name is: $name\\n\"<\/code><br><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&nbsp; &nbsp;&nbsp;}<\/code><br><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">print(name)<\/code><br><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">}<\/code><strong><br><\/strong><br><strong>Output:<\/strong><br><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Current name is: Razia<\/code><br><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Modified name is: Razia Sharma<\/code><\/p>\n<p>It&#8217;s time to conclude about the Scoped functions in Kotlin. Even though we might be using it in our coding, we really hope that our article has provided you with a better way of understanding and implementing the right scoped function in the right place.<br><strong>Thank you so much for your time!<\/strong><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>Kotlin Scoped functions Almost all the Android developers either have moved or are moved from using Java to Kotlin and are experiencing how clean, simple, and concise Kotlin is when contrasted to Java. Kotlin brings in the market a bunch &hellip; <a href=\"https:\/\/www.whizcamp.tech\/blog\/kotlin-scope-functions-explained-and-their-use\/\">Continued<\/a><\/p>\n","protected":false},"author":1,"featured_media":1581,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11],"tags":[62,63,61,59,60],"class_list":["post-1483","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-team-blogs","tag-kotlin-features","tag-kotlin-functions","tag-kotlin-scope","tag-kotlin-scope-functions","tag-kotlin-scope-functions-explained"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Kotlin Scope Functions Explained and their use - Whizcamp<\/title>\n<meta name=\"description\" content=\"Kotlin Scope functions help us in the process of simplifying the logics used in the block. The Kotlin standard library contains several functions and purpose of each is to execute a &amp; hellip.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.whizcamp.tech\/blog\/kotlin-scope-functions-explained-and-their-use\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Kotlin Scope Functions Explained and their use - Whizcamp\" \/>\n<meta property=\"og:description\" content=\"Kotlin Scope functions help us in the process of simplifying the logics used in the block. The Kotlin standard library contains several functions and purpose of each is to execute a &amp; hellip.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.whizcamp.tech\/blog\/kotlin-scope-functions-explained-and-their-use\/\" \/>\n<meta property=\"og:site_name\" content=\"Whizcamp Blogs\" \/>\n<meta property=\"article:published_time\" content=\"2022-03-11T11:40:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-17T05:38:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.whizcamp.tech\/blog\/wp-content\/uploads\/2022\/03\/Kotlin-2.png\" \/>\n\t<meta property=\"og:image:width\" content=\"450\" \/>\n\t<meta property=\"og:image:height\" content=\"300\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.whizcamp.tech\/blog\/kotlin-scope-functions-explained-and-their-use\/\",\"url\":\"https:\/\/www.whizcamp.tech\/blog\/kotlin-scope-functions-explained-and-their-use\/\",\"name\":\"Kotlin Scope Functions Explained and their use - Whizcamp\",\"isPartOf\":{\"@id\":\"https:\/\/www.whizcamp.tech\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.whizcamp.tech\/blog\/kotlin-scope-functions-explained-and-their-use\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.whizcamp.tech\/blog\/kotlin-scope-functions-explained-and-their-use\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.whizcamp.tech\/blog\/wp-content\/uploads\/2022\/03\/Kotlin-2.png\",\"datePublished\":\"2022-03-11T11:40:50+00:00\",\"dateModified\":\"2022-03-17T05:38:40+00:00\",\"author\":{\"@id\":\"https:\/\/www.whizcamp.tech\/blog\/#\/schema\/person\/a9ca452d60d215b9eb9bde80f763453b\"},\"description\":\"Kotlin Scope functions help us in the process of simplifying the logics used in the block. The Kotlin standard library contains several functions and purpose of each is to execute a & hellip.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.whizcamp.tech\/blog\/kotlin-scope-functions-explained-and-their-use\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.whizcamp.tech\/blog\/kotlin-scope-functions-explained-and-their-use\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.whizcamp.tech\/blog\/kotlin-scope-functions-explained-and-their-use\/#primaryimage\",\"url\":\"https:\/\/www.whizcamp.tech\/blog\/wp-content\/uploads\/2022\/03\/Kotlin-2.png\",\"contentUrl\":\"https:\/\/www.whizcamp.tech\/blog\/wp-content\/uploads\/2022\/03\/Kotlin-2.png\",\"width\":450,\"height\":300,\"caption\":\"Kotlin Scope\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.whizcamp.tech\/blog\/kotlin-scope-functions-explained-and-their-use\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.whizcamp.tech\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Kotlin Scope Functions Explained and their use\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.whizcamp.tech\/blog\/#website\",\"url\":\"https:\/\/www.whizcamp.tech\/blog\/\",\"name\":\"Whizcamp Blogs\",\"description\":\"Blogs for the latest tips, tricks, &amp; happenings\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.whizcamp.tech\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.whizcamp.tech\/blog\/#\/schema\/person\/a9ca452d60d215b9eb9bde80f763453b\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.whizcamp.tech\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2c6f6f06aedae775e11cdff7168c0764?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2c6f6f06aedae775e11cdff7168c0764?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"sameAs\":[\"https:\/\/www.whizcamp.tech\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Kotlin Scope Functions Explained and their use - Whizcamp","description":"Kotlin Scope functions help us in the process of simplifying the logics used in the block. The Kotlin standard library contains several functions and purpose of each is to execute a & hellip.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.whizcamp.tech\/blog\/kotlin-scope-functions-explained-and-their-use\/","og_locale":"en_US","og_type":"article","og_title":"Kotlin Scope Functions Explained and their use - Whizcamp","og_description":"Kotlin Scope functions help us in the process of simplifying the logics used in the block. The Kotlin standard library contains several functions and purpose of each is to execute a & hellip.","og_url":"https:\/\/www.whizcamp.tech\/blog\/kotlin-scope-functions-explained-and-their-use\/","og_site_name":"Whizcamp Blogs","article_published_time":"2022-03-11T11:40:50+00:00","article_modified_time":"2022-03-17T05:38:40+00:00","og_image":[{"width":450,"height":300,"url":"https:\/\/www.whizcamp.tech\/blog\/wp-content\/uploads\/2022\/03\/Kotlin-2.png","type":"image\/png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.whizcamp.tech\/blog\/kotlin-scope-functions-explained-and-their-use\/","url":"https:\/\/www.whizcamp.tech\/blog\/kotlin-scope-functions-explained-and-their-use\/","name":"Kotlin Scope Functions Explained and their use - Whizcamp","isPartOf":{"@id":"https:\/\/www.whizcamp.tech\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.whizcamp.tech\/blog\/kotlin-scope-functions-explained-and-their-use\/#primaryimage"},"image":{"@id":"https:\/\/www.whizcamp.tech\/blog\/kotlin-scope-functions-explained-and-their-use\/#primaryimage"},"thumbnailUrl":"https:\/\/www.whizcamp.tech\/blog\/wp-content\/uploads\/2022\/03\/Kotlin-2.png","datePublished":"2022-03-11T11:40:50+00:00","dateModified":"2022-03-17T05:38:40+00:00","author":{"@id":"https:\/\/www.whizcamp.tech\/blog\/#\/schema\/person\/a9ca452d60d215b9eb9bde80f763453b"},"description":"Kotlin Scope functions help us in the process of simplifying the logics used in the block. The Kotlin standard library contains several functions and purpose of each is to execute a & hellip.","breadcrumb":{"@id":"https:\/\/www.whizcamp.tech\/blog\/kotlin-scope-functions-explained-and-their-use\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.whizcamp.tech\/blog\/kotlin-scope-functions-explained-and-their-use\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.whizcamp.tech\/blog\/kotlin-scope-functions-explained-and-their-use\/#primaryimage","url":"https:\/\/www.whizcamp.tech\/blog\/wp-content\/uploads\/2022\/03\/Kotlin-2.png","contentUrl":"https:\/\/www.whizcamp.tech\/blog\/wp-content\/uploads\/2022\/03\/Kotlin-2.png","width":450,"height":300,"caption":"Kotlin Scope"},{"@type":"BreadcrumbList","@id":"https:\/\/www.whizcamp.tech\/blog\/kotlin-scope-functions-explained-and-their-use\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.whizcamp.tech\/blog\/"},{"@type":"ListItem","position":2,"name":"Kotlin Scope Functions Explained and their use"}]},{"@type":"WebSite","@id":"https:\/\/www.whizcamp.tech\/blog\/#website","url":"https:\/\/www.whizcamp.tech\/blog\/","name":"Whizcamp Blogs","description":"Blogs for the latest tips, tricks, &amp; happenings","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.whizcamp.tech\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.whizcamp.tech\/blog\/#\/schema\/person\/a9ca452d60d215b9eb9bde80f763453b","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.whizcamp.tech\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2c6f6f06aedae775e11cdff7168c0764?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2c6f6f06aedae775e11cdff7168c0764?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/www.whizcamp.tech\/blog"]}]}},"jetpack_featured_media_url":"https:\/\/www.whizcamp.tech\/blog\/wp-content\/uploads\/2022\/03\/Kotlin-2.png","post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/www.whizcamp.tech\/blog\/wp-json\/wp\/v2\/posts\/1483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.whizcamp.tech\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.whizcamp.tech\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.whizcamp.tech\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.whizcamp.tech\/blog\/wp-json\/wp\/v2\/comments?post=1483"}],"version-history":[{"count":91,"href":"https:\/\/www.whizcamp.tech\/blog\/wp-json\/wp\/v2\/posts\/1483\/revisions"}],"predecessor-version":[{"id":1651,"href":"https:\/\/www.whizcamp.tech\/blog\/wp-json\/wp\/v2\/posts\/1483\/revisions\/1651"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.whizcamp.tech\/blog\/wp-json\/wp\/v2\/media\/1581"}],"wp:attachment":[{"href":"https:\/\/www.whizcamp.tech\/blog\/wp-json\/wp\/v2\/media?parent=1483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.whizcamp.tech\/blog\/wp-json\/wp\/v2\/categories?post=1483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.whizcamp.tech\/blog\/wp-json\/wp\/v2\/tags?post=1483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}