1"use strict";(self.webpackChunkdmitri_pavlutin_blog=self.webpackChunkdmitri_pavlutin_blog||[]).push([[7410],{3671:function(e,t,o){o.r(t),o.d(t,{default:function(){return y}});var n=o(7294),r=o(1151),s=o(9794);const c={annotations:s.ds,Code:s.EK},a={staticMediaQuery:"not screen, (max-width: 768px)",theme:{name:"one-dark-pro",type:"dark",semanticHighlighting:!0,semanticTokenColors:{enumMember:{foreground:"#56b6c2"},"variable.constant":{foreground:"#d19a66"},"variable.defaultLibrary":{foreground:"#e5c07b"},"variable:dart":{foreground:"#d19a66"},"property:dart":{foreground:"#d19a66"},"annotation:dart":{foreground:"#d19a66"},"parameter.label:dart":{foreground:"#abb2bf"},macro:{foreground:"#d19a66"}},tokenColors:[{settings:{foreground:"#abb2bf",background:"#282c34"}},{scope:"meta.embedded",settings:{foreground:"#abb2bf"}},{name:"unison punctuation",scope:"punctuation.definition.delayed.unison,punctuation.definition.list.begin.unison,punctuation.definition.list.end.unison,punctuation.definition.ability.begin.unison,punctuation.definition.ability.end.unison,punctuation.operator.assignment.as.unison,punctuation.separator.pipe.unison,punctuation.separator.delimiter.unison,punctuation.definition.hash.unison",settings:{foreground:"#e06c75"}},{name:"haskell variable generic-type",scope:"variable.other.generic-type.haskell",settings:{foreground:"#c678dd"}},{name:"haskell storage type",scope:"storage.type.haskell",settings:{foreground:"#d19a66"}},{name:"support.variable.magic.python",scope:"support.variable.magic.python",settings:{foreground:"#e06c75"}},{name:"punctuation.separator.parameters.python",scope:"punctuation.separator.period.python,punctuation.separator.element.python,punctuation.parenthesis.begin.python,punctuation.parenthesis.end.python",settings:{foreground:"#abb2bf"}},{name:"variable.parameter.function.language.special.self.python",scope:"variable.parameter.function.language.special.self.python",settings:{foreground:"#e5c07b"}},{name:"variable.parameter.function.language.special.cls.python",scope:"variable.parameter.function.language.special.cls.python",settings:{foreground:"#e5c07b"}},{name:"storage.modifier.lifetime.rust",scope:"storage.modifier.lifetime.rust",settings:{foreground:"#abb2bf"}},{name:"support.function.std.rust",scope:"support.function.std.rust",settings:{foreground:"#61afef"}},{name:"entity.name.lifetime.rust",scope:"entity.name.lifetime.rust",settings:{foreground:"#e5c07b"}},{name:"variable.language.rust",scope:"variable.language.rust",settings:{foreground:"#e06c75"}},{name:"support.constant.edge",scope:"support.constant.edge",settings:{foreground:"#c678dd"}},{name:"regexp constant character-class",scope:"constant.other.character-class.regexp",settings:{foreground:"#e06c75"}},{name:"keyword.operator",scope:["keyword.operator.word"],settings:{foreground:"#c678dd"}},{name:"regexp operator.quantifier",scope:"keyword.operator.quantifier.regexp",settings:{foreground:"#d19a66"}},{name:"Text",scope:"variable.parameter.function",settings:{foreground:"#abb2bf"}},{name:"Comment Markup Link",scope:"comment markup.link",settings:{foreground:"#5c6370"}},{name:"markup diff",scope:"markup.changed.diff",settings:{foreground:"#e5c07b"}},{name:"diff",scope:"meta.diff.header.from-file,meta.diff.header.to-file,punctuation.definition.from-file.diff,punctuation.definition.to-file.diff",settings:{foreground:"#61afef"}},{name:"inserted.diff",scope:"markup.inserted.diff",settings:{foreground:"#98c379"}},{name:"deleted.diff",scope:"markup.deleted.diff",settings:{foreground:"#e06c75"}},{name:"c++ function",scope:"meta.function.c,meta.function.cpp",settings:{foreground:"#e06c75"}},{name:"c++ block",scope:"punctuation.section.block.begin.bracket.curly.cpp,punctuation.section.block.end.bracket.curly.cpp,punctuation.terminator.statement.c,punctuation.section.block.begin.bracket.curly.c,punctuation.section.block.end.bracket.curly.c,punctuation.section.parens.begin.bracket.round.c,punctuation.section.parens.end.bracket.round.c,punctuation.section.parameters.begin.bracket.round.c,punctuation.section.parameters.end.bracket.round.c",settings:{foreground:"#abb2bf"}}
1,{name:"js/ts punctuation separator key-value",scope:"punctuation.separator.key-value",settings:{foreground:"#abb2bf"}},{name:"js/ts import keyword",scope:"keyword.operator.expression.import",settings:{foreground:"#61afef"}},{name:"math js/ts",scope:"support.constant.math",settings:{foreground:"#e5c07b"}},{name:"math property js/ts",scope:"support.constant.property.math",settings:{foreground:"#d19a66"}},{name:"js/ts variable.other.constant",scope:"variable.other.constant",settings:{foreground:"#e5c07b"}},{name:"java type",scope:["storage.type.annotation.java","storage.type.object.array.java"],settings:{foreground:"#e5c07b"}},{name:"java source",scope:"source.java",settings:{foreground:"#e06c75"}},{name:"java modifier.import",scope:"punctuation.section.block.begin.java,punctuation.section.block.end.java,punctuation.definition.method-parameters.begin.java,punctuation.definition.method-parameters.end.java,meta.method.identifier.java,punctuation.section.method.begin.java,punctuation.section.method.end.java,punctuation.terminator.java,punctuation.section.class.begin.java,punctuation.section.class.end.java,punctuation.section.inner-class.begin.java,punctuation.section.inner-class.end.java,meta.method-call.java,punctuation.section.class.begin.bracket.curly.java,punctuation.section.class.end.bracket.curly.java,punctuation.section.method.begin.bracket.curly.java,punctuation.section.method.end.bracket.curly.java,punctuation.separator.period.java,punctuation.bracket.angle.java,punctuation.definition.annotation.java,meta.method.body.java",settings:{foreground:"#abb2bf"}},{name:"java modifier.import",scope:"meta.method.java",settings:{foreground:"#61afef"}},{name:"java modifier.import",scope:"storage.modifier.import.java,storage.type.java,storage.type.generic.java",settings:{foreground:"#e5c07b"}},{name:"java instanceof",scope:"keyword.operator.instanceof.java",settings:{foreground:"#c678dd"}},{name:"java variable.name",scope:"meta.definition.variable.name.java",settings:{foreground:"#e06c75"}},{name:"operator logical",scope:"keyword.operator.logical",settings:{foreground:"#56b6c2"}},{name:"operator bitwise",scope:"keyword.operator.bitwise",settings:{foreground:"#56b6c2"}},{name:"operator channel",scope:"keyword.operator.channel",settings:{foreground:"#56b6c2"}},{name:"support.constant.property-value.scss",scope:"support.constant.property-value.scss,support.constant.property-value.css",settings:{foreground:"#d19a66"}},{name:"CSS/SCSS/LESS Operators",scope:"keyword.operator.css,keyword.operator.scss,keyword.operator.less",settings:{foreground:"#56b6c2"}},{name:"css color standard name",scope:"support.constant.color.w3c-standard-color-name.css,support.constant.color.w3c-standard-color-name.scss",settings:{foreground:"#d19a66"}},{name:"css comma",scope:"punctuation.separator.list.comma.css",settings:{foreground:"#abb2bf"}},{name:"css attribute-name.id",scope:"support.constant.color.w3c-standard-color-name.css",settings:{foreground:"#d19a66"}},{name:"css property-name",scope:"support.type.vendored.property-name.css",settings:{foreground:"#56b6c2"}},{name:"js/ts module",scope:"support.module.node,support.type.object.module,support.module.node",settings:{foreground:"#e5c07b"}},{name:"entity.name.type.module",scope:"entity.name.type.module",settings:{foreground:"#e5c07b"}},{name:"js variable readwrite",scope:"variable.other.readwrite,meta.object-literal.key,support.variable.property,support.variable.object.process,support.variable.object.node",settings:{foreground:"#e06c75"}},{name:"js/ts json",scope:"support.constant.json",settings:{foreground:"#d19a66"}},{name:"js/ts Keyword",scope:["keyword.operator.expression.instanceof","keyword.operator.new","keyword.operator.ternary","keyword.operator.optional","keyword.operator.expression.keyof"],settings:{foreground:"#c678dd"}},{name:"js/ts console",scope:"support.type.object.console",settings:{foreground:"#e06c75"}},{name:"js/ts support.variable.property.process",scope:"support.variable.property.process",settings:{foreground:"#d19a66"}},{name:"js console function",scope:"entity.name.function,support.function.console",settings:{foreground:"#61afef"}},{name:"keyword.operator.misc.rust",scope:"keyword.operator.misc.rust",settings:{foreground:"#abb2bf"}},{name:"keyword.operator.sigil.rust",scope:"keyword.operator.sigil.rust",settings:{foreground:"#c678dd"}},{name:"operator",scope:"keyword.operator.delete",settings:{foreground:"#c678dd"}},{name:"js dom",scope:"support.type.object.dom",settings:{foreground:"#56b6c2"}},{name:"js dom variable",scope:"support.variable.dom,support.variable.property.dom",settings:{foreground:"#e06c75"}},{name:"keyword.operator",scope:"keyword.operator.arithmetic,keyword.operator.comparison,keyword.operator.decrement,keyword.operator.increment,keyword.operator.relational",settings:{foreground:"#56b6c2"}},{name:"C operator assignment",scope:"keyword.operator.assignment.c,
1keyword.operator.comparison.c,keyword.operator.c,keyword.operator.increment.c,keyword.operator.decrement.c,keyword.operator.bitwise.shift.c,keyword.operator.assignment.cpp,keyword.operator.comparison.cpp,keyword.operator.cpp,keyword.operator.increment.cpp,keyword.operator.decrement.cpp,keyword.operator.bitwise.shift.cpp",settings:{foreground:"#c678dd"}},{name:"Punctuation",scope:"punctuation.separator.delimiter",settings:{foreground:"#abb2bf"}},{name:"Other punctuation .c",scope:"punctuation.separator.c,punctuation.separator.cpp",settings:{foreground:"#c678dd"}},{name:"C type posix-reserved",scope:"support.type.posix-reserved.c,support.type.posix-reserved.cpp",settings:{foreground:"#56b6c2"}},{name:"keyword.operator.sizeof.c",scope:"keyword.operator.sizeof.c,keyword.operator.sizeof.cpp",settings:{foreground:"#c678dd"}},{name:"python parameter",scope:"variable.parameter.function.language.python",settings:{foreground:"#d19a66"}},{name:"python type",scope:"support.type.python",settings:{foreground:"#56b6c2"}},{name:"python logical",scope:"keyword.operator.logical.python",settings:{foreground:"#c678dd"}},{name:"pyCs",scope:"variable.parameter.function.python",settings:{foreground:"#d19a66"}},{name:"python block",scope:"punctuation.definition.arguments.begin.python,punctuation.definition.arguments.end.python,punctuation.separator.arguments.python,punctuation.definition.list.begin.python,punctuation.definition.list.end.python",settings:{foreground:"#abb2bf"}},{name:"python function-call.generic",scope:"meta.function-call.generic.python",settings:{foreground:"#61afef"}},{name:"python placeholder reset to normal string",scope:"constant.character.format.placeholder.other.python",settings:{foreground:"#d19a66"}},{name:"Operators",scope:"keyword.operator",settings:{foreground:"#abb2bf"}},{name:"Compound Assignment Operators",scope:"keyword.operator.assignment.compound",settings:{foreground:"#c678dd"}},{name:"Compound Assignment Operators js/ts",scope:"keyword.operator.assignment.compound.js,keyword.operator.assignment.compound.ts",settings:{foreground:"#56b6c2"}},{name:"Keywords",scope:"keyword",settings:{foreground:"#c678dd"}},{name:"Namespaces",scope:"entity.name.namespace",settings:{foreground:"#e5c07b"}},{name:"Variables",scope:"variable",settings:{foreground:"#e06c75"}},{name:"Variables",scope:"variable.c",settings:{foreground:"#abb2bf"}},{name:"Language variables",scope:"variable.language",settings:{foreground:"#e5c07b"}},{name:"Java Variables",scope:"token.variable.parameter.java",settings:{foreground:"#abb2bf"}},{name:"Java Imports",scope:"import.storage.java",settings:{foreground:"#e5c07b"}},{name:"Packages",scope:"token.package.keyword",settings:{foreground:"#c678dd"}},{name:"Packages",scope:"token.package",settings:{foreground:"#abb2bf"}},{name:"Functions",scope:["entity.name.function","meta.require","support.function.any-method","variable.function"],settings:{foreground:"#61afef"}},{name:"Classes",scope:"entity.name.type.namespace",settings:{foreground:"#e5c07b"}},{name:"Classes",scope:"support.class, entity.name.type.class",settings:{foreground:"#e5c07b"}},{name:"Class name",scope:"entity.name.class.identifier.namespace.type",settings:{foreground:"#e5c07b"}},{name:"Class name",scope:["entity.name.class","variable.other.class.js","variable.other.class.ts"],settings:{foreground:"#e5c07b"}},{name:"Class name php",scope:"variable.other.class.php",settings:{foreground:"#e06c75"}},{name:"Type Name",scope:"entity.name.type",settings:{foreground:"#e5c07b"}},{name:"Keyword Control",scope:"keyword.control",settings:{foreground:"#c678dd"}},{name:"Control Elements",scope:"control.elements, keyword.operator.less",settings:{foreground:"#d19a66"}},{name:"Methods",scope:"keyword.other.special-method",settings:{foreground:"#61afef"}},{name:"Storage",scope:"storage",settings:{foreground:"#c678dd"}},{name:"Storage JS TS",scope:"token.storage",settings:{foreground:"#c678dd"}},{name:"Source Js Keyword Operator Delete,source Js Keyword Operator In,source Js Keyword Operator Of,source Js Keyword Operator Instanceof,source Js Keyword Operator New,source Js Keyword Operator Typeof,source Js Keyword Operator Void",scope:"keyword.operator.expression.delete,keyword.operator.expression.in,keyword.operator.expression.of,keyword.operator.expression.instanceof,keyword.operator.new,keyword.operator.expression.typeof,keyword.operator.expression.void",settings:{foreground:"#c678dd"}},{name:"Java Storage",scope:"token.storage.type.java",settings:{foreground:"#e5c07b"}},{name:"Support",scope:"support.function",settings:{foreground:"#56b6c2"}},{name:"Support type",scope:"support.type.property-name",settings:{foreground:"#abb2bf"}},{name:"Support type",scope:"support.constant.property-value",settings:{foreground:"#abb2bf"}},{name:"Support type",scope:"support.constant.font-name",settings:{foreground:"#d19a66"}},{name:"Meta tag",scope:"meta.tag",settings:{foreground:"#abb2bf"}},{name:"Strings",scope:"string",settings:{foreground:"#98c379"}},{name:"Constant other symbol",scope:"constant.other.symbol",settings:{foreground:"#56b6c2"}},{name:"Integers",scope:"constant.numeric",settings:{foreground:"#d19a66"}},{name:"Constants",scope:"constant",settings:{foreground:"#d19a66"}},{name:"Constants",scope:"punctuation.definition.constant",settings:{foreground:"#d19a66"}},{name:"Tags",scope:"entity.name.tag",settings:{foreground:"#e06c75"}},{name:"Attributes",scope:"entity.other.attribute-name",settings:{foreground:"#d19a66"}},{name:"Attribute IDs",scope:"entity.other.attribute-name.id",settings:{fontStyle:"normal",foreground:"#61afef"}},{name:"Attribute class",scope:"entity.other.attribute-name.class.css",settings:{fontStyle:"normal",foreground:"#d19a66"}},{name:"Selector",scope:"meta.selector",settings:{foreground:"#c678dd"}},{name:"Headings",scope:"markup.heading",settings:{foreground:"#e06c75"}},{name:"Headings",scope:"markup.heading punctuation.definition.heading, entity.name.section",settings:{foreground:"#61afef"}},{name:"Units",scope:"keyword.other.unit",settings:{foreground:"#e06c75"}},{name:"Bold",scope:"markup.bold,todo.bold",settings:{foreground:"#d19a66"}},{name:"Bold",scope:"punctuation.definition.bold",settings:{foreground:"#e5c07b"}},{name:"markup Italic",scope:"markup.italic, punctuation.definition.italic,todo.emphasis",settings:{foreground:"#c678dd"}},{name:"emphasis md",scope:"emphasis md",settings:{foreground:"#c678dd"}},{name:"[VSCODE-CUSTOM] Markdown headings",scope:"entity.name.section.markdown",settings:{foreground:"#e06c75"}},{name:"[VSCODE-CUSTOM] Markdown heading Punctuation Definition",scope:"punctuation.definition.heading.markdown",settings:{foreground:"#e06c75"}},{name:"punctuation.definition.list.begin.markdown",scope:"punctuation.definition.list.begin.markdown",settings:{foreground:"#e5c07b"}},{name:"[VSCODE-CUSTOM] Markdown heading setext",scope:"markup.heading.setext",settings:{foreground:"#abb2bf"}},{name:"[VSCODE-CUSTOM] Markdown Punctuation Definition Bold",scope:"punctuation.definition.bold.markdown",settings:{foreground:"#d19a66"}},{name:"[VSCODE-CUSTOM] Markdown Inline Raw",scope:"markup.inline.raw.markdown",settings:{foreground:"#98c379"}},{name:"[VSCODE-CUSTOM] Markdown Inline Raw",scope:"markup.inline.raw.string.markdown",settings:{foreground:"#98c379"}},{name:"[VSCODE-CUSTOM] Markdown Inline Raw punctuation",scope:"punctuation.definition.raw.markdown",settings:{foreground:"#e5c07b"}},{name:"[VSCODE-CUSTOM] Markdown List Punctuation Definition",scope:"punctuation.definition.list.markdown",settings:{foreground:"#e5c07b"}},{name:"[VSCODE-CUSTOM] Markdown Punctuation Definition String",scope:["punctuation.definition.string.begin.markdown","punctuation.definition.string.end.markdown","punctuation.definition.metadata.markdown"],settings:{foreground:"#e06c75"}},{name:"beginning.punctuation.definition.list.markdown",scope:["beginning.punctuation.definition.list.markdown"],settings:{foreground:"#e06c75"}},{name:"[VSCODE-CUSTOM] Markdown Punctuation Definition Link",scope:"punctuation.definition.metadata.markdown",settings:{foreground:"#e06c75"}},{name:"[VSCODE-CUSTOM] Markdown Underline Link/Image",scope:"markup.underline.link.markdown,markup.underline.link.image.markdown",settings:{foreground:"#c678dd"}},{name:"[VSCODE-CUSTOM] Markdown Link Title/Description",scope:"string.other.link.title.markdown,string.other.link.description.markdown",settings:{foreground:"#61afef"}},{name:"[VSCODE-CUSTOM] Asciidoc Inline Raw",scope:"markup.raw.monospace.asciidoc",settings:{foreground:"#98c379"}},{name:"[VSCODE-CUSTOM] Asciidoc Inline Raw Punctuation Definition",scope:"punctuation.definition.asciidoc",settings:{foreground:"#e5c07b"}},{name:"[VSCODE-CUSTOM] Asciidoc List Punctuation Definition",scope:"markup.list.asciidoc",settings:{foreground:"#e5c07b"}},{name:"[VSCODE-CUSTOM] Asciidoc underline link",scope:"markup.link.asciidoc,markup.other.url.asciidoc",settings:{foreground:"#c678dd"}},{name:"[VSCODE-CUSTOM] Asciidoc link name",scope:"string.unquoted.asciidoc,markup.other.url.asciidoc",settings:{foreground:"#61afef"}},{name:"Regular Expressions",scope:"string.regexp",settings:{foreground:"#56b6c2"}},{name:"Escape Characters",scope:"constant.character.escape",settings:{foreground:"#56b6c2"}},{name:"Embedded",scope:"punctuation.section.embedded, variable.
1interpolation",settings:{foreground:"#e06c75"}},{name:"Embedded",scope:"punctuation.section.embedded.begin,punctuation.section.embedded.end",settings:{foreground:"#c678dd"}},{name:"illegal",scope:"invalid.illegal",settings:{foreground:"#ffffff"}},{name:"illegal",scope:"invalid.illegal.bad-ampersand.html",settings:{foreground:"#abb2bf"}},{name:"Broken",scope:"invalid.broken",settings:{foreground:"#ffffff"}},{name:"Deprecated",scope:"invalid.deprecated",settings:{foreground:"#ffffff"}},{name:"Unimplemented",scope:"invalid.unimplemented",settings:{foreground:"#ffffff"}},{name:"Source Json Meta Structure Dictionary Json > String Quoted Json",scope:"source.json meta.structure.dictionary.json > string.quoted.json",settings:{foreground:"#e06c75"}},{name:"Source Json Meta Structure Dictionary Json > String Quoted Json > Punctuation String",scope:"source.json meta.structure.dictionary.json > string.quoted.json > punctuation.string",settings:{foreground:"#e06c75"}},{name:"Source Json Meta Structure Dictionary Json > Value Json > String Quoted Json,source Json Meta Structure Array Json > Value Json > String Quoted Json,source Json Meta Structure Dictionary Json > Value Json > String Quoted Json > Punctuation,source Json Meta Structure Array Json > Value Json > String Quoted Json > Punctuation",scope:"source.json meta.structure.dictionary.json > value.json > string.quoted.json,source.json meta.structure.array.json > value.json > string.quoted.json,source.json meta.structure.dictionary.json > value.json > string.quoted.json > punctuation,source.json meta.structure.array.json > value.json > string.quoted.json > punctuation",settings:{foreground:"#98c379"}},{name:"Source Json Meta Structure Dictionary Json > Constant Language Json,source Json Meta Structure Array Json > Constant Language Json",scope:"source.json meta.structure.dictionary.json > constant.language.json,source.json meta.structure.array.json > constant.language.json",settings:{foreground:"#56b6c2"}},{name:"[VSCODE-CUSTOM] JSON Property Name",scope:"support.type.property-name.json",settings:{foreground:"#e06c75"}},{name:"[VSCODE-CUSTOM] JSON Punctuation for Property Name",scope:"support.type.property-name.json punctuation",settings:{foreground:"#e06c75"}},{name:"laravel blade tag",scope:"text.html.laravel-blade source.php.embedded.line.html entity.name.tag.laravel-blade",settings:{foreground:"#c678dd"}},{name:"laravel blade @",scope:"text.html.laravel-blade source.php.embedded.line.html support.constant.laravel-blade",settings:{foreground:"#c678dd"}},{name:"use statement for other classes",scope:"support.other.namespace.use.php,support.other.namespace.use-as.php,entity.other.alias.php,meta.interface.php",settings:{foreground:"#e5c07b"}},{name:"error suppression",scope:"keyword.operator.error-control.php",settings:{foreground:"#c678dd"}},{name:"php instanceof",scope:"keyword.operator.type.php",settings:{foreground:"#c678dd"}},{name:"style double quoted array index normal begin",scope:"punctuation.section.array.begin.php",settings:{foreground:"#abb2bf"}},{name:"style double quoted array index normal end",scope:"punctuation.section.array.end.php",settings:{foreground:"#abb2bf"}},{name:"php illegal.non-null-typehinted",scope:"invalid.illegal.non-null-typehinted.php",settings:{foreground:"#f44747"}},{name:"php types",scope:"storage.type.php,meta.other.type.phpdoc.php,keyword.other.type.php,keyword.other.array.phpdoc.php",settings:{foreground:"#e5c07b"}},{name:"php call-function",scope:"meta.function-call.php,meta.function-call.object.php,meta.function-call.static.php",settings:{foreground:"#61afef"}},{name:"php function-resets",scope:"punctuation.definition.parameters.begin.bracket.round.php,punctuation.definition.parameters.end.bracket.round.php,punctuation.separator.delimiter.php,punctuation.section.scope.begin.php,punctuation.section.scope.end.php,punctuation.terminator.expression.php,punctuation.definition.arguments.begin.bracket.round.php,punctuation.definition.arguments.end.bracket.round.php,punctuation.definition.storage-type.begin.bracket.round.php,punctuation.definition.storage-type.end.bracket.round.php,punctuation.definition.array.begin.bracket.round.php,punctuation.definition.array.end.bracket.round.php,punctuation.definition.begin.bracket.round.php,punctuation.definition.end.bracket.round.php,punctuation.definition.begin.bracket.curly.php,punctuation.definition.end.bracket.curly.php,punctuation.definition.section.switch-block.end.bracket.curly.php,punctuation.definition.section.switch-block.start.bracket.curly.php,punctuation.definition.section.switch-block.begin.bracket.curly.php,punctuation.definition.section.switch-block.end.bracket.curly.php",settings:{foreground:"#abb2bf"}},{name:"support php constants",scope:"support.constant.core.rust",settings:{foreground:"#d19a66"}},{name:"support php constants",scope:"support.constant.ext.php,support.constant.std.php,support.constant.core.php,support.constant.parser-token.php",settings:{foreground:"#d19a66"}},{name:"php goto",scope:"entity.name.goto-label.php,support.other.php",settings:{foreground:"#61afef"}},{name:"php logical/bitwise operator",scope:"keyword.operator.logical.php,keyword.operator.bitwise.php,keyword.operator.arithmetic.php",settings:{foreground:"#56b6c2"}},{name:"php regexp operator",scope:"keyword.operator.regexp.php",settings:{foreground:"#c678dd"}},{name:"php comparison",scope:"keyword.operator.comparison.php",settings:{foreground:"#56b6c2"}},{name:"php heredoc/nowdoc",scope:"keyword.operator.heredoc.php,keyword.operator.nowdoc.php",settings:{foreground:"#c678dd"}},{name:"python function decorator @",scope:"meta.function.decorator.python",settings:{foreground:"#61afef"}},{name:"python function support",scope:"support.token.decorator.python,meta.function.decorator.identifier.python",settings:{foreground:"#56b6c2"}},{name:"parameter function js/ts",scope:"function.parameter",settings:{foreground:"#abb2bf"}},{name:"brace function",scope:"function.brace",settings:{foreground:"#abb2bf"}},{name:"parameter function ruby cs",scope:"function.parameter.ruby, function.parameter.cs",settings:{foreground:"#abb2bf"}},{name:"constant.language.symbol.ruby",scope:"constant.language.symbol.ruby",settings:{foreground:"#56b6c2"}},{name:"rgb-value",scope:"rgb-value",settings:{foreground:"#56b6c2"}},{name:"rgb value",scope:"inline-color-decoration rgb-value",settings:{foreground:"#d19a66"}},{name:"rgb value less",scope:"less rgb-value",settings:{foreground:"#d19a66"}},{name:"sass selector",scope:"selector.sass",settings:{foreground:"#e06c75"}},{name:"ts primitive/builtin types",scope:"support.type.primitive.ts,support.type.builtin.ts,support.type.primitive.tsx,support.type.builtin.tsx",settings:{foreground:"#e5c07b"}},{name:"block scope",scope:"block.scope.end,block.scope.begin",settings:{foreground:"#abb2bf"}},{name:"cs storage type",scope:"storage.type.cs",settings:{foreground:"#e5c07b"}},{name:"cs local variable",scope:"entity.name.variable.local.cs",settings:{foreground:"#e06c75"}},{scope:"token.info-token",settings:{foreground:"#61afef"}},{scope:"token.warn-token",settings:{foreground:"#d19a66"}},{scope:"token.error-token",settings:{foreground:"#f44747"}},{scope:"token.debug-token",settings:{foreground:"#c678dd"}},{name:"String interpolation",scope:["punctuation.definition.template-expression.begin","punctuation.definition.template-expression.end","punctuation.section.embedded"],settings:{foreground:"#c678dd"}},{name:"Reset JavaScript string interpolation expression",scope:["meta.template.expression"],settings:{foreground:"#abb2bf"}},{name:"Import module JS",scope:["keyword.operator.module"],settings:{foreground:"#c678dd"}},{name:"js Flowtype",scope:["support.type.type.flowtype"],settings:{foreground:"#61afef"}},{name:"js Flow",scope:["support.type.primitive"],settings:{foreground:"#e5c07b"}},{name:"js class prop",scope:["meta.property.object"],settings:{foreground:"#e06c75"}},{name:"js func parameter",scope:["variable.parameter.function.js"],settings:{foreground:"#e06c75"}},{name:"js template literals begin",scope:["keyword.other.template.begin"],settings:{foreground:"#98c379"}},{name:"js template literals end",scope:["keyword.other.template.end"],settings:{foreground:"#98c379"}},{name:"js template literals variable braces begin",scope:["keyword.other.substitution.begin"],settings:{foreground:"#98c379"}},{name:"js template literals variable braces end",scope:["keyword.other.substitution.end"],settings:{foreground:"#98c379"}},{name:"js operator.assignment",scope:["keyword.operator.assignment"],settings:{foreground:"#56b6c2"}},{name:"go operator",scope:["keyword.operator.assignment.go"],settings:{foreground:"#e5c07b"}},{name:"go operator",scope:["keyword.operator.arithmetic.go","keyword.operator.address.go"],settings:{foreground:"#c678dd"}},{name:"Go package name",scope:["entity.name.package.go"],settings:{foreground:"#e5c07b"}},{name:"elm prelude",scope:["support.type.prelude.elm"],settings:{foreground:"#56b6c2"}},{name:"elm constant",scope:["support.constant.elm"],settings:{foreground:"#d19a66"}},{name:"template literal",scope:["punctuation.quasi.element"],settings:{foreground:"#c678dd"}},{name:"html/pug (jade) escaped characters and entities",scope:["constant.character.entity"],settings:{foreground:"#e06c75"}},{name:"styling css pseudo-elements/classes to be able to differentiate from classes which are the same colour",scope:["entity.other.attribute-name.pseudo-element","entity.other.attribute-name.pseudo-class"],settings:{foreground:"#56b6c2"}},{name:"Clojure globals",scope:["entity.global.clojure"],settings:{foreground:"#e5c07b"}},{name:"Clojure symbols",scope:["meta.symbol.clojure"],settings:{foreground:"#e06c75"}},{name:"Clojure constants",scope:["constant.keyword.clojure"],settings:{foreground:"#56b6c2"}},{name:"CoffeeScript Function Argument",scope:["meta.arguments.coffee","variable.parameter.function.coffee"],settings:{foreground:"#e06c75"}},{name:"Ini Default Text",scope:["source.ini"],settings:{foreground:"#98c379"}},{name:"Makefile prerequisities",scope:["meta.scope.prerequisites.makefile"],settings:{foreground:"#e06c75"}},{name:"Makefile text colour",scope:["source.makefile"],settings:{foreground:"#e5c07b"}},{name:"Groovy import names",scope:["storage.modifier.import.groovy"],settings:{foreground:"#e5c07b"}},{name:"Groovy Methods",scope:["meta.method.groovy"],settings:{foreground:"#61afef"}},{name:"Groovy Variables",scope:["meta.definition.variable.name.groovy"],settings:{foreground:"#e06c75"}},{name:"Groovy Inheritance",scope:["meta.definition.class.inherited.classes.groovy"],settings:{foreground:"#98c379"}},{name:"HLSL Semantic",scope:["support.variable.semantic.hlsl"],settings:{foreground:"#e5c07b"}},{name:"HLSL Types",scope:["support.type.texture.hlsl","support.type.sampler.hlsl","support.type.object.hlsl","support.type.object.rw.hlsl","support.type.fx.hlsl","support.type.object.hlsl"],settings:{foreground:"#c678dd"}},{name:"SQL Variables",scope:["text.variable","text.bracketed"],settings:{foreground:"#e06c75"}},{name:"types",scope:["support.type.swift","support.type.vb.asp"],settings:{foreground:"#e5c07b"}},{name:"heading 1, keyword",scope:["entity.name.function.xi"],settings:{foreground:"#e5c07b"}},{name:"heading 2, callable",scope:["entity.name.class.xi"],settings:{foreground:"#56b6c2"}},{name:"heading 3, property",scope:["constant.character.character-class.regexp.xi"],settings:{foreground:"#e06c75"}},{name:"heading 4, type, class, interface",scope:["constant.regexp.xi"],settings:{foreground:"#c678dd"}},{name:"heading 5, enums, preprocessor, constant, decorator",scope:["keyword.control.xi"],settings:{foreground:"#56b6c2"}},{name:"heading 6, number",scope:["invalid.xi"],settings:{foreground:"#abb2bf"}},{name:"string",scope:["beginning.punctuation.definition.quote.markdown.xi"],settings:{foreground:"#98c379"}},{name:"comments",scope:["beginning.punctuation.definition.list.markdown.xi"],settings:{foreground:"#7f848e"}},{name:"link",scope:["constant.character.xi"],settings:{foreground:"#61afef"}},{name:"accent",scope:["accent.xi"],settings:{foreground:"#61afef"}},{name:"wikiword",scope:["wikiword.xi"],settings:{foreground:"#d19a66"}},{name:"language operators like '+', '-' etc",scope:["constant.other.color.rgb-value.xi"],settings:{foreground:"#ffffff"}},{name:"elements to dim",scope:["punctuation.definition.tag.xi"],settings:{foreground:"#5c6370"}},{name:"C++/C#",scope:["entity.name.label.cs","entity.name.scope-resolution.function.call","entity.name.scope-resolution.function.definition"],settings:{foreground:"#e5c07b"}},{name:"Markdown underscore-style headers",scope:["entity.name.label.cs","markup.heading.setext.1.markdown","markup.heading.setext.2.markdown"],settings:{foreground:"#e06c75"}},{name:"meta.brace.square",scope:[" meta.brace.square"],settings:{foreground:"#abb2bf"}},{name:"Comments",scope:"comment, punctuation.definition.comment",settings:{fontStyle:"italic",foreground:"#7f848e"}},{name:"[VSCODE-CUSTOM] Markdown Quote",scope:"markup.quote.markdown",settings:{foreground:"#5c6370"}},{name:"punctuation.definition.block.sequence.item.yaml",scope:"punctuation.definition.block.sequence.item.yaml",settings:{foreground:"#abb2bf"}},{scope:["constant.language.symbol.elixir","constant.language.symbol.double-quoted.elixir"],settings:{foreground:"#56b6c2"}},{scope:["entity.name.variable.parameter.cs"],settings:{foreground:"#e5c07b"}},{scope:["entity.name.variable.field.cs"],settings:{foreground:"#e06c75"}},{name:"Deleted",scope:"markup.deleted",settings:{foreground:"#e06c75"}}
1,{name:"Inserted",scope:"markup.inserted",settings:{foreground:"#98c379"}},{name:"Underline",scope:"markup.underline",settings:{fontStyle:"underline"}},{name:"punctuation.section.embedded.begin.php",scope:["punctuation.section.embedded.begin.php","punctuation.section.embedded.end.php"],settings:{foreground:"#BE5046"}},{name:"support.other.namespace.php",scope:["support.other.namespace.php"],settings:{foreground:"#abb2bf"}},{name:"variable.other.object",scope:["variable.other.object"],settings:{foreground:"#e5c07b"}},{name:"variable.other.constant.property",scope:["variable.other.constant.property"],settings:{foreground:"#e06c75"}},{name:"entity.other.inherited-class",scope:["entity.other.inherited-class"],settings:{foreground:"#e5c07b"}},{name:"c variable readwrite",scope:"variable.other.readwrite.c",settings:{foreground:"#e06c75"}},{name:"php scope",scope:"entity.name.variable.parameter.php,punctuation.separator.colon.php,constant.other.php",settings:{foreground:"#abb2bf"}},{name:"Assembly",scope:["constant.numeric.decimal.asm.x86_64"],settings:{foreground:"#c678dd"}},{scope:["support.other.parenthesis.regexp"],settings:{foreground:"#d19a66"}},{scope:["constant.character.escape"],settings:{foreground:"#e5c07b"}},{scope:["string.regexp"],settings:{foreground:"#e06c75"}},{name:"js/ts italic",scope:"entity.other.attribute-name.js,entity.other.attribute-name.ts,entity.other.attribute-name.jsx,entity.other.attribute-name.tsx,variable.parameter,variable.language.super",settings:{fontStyle:"italic"}},{name:"comment",scope:"comment.line.double-slash,comment.block.documentation",settings:{fontStyle:"italic"}},{name:"Python Keyword Control",scope:"keyword.control.import.python,keyword.control.flow.python,keyword.operator.logical.python",settings:{fontStyle:"italic"}},{name:"markup.italic.markdown",scope:"markup.italic.markdown",settings:{fontStyle:"italic"}}],colors:{"activityBar.background":"#282c34","activityBar.foreground":"#d7dae0","activityBarBadge.background":"#4d78cc","activityBarBadge.foreground":"#f8fafd","badge.background":"#282c34","button.background":"#404754","button.secondaryBackground":"#30333d","button.secondaryForeground":"#c0bdbd","checkbox.border":"#404754","debugToolBar.background":"#21252b",descriptionForeground:"#abb2bf","diffEditor.insertedTextBackground":"#00809b33","dropdown.background":"#21252b","dropdown.border":"#21252b","editor.background":"#282c34","editor.findMatchBackground":"#42557b","editor.findMatchBorder":"#457dff","editor.findMatchHighlightBackgroun
1d":"#6199ff2f","editor.foreground":"#abb2bf","editorHoverWidget.highlightForeground":"#61afef","editorInlayHint.foreground":"#abb2bf","editorInlayHint.background":"#2c313c","editor.lineHighlightBackground":"#2c313c","editorLineNumber.activeForeground":"#abb2bf","editorGutter.addedBackground":"#109868","editorGutter.deletedBackground":"#9A353D","editorGutter.modifiedBackground":"#948B60","editorOverviewRuler.addedBackground":"#109868","editorOverviewRuler.deletedBackground":"#9A353D","editorOverviewRuler.modifiedBackground":"#948B60","editor.selectionBackground":"#67769660","editor.selectionHighlightBackground":"#ffffff10","editor.selectionHighlightBorder":"#dddddd","editor.wordHighlightBackground":"#d2e0ff2f","editor.wordHighlightBorder":"#7f848e","editor.wordHighlightStrongBackground":"#abb2bf26","editor.wordHighlightStrongBorder":"#7f848e","editorActiveLineNumber.foreground":"#737984","editorBracketMatch.background":"#515a6b","editorBracketMatch.border":"#515a6b","editorCursor.background":"#ffffffc9","editorCursor.foreground":"#528bff","editorError.foreground":"#c24038","editorGroup.background":"#181a1f","editorGroup.border":"#181a1f","editorGroupHeader.tabsBackground":"#21252b","editorHoverWidget.background":"#21252b","editorHoverWidget.border":"#181a1f","editorIndentGuide.activeBackground":"#c8c8c859","editorIndentGuide.background":"#3b4048","editorLineNumber.foreground":"#495162","editorMarkerNavigation.background":"#21252b","editorRuler.foreground":"#abb2bf26","editorSuggestWidget.background":"#21252b","editorSuggestWidget.border":"#181a1f","editorSuggestWidget.selectedBackground":"#2c313a","editorWarning.foreground":"#d19a66","editorWhitespace.foreground":"#3b4048","editorWidget.background":"#21252b",focusBorder:"#3e4452","gitDecoration.ignoredResourceForeground":"#636b78","input.background":"#1d1f23","list.activeSelectionBackground":"#2c313a","list.activeSelectionForeground":"#d7dae0","list.focusBackground":"#323842","list.focusForeground":"#f0f0f0","list.highlightForeground":"#c5c5c5","list.hoverBackground":"#2c313a","list.hoverForeground":"#fff","list.inactiveSelectionBackground":"#323842","list.inactiveSelectionForeground":"#d7dae0","list.warningForeground":"#d19a66","menu.foreground":"#abb2bf","menu.separatorBackground":"#343a45","minimapGutter.addedBackground":"#109868","minimapGutter.deletedBackground":"#9A353D","minimapGutter.modifiedBackground":"#948B60","panel.border":"#3e4452","panelSectionHeader.background":"#21252b","peekViewEditor.background":"#1b1d23","peekViewEditor.matchHighlightBackgroun
1d":"#29244b","peekViewResult.background":"#22262b","scrollbar.shadow":"#23252c","scrollbarSlider.activeBackground":"#747d9180","scrollbarSlider.background":"#4e566660","scrollbarSlider.hoverBackground":"#5a637580","settings.focusedRowBackground":"#282c34","settings.headerForeground":"#fff","sideBar.background":"#21252b","sideBar.foreground":"#abb2bf","sideBarSectionHeader.background":"#282c34","sideBarSectionHeader.foreground":"#abb2bf","statusBar.background":"#21252b","statusBar.debuggingBackground":"#cc6633","statusBar.debuggingBorder":"#66017a","statusBar.debuggingForeground":"#ffffff","statusBar.foreground":"#9da5b4","statusBar.noFolderBackground":"#21252b","statusBarItem.hoverBackground":"#2c313a","statusBarItem.remoteBackground":"#4d78cc","statusBarItem.remoteForeground":"#f8fafd","tab.activeBackground":"#282c34","tab.activeForeground":"#dcdcdc","tab.border":"#181a1f","tab.hoverBackground":"#323842","tab.inactiveBackground":"#21252b","tab.unfocusedHoverBackground":"#323842","terminal.ansiBlack":"#3f4451","terminal.ansiBlue":"#4aa5f0","terminal.ansiBrightBlack":"#4f5666","terminal.ansiBrightBlue":"#4dc4ff","terminal.ansiBrightCyan":"#4cd1e0","terminal.ansiBrightGreen":"#a5e075","terminal.ansiBrightMagenta":"#de73ff","terminal.ansiBrightRed":"#ff616e","terminal.ansiBrightWhite":"#d7dae0","terminal.ansiBrightYellow":"#f0a45d","terminal.ansiCyan":"#42b3c2","terminal.ansiGreen":"#8cc265","terminal.ansiMagenta":"#c162de","terminal.ansiRed":"#e05561","terminal.ansiWhite":"#e6e6e6","terminal.ansiYellow":"#d18f52","terminal.background":"#282c34","terminal.border":"#3e4452","terminal.foreground":"#abb2bf","terminal.selectionBackground":"#abb2bf30","textBlockQuote.background":"#2e3440","textBlockQuote.border":"#4b5362","textLink.foreground":"#61afef","textPreformat.foreground":"#d19a66","titleBar.activeBackground":"#282c34","titleBar.activeForeground":"#9da5b4","titleBar.inactiveBackground":"#21252b","titleBar.inactiveForeground":"#6b717d","walkThrough.embeddedEditorBackground":"#2e3440","welcomePage.buttonHoverBackground":"#404754"}},autoImport:!0,skipLanguages:[],filepath:"/home/runner/work/dmitripavlutin.com/dmitripavlutin.com/posts/029-swift-self/index.md"};function l(e){const t=Object.assign({p:"p",code:"code",a:"a",em:"em",ul:"ul",li:"li",h2:"h2",span:"span",strong:"strong",br:"br",h3:"h3"},(0,r.ah)(),e.components);return c||p("CH",!1),c.Code||p("CH.Code",!0),n.createElement(n.Fragment,null,n.createElement(t.p,null,"In Swift ",n.createElement(t.code,null,"self")," is a special property of an instance that holds the instance itself. Most of the times ",n.createElement(t.code,null,"self")," appears in an initializer or method of a class, structure or enumeration."),"\n",n.createElement(t.p,null,"The motto ",n.createElement(t.a,{href:"https://swift.org/documentation/api-design-guidelines/#clarity-over-brevity",target:"_blank",rel:"noopener noreferrer"},n.createElement(t.em,null,"favor clarity over brevity"))," is a valuable strategy to follow. It applies efficiently in most of the cases, and helps to increase the code readability, but... affects code shortness."),"\n",n.createElement(t.p,null,"Applying this motto to ",n.createElement(t.code,null,"self"),", the following 2 opposites arise:"),"\n",n.createElement(t.ul,null,"\n",n.createElement(t.li,null,"Should you apply the clarity strategy and keep always ",n.createElement(t.code,null,"self")," to access instance properties ",n.createElement(t.code,null,"self.property"),"?"),"\n",n.createElement(t.li,null,"Or accessing the property from a method brings sufficient context to omit ",n.createElement(t.code,null,"self")," from ",n.createElement(t.code,null,"self.property")," without significant readability loss?"),"\n"),"\n",n.createElement(t.p,null,"The answer is that Swift permits and even encourages you to omit ",n.createElement(t.code,null,"self")," keyword when it can be done. The challenge is to determine the scenarios when ",n.createElement(t.code,null,"self")," is obligatory and when optional."),"\n",n.createElement(t.p,null,"Challenge accepted! Let's dive into an interesting review of ",n.createElement(t.code,null,"self")," keyword in Swift."),"\n",n.createElement(t.h2,{id:"1-access-instance-properties-and-methods",style:{position:"relative"}}
1,n.createElement(t.a,{href:"#1-access-instance-properties-and-methods","aria-label":"1 access instance properties and methods permalink",className:"anchor before"},n.createElement(t.span,{dangerouslySetInnerHTML:{__html:'<svg aria-hidden="true" height="20" version="1.1" viewBox="0 0 16 16" width="20"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>'}})),"1. Access instance properties and methods"),"\n",n.createElement(t.p,null,"For example the following structure ",n.createElement(t.code,null,"Weather")," uses ",n.createElement(t.code,null,"self")," in its initializer and ",n.createElement(t.code,null,"isDayForWalk()")," method:"),"\n",n.createElement(c.Code,{codeConfig:a,northPanel:{tabs:[""],active:"",heightRatio:1},files:[{name:"",focus:"",code:{lines:[{tokens:[{content:"struct ",props:{style:{color:"#C678DD"}}},{content:"Weather",props:{style:{color:"#E5C07B"}}},{content:" {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" ",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" let",props:{style:{color:"#C678DD"}}},{content:" windSpeed: ",props:{style:{color:"#ABB2BF"}}},{content:"Int ",props:{style:{color:"#E5C07B"}}},{content:"// miles per hour",props:{style:{color:"#7F848E",fontStyle:"italic"}}}]},{tokens:[{content:" let chanceOfRain: ",props:{style:{color:"#ABB2BF"}}},{content:"Int ",props:{style:{color:"#E5C07B"}}},{content:"// percent",props:{style:{color:"#7F848E",fontStyle:"italic"}}}]},{tokens:[{content:"",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" init",props:{style:{color:"#C678DD"}}},{content:"(",props:{style:{color:"#ABB2BF"}}},{content:"windSpeed",props:{style:{color:"#61AFEF",fontStyle:"italic"}}},{content:": ",props:{style:{color:"#ABB2BF"}}},{content:"Int",props:{style:{color:"#E5C07B"}}},{content:", ",props:{style:{color:"#ABB2BF"}}},{content:"chanceOfRain",props:{style:{color:"#61AFEF",fontStyle:"italic"}}},{content:": ",props:{style:{color:"#ABB2BF"}}},{content:"Int",props:{style:{color:"#E5C07B"}}},{content:") {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" self",props:{style:{color:"#E5C07B"}}},{content:".",props:{style:{color:"#ABB2BF"}}},{content:"windSpeed",props:{style:{color:"#E06C75"}}},{content:" = windSpeed",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" self",props:{style:{color:"#E5C07B"}}},{content:".",props:{style:{color:"#ABB2BF"}}},{content:"chanceOfRain",props:{style:{color:"#E06C75"}}},{content:" = chanceOfRain",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" }",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" func ",props:{style:{color:"#C678DD"}}},{content:"isDayForWalk",props:{style:{color:"#61AFEF"}}},{content:"() -> ",props:{style:{color:"#ABB2BF"}}},{content:"Bool",props:{style:{color:"#E5C07B"}}},{content:" {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" let",props:{style:{color:"#C678DD"}}},{content:" comfortableWindSpeed = ",props:{style:{color:"#ABB2BF"}}},{content:"5",props:{style:{color:"#D19A66"}}}]},{tokens:[{content:" let",props:{style:{color:"#C678DD"}}},{content:" acceptableChanceOfRain = ",props:{style:{color:"#ABB2BF"}}},{content:"30",props:{style:{color:"#D19A66"}}}]},{tokens:[{content:" return ",props:{style:{color:"#C678DD"}}},{content:"self",props:{style:{color:"#E5C07B"}}},{content:".",props:{style:{color:"#ABB2BF"}}},{content:"windSpeed",props:{style:{color:"#E06C75"}}},{content:" <= comfortableWindSpeed",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" && ",props:{style:{color:"#ABB2BF"}}},{content:"self",props:{style:{color:"#E5C07B"}}},{content:".",props:{style:{color:"#ABB2BF"}}},{content:"chanceOfRain",props:{style:{color:"#E06C75"}}},{content:" <= acceptableChanceOfRain",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" }",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" ",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"}",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"// A nice day for a walk",props:{style:{color:"#7F848E",fontStyle:"italic"}}}]},{tokens:[{content:"let",props:{style:{color:"#C678DD"}}},{content:" niceWeather = ",props:{style:{color:"#ABB2BF"}}},{content:"Weather",props:{style:{color:"#61AFEF"}}},{content:"(",props:{style:{color:"#ABB2BF"}}},{content:"windSpeed",props:{style:{color:"#61AFEF"}}},{content:": ",props:{style:{color:"#ABB2BF"}}},{content:"4",props:{style:{color:"#D19A66"}}},{content:", ",props:{style:{color:"#ABB2BF"}}},{content:"chanceOfRain",props:{style:{color:"#61AFEF"}}},{content:": ",props:{style:{color:"#ABB2BF"}}},{content:"25",props:{style:{color:"#D19A66"}}},{content:")",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"print",props:{style:{color:"#56B6C2"}}},{content:"(niceWeather.",props:{style:{color:"#ABB2BF"}}},{content:"isDayForWalk",props:{style:{color:"#61AFEF"}}},{content:"()) ",props:{style:{color:"#ABB2BF"}}},{content:"// => true",props:{style:{color:"#7F848E",fontStyle:"italic"}}}]}],lang:"swift"},annotations:[]}]}
1),"\n",n.createElement(t.p,null,n.createElement(t.code,null,"self")," special property inside ",n.createElement(t.code,null,"init(windSpeed:chanceOfRain:)")," and ",n.createElement(t.code,null,"isDayForWalk()")," is the current instance of ",n.createElement(t.code,null,"Weather")," structure. It allows to set and access the structure properties ",n.createElement(t.code,null,"self.windSpeed")," and ",n.createElement(t.code,null,"self.chanceOfRain"),"."),"\n",n.createElement(t.p,null,"The structure looks nice."),"\n",n.createElement(t.p,null,"Nevertheless accessing every time ",n.createElement(t.code,null,"self")," property may be excessive. The structure initializer and method provide enough context: everything happens inside the structure. Is it possible to get rid of ",n.createElement(t.code,null,"self"),"?"),"\n",n.createElement(t.p,null,"As mentioned in the introduction, Swift encourages you to omit the ",n.createElement(t.code,null,"self")," keyword whenever possible."),"\n",n.createElement(t.p,null,"In the previous example, it is ",n.createElement(t.strong,null,"recommended to remove ",n.createElement(t.code,null,"self"))," from ",n.createElement(t.code,null,"isDayForWalk()")," method. This makes the method shorter:"),"\n",n.createElement(c.Code,{codeConfig:a,northPanel:{tabs:[""],active:"",heightRatio:1},files:[{name:"",focus:"",code:{lines:[{tokens:[{content:"struct ",props:{style:{color:"#C678DD"}}},{content:"Weather",props:{style:{color:"#E5C07B"}}},{content:" {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" /* ... */",props:{style:{color:"#7F848E",fontStyle:"italic"}}}]},{tokens:[{content:" func ",props:{style:{color:"#C678DD"}}},{content:"isDayForWalk",props:{style:{color:"#61AFEF"}}},{content:"() -> ",props:{style:{color:"#ABB2BF"}}},{content:"Bool",props:{style:{color:"#E5C07B"}}},{content:" {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" let",props:{style:{color:"#C678DD"}}},{content:" comfortableWindSpeed = ",props:{style:{color:"#ABB2BF"}}},{content:"5",props:{style:{color:"#D19A66"}}}]},{tokens:[{content:" let",props:{style:{color:"#C678DD"}}},{content:" acceptableChanceOfRain = ",props:{style:{color:"#ABB2BF"}}},{content:"30",props:{style:{color:"#D19A66"}}}]},{tokens:[{content:" return",props:{style:{color:"#C678DD"}}},{content:" windSpeed <= comfortableWindSpeed",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" && chanceOfRain <= acceptableChanceOfRain",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" }",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"}",props:{style:{color:"#ABB2BF"}}}]}],lang:"swift"},annotations:[]}]}),"\n",n.createElement(t.p,null,"Because ",n.createElement(t.code,null,"isDayForWalk()")," method is always invoked in the context of ",n.createElement(t.code,null,"Weather"),"'s instance, Swift enables access of ",n.createElement(t.code,null,"windSpeed")," and ",n.createElement(t.code,null,"chanceOfRain")," without ",n.createElement(t.code,null,"self"),"."),"\n",n.createElement(t.p,null,"Inside the initializer ",n.createElement(t.code,null,"init(windSpeed:chanceOfRain:)")," the parameters and structure properties have the same names ",n.createElement(t.code,null,"windSpeed")," and ",n.createElement(t.code,null,"chanceOfRain"),". Contrary to previous case, now you ",n.createElement(t.strong,null,"cannot remove ",n.createElement(t.code,null,"self"))," keyword, because it would create an ambiguity between the parameter and property names."),"\n",n.createElement(t.p,null,n.createElement(t.code,null,"self")," helps making an explicit distinction between parameters (",n.createElement(t.code,null,"windSpeed")," and ",n.createElement(t.code,null,"chanceOfRain")," used without ",n.createElement(t.code,null,"self"),") and structure properties (",n.createElement(t.code,null,"self.windSpeed")," and ",n.createElement(t.code,null,"self.chanceOfRain"),", accessed with ",n.createElement(t.code,null,"self"),")."),"\n",n.createElement(t.p,null,"Let's make an experiment and still remove ",n.createElement(t.code,null,"self")," from the initializer:"),"\n",n.createElement(c.Code,{codeConfig:a,northPanel:{tabs:[""],active:"",heightRatio:1},files:[{name:"",focus:"",code:{lines:[{tokens:[{content:"struct ",props:{style:{color:"#C678DD"}}},{content:"Weather",props:{style:{color:"#E5C07B"}}},{content:" {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" /* ... */",props:{style:{color:"#7F848E",fontStyle:"italic"}}}]},{tokens:[{content:" init",props:{style:{color:"#C678DD"}}},{content:"(",props:{style:{color:"#ABB2BF"}}},{content:"windSpeed",props:{style:{color:"#61AFEF",fontStyle:"italic"}}},{content:": ",props:{style:{color:"#ABB2BF"}}},{content:"Int",props:{style:{color:"#E5C07B"}}},{content:", ",props:{style:{color:"#ABB2BF"}}},{content:"chanceOfRain",props:{style:{color:"#61AFEF",fontStyle:"italic"}}},{content:": ",props:{style:{color:"#ABB2BF"}}},{content:"Int",props:{style:{color:"#E5C07B"}}},{content:") {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" windSpeed = windSpeed",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" chanceOfRain = chanceOfRain",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" }",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" /* ... */",props:{style:{color:"#7F848E",fontStyle:"italic"}}}]},{tokens:[{content:"}",props:{style:{color:"#ABB2BF"}}}]}],lang:"swift"},annotations:[]}]}),"\n",n.createElement(t.p,null,"Looking at the assignment ",n.createElement(t.code,null,"windSpeed = windSpeed"),", how Swift can understand which variable is a parameter and which is a property? It's an ambiguity, so Swift plainly decides that you mean only the parameters."),"\n",n.createElement(t.p,null,"As result an error is thrown: ",n.createElement(t.code,null,"cannot assign to value: 'windSpeed' is a 'let' constant"),". It happens because ",n.createElement(t.code,null,"let")," constant is assigned to itself ",n.createElement(t.code,null,"windSpeed = windSpeed"),", which is not allowed."),"\n",n.createElement(t.span,{dangerouslySetInnerHTML:{__html:'<span\n class="gatsby-resp-image-wrapper"\n style="position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 560px; "\n >\n <a\n class="gatsby-resp-image-link"\n href="/static/27053589f3266c8bcf67598b17cd8159/4033b/cat-mirror.jpg"\n style="display: block"\n target="_blank"\n rel="noopener"\n >\n <span\n class="gatsby-resp-image-background-image"\n style="padding-bottom: 123.57142857142856%; position: relative; bottom: 0; left: 0; background-image: url(\'data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAZABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAMEAgH/xAAWAQEBAQAAAAAAAAAAAAAAAAABAAL/2gAMAwEAAhADEAAAAdzM5jTTZUiqqRkHA//EABwQAQABBQEBAAAAAAAAAAAAAAIBAAMREiETMv/aAAgBAQABBQI8PZoLMJaiORaxoDteWwQ9MW6NXPr/xAAXEQADAQAAAAAAAAAAAAAAAAAAAREQ/9oACAEDAQE/AcThT//EABQRAQAAAAAAAAAAAAAAAAAAACD/2gAIAQIBAT8BH//EAB4QAAIBAwUAAAAAAAAAAAAAAAAhQQEQESAxYXHR/9oACAEBAAY/AumZHBzWz3MiiT3R/8QAHBAAAgIDAQEAAAAAAAAAAAAAAREAIUFhcTHR/9oACAEBAAE/IQBDtGS3qEMjZOUOj7LJogFdgAyMWWZYyR3MY2JphGOQLsUuYcnueXJ//9oADAMBAAIAAwAAABD7FLP/xAAXE
1QEBAQEAAAAAAAAAAAAAAAABABEQ/9oACAEDAQE/EAkNkW8f/8QAFxEAAwEAAAAAAAAAAAAAAAAAAAERIf/aAAgBAgEBPxBso48hD//EACAQAQACAgEEAwAAAAAAAAAAAAERIQAxQRBhcaFRkcH/2gAIAQEAAT8QgAJQ8Fe8FZOTYh7YASUKpeb+8OCxDFOFlR4xJSChyn4wBZQWDOslIJU2e6/3DxKBJJG6w7GZA0PN3uel3+XPU5//2Q==\'); background-size: cover; display: block;"\n ></span>\n <img\n class="gatsby-resp-image-image"\n alt="Self keyword in Swift"\n title=""\n src="/static/27053589f3266c8bcf67598b17cd8159/9605b/cat-mirror.jpg"\n srcset="/static/27053589f3266c8bcf67598b17cd8159/c496c/cat-mirror.jpg 140w,\n/static/27053589f3266c8bcf67598b17cd8159/cbbf7/cat-mirror.jpg 280w,\n/static/27053589f3266c8bcf67598b17cd8159/9605b/cat-mirror.jpg 560w,\n/static/27053589f3266c8bcf67598b17cd8159/4033b/cat-mirror.jpg 565w"\n sizes="(max-width: 560px) 100vw, 560px"\n style="width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"\n loading="lazy"\n decoding="async"\n />\n </a>\n </span>'}}),"\n",n.createElement(t.h2,{id:"2-to-be-or-not-to-be",style:{position:"relative"}},n.createElement(t.a,{href:"#2-to-be-or-not-to-be","aria-label":"2 to be or not to be permalink",className:"anchor before"},n.createElement(t.span,{dangerouslySetInnerHTML:{__html:'<svg aria-hidden="true" height="20" version="1.1" viewBox="0 0 16 16" width="20"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>'}})),"2. To be, or not to be"),"\n",n.createElement(t.p,null,"There were plenty of discussions about ",n.createElement(t.a,{href:"https://github.com/apple/swift-evolution/blob/master/proposals/0009-require-self-for-accessing-instance-members.md",target:"_blank",rel:"noopener noreferrer"},"the obligatory usage")," of ",n.createElement(t.code,null,"self")," to access properties, or contrary to skip ",n.createElement(t.code,null,"self"),"."),"\n",n.createElement(t.p,null,"The obligatory usage of ",n.createElement(t.code,null,"self")," brings the benefits of consistency and favors clarity over brevity. You can clearly see the difference between the instance properties (that are prefixed with ",n.createElement(t.code,null,"self."),") from locally defined variables. Maybe..."),"\n",n.createElement(t.p,null,"In my opinion, when you have troubles to distinguish instance properties from local variables within a method: you have a different, deeper problem."),"\n",n.createElement(t.p,null,"When a structure or class has an excessive number of properties (so called ",n.createElement(t.a,{href:"https://en.wikipedia.org/wiki/God_object",target:"_blank",rel:"noopener noreferrer"},"God object"),"), you're probably breaking the ",n.createElement(t.a,{href:"https://en.wikipedia.org/wiki/Single_responsibility_principle",target:"_blank",rel:"noopener noreferrer"},"Single responsibility principle"),".",n.createElement(t.br),"\n","If a method uses a big number of these properties and declares correspondingly many local variables, then such method does too many things. Using an explicit ",n.createElement(t.code,null,"self")," to distinguish ",n.createElement(t.em,null,"somehow")," properties from variables is a temporary workaround for a bad code."),"\n",n.createElement(t.p,null,"It should not be that way."),"\n",n.createElement(t.p,null,"Now imagine a well designed structure or class, which has a single responsibility. It contains only strictly necessary properties. A well written method is performing one determined action and as result its code is simple and obvious.",n.createElement(t.br),"\n","Surely you don't have the problem to distinguish local variables from instance properties in such a method. The usage of ",n.createElement(t.code,null,"self")," may be even discouraged, since your adding to obvious code redundant explanations."),"\n",n.createElement(t.p,null,"So design your classes and structures well, and don't let the methods grow to thousands of lines of code. Then you can omit ",n.createElement(t.code,null,"self")," without difficulties, and your code becomes even more expressive and concise."),"\n",n.createElement(t.h2,{id:"3-access-type-properties-and-methods",style:{position:"relative"}}
1,n.createElement(t.a,{href:"#3-access-type-properties-and-methods","aria-label":"3 access type properties and methods permalink",className:"anchor before"},n.createElement(t.span,{dangerouslySetInnerHTML:{__html:'<svg aria-hidden="true" height="20" version="1.1" viewBox="0 0 16 16" width="20"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>'}})),"3. Access type properties and methods"),"\n",n.createElement(t.p,null,n.createElement(t.code,null,"self")," refers to a type (rather than to an instance) when used in a type method."),"\n",n.createElement(t.p,null,"Let's define following ",n.createElement(t.code,null,"Const")," structure with type properties ",n.createElement(t.code,null,"minLimit"),", ",n.createElement(t.code,null,"maxLimit")," and type method ",n.createElement(t.code,null,"getLimitRange()"),":"),"\n",n.createElement(c.Code,{codeConfig:a,northPanel:{tabs:[""],active:"",heightRatio:1},files:[{name:"",focus:"",code:{lines:[{tokens:[{content:"struct ",props:{style:{color:"#C678DD"}}},{content:"Const",props:{style:{color:"#E5C07B"}}},{content:" { ",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" static let",props:{style:{color:"#C678DD"}}},{content:" minLimit = ",props:{style:{color:"#ABB2BF"}}},{content:"0",props:{style:{color:"#D19A66"}}}]},{tokens:[{content:" static let",props:{style:{color:"#C678DD"}}},{content:" maxLimit = ",props:{style:{color:"#ABB2BF"}}},{content:"250",props:{style:{color:"#D19A66"}}}]},{tokens:[{content:" static func ",props:{style:{color:"#C678DD"}}},{content:"getLimitRange",props:{style:{color:"#61AFEF"}}},{content:"() -> ",props:{style:{color:"#ABB2BF"}}},{content:"ClosedRange",props:{style:{color:"#E5C07B"}}},{content:"<",props:{style:{color:"#ABB2BF"}}},{content:"Int",props:{style:{color:"#E5C07B"}}},{content:"> {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" return ",props:{style:{color:"#C678DD"}}},{content:"self",props:{style:{color:"#E5C07B"}}},{content:".",props:{style:{color:"#ABB2BF"}}},{content:"minLimit",props:{style:{color:"#E06C75"}}},{content:"...self.",props:{style:{color:"#ABB2BF"}}},{content:"maxLimit ",props:{style:{color:"#E06C75"}}}]},{tokens:[{content:" }",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"}",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"print",props:{style:{color:"#56B6C2"}}},{content:"(Const.",props:{style:{color:"#ABB2BF"}}},{content:"getLimitRange",props:{style:{color:"#61AFEF"}}},{content:"()) ",props:{style:{color:"#ABB2BF"}}},{content:"// => 0...250 ",props:{style:{color:"#7F848E",fontStyle:"italic"}}}]}],lang:"swift"},annotations:[]}]}),"\n",n.createElement(t.p,null,"Within the type method ",n.createElement(t.code,null,"getLimitRange()")," the type properties are accessed using ",n.createElement(t.code,null,"self.minLimit")," and ",n.createElement(t.code,null,"self.maxLimit"),". In this case ",n.createElement(t.code,null,"self")," refers to ",n.createElement(t.code,null,"Const")," type directly."),"\n",n.createElement(t.p,null,"Interestingly that you can access type properties using two additional forms."),"\n",n.createElement(t.p,null,"Firstly you can omit ",n.createElement(t.code,null,"self"),", and Swift cleverly deduces you're accessing type properties:"),"\n",n.createElement(c.Code,{codeConfig:a,northPanel:{tabs:[""],active:"",heightRatio:1},files:[{name:"",focus:"",code:{lines:[{tokens:[{content:"struct ",props:{style:{color:"#C678DD"}}},{content:"Const",props:{style:{color:"#E5C07B"}}},{content:" {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" static let",props:{style:{color:"#C678DD"}}},{content:" minLimit = ",props:{style:{color:"#ABB2BF"}}},{content:"0",props:{style:{color:"#D19A66"}}}]},{tokens:[{content:" static let",props:{style:{color:"#C678DD"}}},{content:" maxLimit = ",props:{style:{color:"#ABB2BF"}}},{content:"250",props:{style:{color:"#D19A66"}}}]},{tokens:[{content:" static func ",props:{style:{color:"#C678DD"}}},{content:"getLimitRange",props:{style:{color:"#61AFEF"}}},{content:"() -> ",props:{style:{color:"#ABB2BF"}}},{content:"ClosedRange",props:{style:{color:"#E5C07B"}}},{content:"<",props:{style:{color:"#ABB2BF"}}},{content:"Int",props:{style:{color:"#E5C07B"}}},{content:"> {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" return",props:{style:{color:"#C678DD"}}},{content:" minLimit...",props:{style:{color:"#ABB2BF"}}},{content:"maxLimit",props:{style:{color:"#E06C75"}}}]},{tokens:[{content:" }",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"}",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"print",props:{style:{color:"#56B6C2"}}},{content:"(Const.",props:{style:{color:"#ABB2BF"}}},{content:"getLimitRange",props:{style:{color:"#61AFEF"}}},{content:"()) ",props:{style:{color:"#ABB2BF"}}},{content:"// => 0...250",props:{style:{color:"#7F848E",fontStyle:"italic"}}}]}],lang:"swift"},annotations:[]}]}),"\n",n.createElement(t.p,null,"Secondly you can indicate the type ",n.createElement(t.code,null,"Const")," explicitly when accessing the properties:"),"\n",n.createElement(c.Code,{codeConfig:a,northPanel:{tabs:[""],active:"",heightRatio:1},files:[{name:"",focus:"",code:{lines:[{tokens:[{content:"struct ",props:{style:{color:"#C678DD"}}},{content:"Const",props:{style:{color:"#E5C07B"}}},{content:" {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" static let",props:{style:{color:"#C678DD"}}},{content:" minLimit = ",props:{style:{color:"#ABB2BF"}}},{content:"0",props:{style:{color:"#D19A66"}}}]},{tokens:[{content:" static let",props:{style:{color:"#C678DD"}}},{content:" maxLimit = ",props:{style:{color:"#ABB2BF"}}},{content:"250",props:{style:{color:"#D19A66"}}}]},{tokens:[{content:" static func ",props:{style:{color:"#C678DD"}}},{content:"getLimitRange",props:{style:{color:"#61AFEF"}}},{content:"() -> ",props:{style:{color:"#ABB2BF"}}},{content:"ClosedRange",props:{style:{color:"#E5C07B"}}},{content:"<",props:{style:{color:"#ABB2BF"}}},{content:"Int",props:{style:{color:"#E5C07B"}}},{content:"> {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" return",props:{style:{color:"#C678DD"}}},{content:" Const.",props:{style:{color:"#ABB2BF"}}},{content:"minLimit",props:{style:{color:"#E06C75"}}},{content:"...",props:{style:{color:"#ABB2BF"}}},{content:"Const",props:{style:{color:"#E06C75"}}},{content:".",props:{style:{color:"#ABB2BF"}}},{content:"maxLimit",props:{style:{color:"#E06C75"}}}]},{tokens:[{content:" }",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"}",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"print",props:{style:{color:"#56B6C2"}}},{content:"(Const.",props:{style:{color:"#ABB2BF"}}},{content:"getLimitRange",props:{style:{color:"#61AFEF"}}},{content:"()) ",props:{style:{color:"#ABB2BF"}}},{content:"// => 0...250",props:{style:{color:"#7F848E",fontStyle:"italic"}}}]}],lang:"swift"},annotations:[]}]}
1),"\n",n.createElement(t.p,null,n.createElement(t.code,null,"Const.minLimit")," is my personal preference when accessing type properties. ",n.createElement(t.code,null,"Const")," serves as a namespace that groups constants."),"\n",n.createElement(t.p,null,"I find great that Swift allows 3 ways to access static properties. Use the one you like!"),"\n",n.createElement(t.h2,{id:"4-access-instance-properties-and-methods-in-a-closure",style:{position:"relative"}},n.createElement(t.a,{href:"#4-access-instance-properties-and-methods-in-a-closure","aria-label":"4 access instance properties and methods in a closure permalink",className:"anchor before"},n.createElement(t.span,{dangerouslySetInnerHTML:{__html:'<svg aria-hidden="true" height="20" version="1.1" viewBox="0 0 16 16" width="20"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>'}})),"4. Access instance properties and methods in a closure"),"\n",n.createElement(t.p,null,"A closure is a block of code that can be referenced, passed around and invoked when necessary. A closure has access to variables from the environment where it was defined, also called the closure scope."),"\n",n.createElement(t.p,null,"Sometimes you need to define a closure in a method. The closure can access the local method variables. What's more important you have to explicitly write ",n.createElement(t.code,null,"self")," to access instance properties or methods within the closure."),"\n",n.createElement(t.p,null,"Let's define a closure inside a method:"),"\n",n.createElement(c.Code,{codeConfig:a,northPanel:{tabs:[""],active:"",heightRatio:1},files:[{name:"",focus:"",code:{lines:[{tokens:[{content:"class ",props:{style:{color:"#C678DD"}}},{content:"Collection",props:{style:{color:"#E5C07B"}}},{content:" {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" var",props:{style:{color:"#C678DD"}}},{content:" numbers: [",props:{style:{color:"#ABB2BF"}}},{content:"Int",props:{style:{color:"#E5C07B"}}},{content:"]",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" init",props:{style:{color:"#C678DD"}}},{content:"(",props:{style:{color:"#ABB2BF"}}},{content:"from ",props:{style:{color:"#61AFEF"}}},{content:"numbers",props:{style:{color:"#ABB2BF",fontStyle:"italic"}}},{content:": [",props:{style:{color:"#ABB2BF"}}},{content:"Int",props:{style:{color:"#E5C07B"}}},{content:"]) {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" self",props:{style:{color:"#E5C07B"}}},{content:".",props:{style:{color:"#ABB2BF"}}},{content:"numbers",props:{style:{color:"#E06C75"}}},{content:" = numbers",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" }",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" func ",props:{style:{color:"#C678DD"}}},{content:"getAppendClosure",props:{style:{color:"#61AFEF"}}},{content:"() -> (",props:{style:{color:"#ABB2BF"}}},{content:"Int",props:{style:{color:"#E5C07B"}}},{content:") -> ",props:{style:{color:"#ABB2BF"}}},{content:"Void",props:{style:{color:"#E5C07B"}}},{content:" {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" return",props:{style:{color:"#C678DD"}}},{content:" { ",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" self",props:{style:{color:"#E5C07B"}}},{content:".",props:{style:{color:"#ABB2BF"}}},{content:"numbers",props:{style:{color:"#E06C75"}}},{content:".",props:{style:{color:"#ABB2BF"}}},{content:"append",props:{style:{color:"#56B6C2"}}},{content:"(",props:{style:{color:"#ABB2BF"}}},{content:"$0",props:{style:{color:"#E5C07B"}}},{content:")",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" } ",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" }",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"}",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"var",props:{style:{color:"#C678DD"}}},{content:" primes = ",props:{style:{color:"#ABB2BF"}}},{content:"Collection",props:{style:{color:"#E5C07B"}}},{content:"(",props:{style:{color:"#ABB2BF"}}},{content:"from",props:{style:{color:"#61AFEF"}}},{content:": [",props:{style:{color:"#ABB2BF"}}},{content:"2",props:{style:{color:"#D19A66"}}},{content:", ",props:{style:{color:"#ABB2BF"}}},{content:"3",props:{style:{color:"#D19A66"}}},{content:", ",props:{style:{color:"#ABB2BF"}}},{content:"5",props:{style:{color:"#D19A66"}}},{content:"])",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"let",props:{style:{color:"#C678DD"}}},{content:" appendToPrimes = primes.",props:{style:{color:"#ABB2BF"}}},{content:"getAppendClosure",props:{style:{color:"#61AFEF"}}},{content:"()",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"appendToPrimes",props:{style:{color:"#61AFEF"}}},{content:"(",props:{style:{color:"#ABB2BF"}}},{content:"7",props:{style:{color:"#D19A66"}}},{content:")",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"appendToPrimes",props:{style:{color:"#61AFEF"}}},{content:"(",props:{style:{color:"#ABB2BF"}}},{content:"11",props:{style:{color:"#D19A66"}}},{content:")",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"print",props:{style:{color:"#56B6C2"}}},{content:"(primes.",props:{style:{color:"#ABB2BF"}}},{content:"numbers",props:{style:{color:"#E06C75"}}},{content:") ",props:{style:{color:"#ABB2BF"}}},{content:"// => [2, 3, 5, 7, 11]",props:{style:{color:"#7F848E",fontStyle:"italic"}}}]}],lang:"swift"},annotations:[]}]}
1),"\n",n.createElement(t.p,null,n.createElement(t.code,null,"Collection")," is a class that holds some prime numbers.",n.createElement(t.br),"\n","The method ",n.createElement(t.code,null,"getAppendClosure()")," returns a closure that when invoked appends a new number to the collection. To access ",n.createElement(t.code,null,"numbers")," property within the closure ",n.createElement(t.strong,null,"you have to explicitly use ",n.createElement(t.code,null,"self"))," keyword: ",n.createElement(t.code,null,"{ self.numbers.append($0) }")," ."),"\n",n.createElement(t.p,null,"The explicit use of ",n.createElement(t.code,null,"self")," in a closure is an intentional design. Because closure captures the scope variables, including ",n.createElement(t.code,null,"self")," reference, you may create a ",n.createElement(t.a,{href:"https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html#//apple_ref/doc/uid/TP40014097-CH20-ID56",target:"_blank",rel:"noopener noreferrer"},"strong reference cycle")," between the closure and ",n.createElement(t.code,null,"self"),". And you should beware of this potential problem."),"\n",n.createElement(t.p,null,"To create a strong reference cycle, the common scenario is when a property of an instance is a closure that captures the instance itself. Let's see a sample:"),"\n",n.createElement(c.Code,{codeConfig:a,northPanel:{tabs:[""],active:"",heightRatio:1},files:[{name:"",focus:"",code:{lines:[{tokens:[{content:"class ",props:{style:{color:"#C678DD"}}},{content:"Person",props:{style:{color:"#E5C07B"}}},{content:" {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" let",props:{style:{color:"#C678DD"}}},{content:" name: ",props:{style:{color:"#ABB2BF"}}},{content:"String",props:{style:{color:"#E5C07B"}}}]},{tokens:[{content:" lazy var",props:{style:{color:"#C678DD"}}},{content:" sayMyName: () -> ",props:{style:{color:"#ABB2BF"}}},{content:"String",props:{style:{color:"#E5C07B"}}},{content:" = {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" return ",props:{style:{color:"#C678DD"}}},{content:"self",props:{style:{color:"#E5C07B"}}},{content:".",props:{style:{color:"#ABB2BF"}}},{content:"name ",props:{style:{color:"#E06C75"}}}]},{tokens:[{content:" }",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" ",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" init",props:{style:{color:"#C678DD"}}},{content:"(",props:{style:{color:"#ABB2BF"}}},{content:"withName ",props:{style:{color:"#61AFEF"}}},{content:"name",props:{style:{color:"#ABB2BF",fontStyle:"italic"}}},{content:": ",props:{style:{color:"#ABB2BF"}}},{content:"String",props:{style:{color:"#E5C07B"}}},{content:") {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" self",props:{style:{color:"#E5C07B"}}},{content:".",props:{style:{color:"#ABB2BF"}}},{content:"name",props:{style:{color:"#E06C75"}}},{content:" = name",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" }",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" deinit",props:{style:{color:"#C678DD"}}},{content:" {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" print",props:{style:{color:"#56B6C2"}}},{content:"(",props:{style:{color:"#ABB2BF"}}},{content:'"Person deinitialized"',props:{style:{color:"#98C379"}}},{content:")",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" }",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"}",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"var",props:{style:{color:"#C678DD"}}},{content:" leader: Person? ",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"leader = ",props:{style:{color:"#ABB2BF"}}},{content:"Person",props:{style:{color:"#61AFEF"}}},{content:"(",props:{style:{color:"#ABB2BF"}}},{content:"withName",props:{style:{color:"#61AFEF"}}},{content:": ",props:{style:{color:"#ABB2BF"}}},{content:'"John Connor"',props:{style:{color:"#98C379"}}},{content:")",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"if let",props:{style:{color:"#C678DD"}}},{content:" leader = leader {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" print",props:{style:{color:"#56B6C2"}}},{content:"(leader.",props:{style:{color:"#ABB2BF"}}},{content:"sayMyName",props:{style:{color:"#61AFEF"}}},{content:"()) ",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"}",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"leader = ",props:{style:{color:"#ABB2BF"}}},{content:"nil",props:{style:{color:"#D19A66"}}}]}],lang:"swift"},annotations:[]}]}),"\n",n.createElement(t.p,null,"When the variable ",n.createElement(t.code,null,"leader")," is assigned with ",n.createElement(t.code,null,"nil"),", normally the instance should be deinitianilazied. You can expect ",n.createElement(t.code,null,"deinit")," method to be called that prints ",n.createElement(t.code,null,'"Person deinitialized"')," message. However this does not happen."),"\n",n.createElement(t.p,null,"The strong reference cycle creates the problem."),"\n",n.createElement(t.span,{dangerouslySetInnerHTML:{__html:'<span\n class="gatsby-resp-image-wrapper"\n style="position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 560px; "\n >\n <a\n class="gatsby-resp-image-link"\n href="/static/00059e4ec42ea2588abf46a95735ba2c/f292f/two-cats.jpg"\n style="display: block"\n target="_blank"\n rel="noopener"\n >\n <span\n class="gatsby-resp-image-background-image"\n style="padding-bottom: 76.42857142857143%; position: relative; bottom: 0; left: 0; background-image: url(\'data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAPABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAMCBf/EABYBAQEBAAAAAAAAAAAAAAAAAAABAv/aAAwDAQACEAMQAAAB7EtwaCP/xAAcEAABAwUAAAAAAAAAAAAAAAABAAIRAxITITH/2gAIAQEAAQUCAVQ2skOXW4xGgv/EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8BP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EABgQAAMBAQAAAAAAAAAAAAAAAAABERJR/9oACAEBAAY/AjRUQlaXCI//xAAaEAEBAAMBAQAAAAAAAAAAAAABABEhMVFx/9oACAEBAAE/Ic5MwBDtiHBtIuM63xTq+Jf/2gAMAwEAAgADAAAAEEP/AP/EABURAQEAAAAAAAAAAAAAAAAAABAh/9oACAEDAQE/EKf/xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/ED//xAAZEAEBAQEBAQAAAAAAAAAAAAABEQAhQTH/2gAIAQEAAT8QLwN4vJNhevPuFdQoyarTGMw2kNe5kICA4b//2Q==\'); background-size: cover; display: block;"\n ></span>\n <img\n class="gatsby-resp-image-image"\n alt="Two lazy cats like a strong reference cycle"\n title=""\n src="/static/00059e4ec42ea2588abf46a95735ba2c/9605b/two-cats.jpg"\n srcset="/static/00059e4ec42ea2588abf46a95735ba2c/c496c/two-cats.jpg 140w,\n/static/00059e4ec42ea2588abf46a95735ba2c/cbbf7/two-cats.jpg 280w,\n/static/00059e4ec42ea2588abf46a95735ba2c/9605b/two-cats.jpg 560w,\n/static/00059e4ec42ea2588abf46a95735ba2c/f292f/two-cats.jpg 671w"\n sizes="(max-width: 560px) 100v
1w, 560px"\n style="width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"\n loading="lazy"\n decoding="async"\n />\n </a>\n </span>'}}),"\n",n.createElement(t.p,null,"The instance holds the closure reference ",n.createElement(t.code,null,"leader.sayMyName")," and simultaneously the closure captures and holds the instance reference ",n.createElement(t.code,null,"{ return self.name }"),". Under such circumstances the reference cycle cannot be broken and as result both ",n.createElement(t.code,null,"leader")," instance and the closure cannot be deallocated."),"\n",n.createElement(t.p,null,"The solution is to define inside the closure a capture list and mark ",n.createElement(t.code,null,"self")," as an ",n.createElement(t.code,null,"unowned")," reference. Let's fix the previous example:"),"\n",n.createElement(c.Code,{codeConfig:a,northPanel:{tabs:[""],active:"",heightRatio:1},files:[{name:"",focus:"",code:{lines:[{tokens:[{content:"class ",props:{style:{color:"#C678DD"}}},{content:"Person",props:{style:{color:"#E5C07B"}}},{content:" {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" /* ... */",props:{style:{color:"#7F848E",fontStyle:"italic"}}}]},{tokens:[{content:" lazy var",props:{style:{color:"#C678DD"}}},{content:" sayMyName: () -> ",props:{style:{color:"#ABB2BF"}}},{content:"String",props:{style:{color:"#E5C07B"}}},{content:" = { ",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" [",props:{style:{color:"#ABB2BF"}}},{content:"unowned ",props:{style:{color:"#C678DD"}}},{content:"self",props:{style:{color:"#E5C07B"}}},{content:"] ",props:{style:{color:"#ABB2BF"}}},{content:"in",props:{style:{color:"#C678DD"}}}]},{tokens:[{content:" return ",props:{style:{color:"#C678DD"}}},{content:"self",props:{style:{color:"#E5C07B"}}},{content:".",props:{style:{color:"#ABB2BF"}}},{content:"name ",props:{style:{color:"#E06C75"}}}]},{tokens:[{content:" }",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" /* ... */",props:{style:{color:"#7F848E",fontStyle:"italic"}}}]},{tokens:[{content:" deinit",props:{style:{color:"#C678DD"}}},{content:" {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" print",props:{style:{color:"#56B6C2"}}},{content:"(",props:{style:{color:"#ABB2BF"}}},{content:'"Person deinitialized"',props:{style:{color:"#98C379"}}},{content:")",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" }",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"}",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"var",props:{style:{color:"#C678DD"}}},{content:" leader: Person? ",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"leader = ",props:{style:{color:"#ABB2BF"}}},{content:"Person",props:{style:{color:"#61AFEF"}}},{content:"(",props:{style:{color:"#ABB2BF"}}},{content:"withName",props:{style:{color:"#61AFEF"}}},{content:": ",props:{style:{color:"#ABB2BF"}}},{content:'"John Connor"',props:{style:{color:"#98C379"}}},{content:")",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"if let",props:{style:{color:"#C678DD"}}},{content:" leader = leader {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" print",props:{style:{color:"#56B6C2"}}},{content:"(leader.",props:{style:{color:"#ABB2BF"}}},{content:"sayMyName",props:{style:{color:"#61AFEF"}}},{content:"()) ",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"}",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"leader = ",props:{style:{color:"#ABB2BF"}}},{content:"nil",props:{style:{color:"#D19A66"}}}]},{tokens:[{content:'// => "Person deinitialized"',props:{style:{color:"#7F848E",fontStyle:"italic"}}}]}],lang:"swift"},annotations:[]}]}),"\n",n.createElement(t.p,null,"Notice that ",n.createElement(t.code,null,"[unowned self]")," is added in the closure, which marks ",n.createElement(t.code,null,"self")," as an unowned reference. Now the closure does not keep a strong reference to ",n.createElement(t.code,null,"self")," instance. The strong reference cycle between instance and closure is no longer created."),"\n",n.createElement(t.p,null,"Since ",n.createElement(t.code,null,"leader")," instance is not necessary ",n.createElement(t.code,null,"leader = nil"),", the memory is deallocated correctly. The deinitializer ",n.createElement(t.code,null,"deinit")," is called as expected, which prints ",n.createElement(t.code,null,'"Person deinitialized"')," message to the console."),"\n",n.createElement(t.p,null,"I recommend to read more about instances lifetime at ",n.createElement(t.a,{href:"https://www.uraimo.com/2016/10/27/unowned-or-weak-lifetime-and-performance/",target:"_blank",rel:"noopener noreferrer"},"Unowned or Weak? Lifetime and Performance"),"."),"\n",n.createElement(t.p,null,
1n.createElement(t.strong,null,"Important.")," When you access ",n.createElement(t.code,null,"self")," in a closure, you should ",n.createElement(t.em,null,"always")," verify whether a strong reference cycle is not created."),"\n",n.createElement(t.h2,{id:"4-individual-self-usage",style:{position:"relative"}},n.createElement(t.a,{href:"#4-individual-self-usage","aria-label":"4 individual self usage permalink",className:"anchor before"},n.createElement(t.span,{dangerouslySetInnerHTML:{__html:'<svg aria-hidden="true" height="20" version="1.1" viewBox="0 0 16 16" width="20"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>'}})),"4. Individual self usage"),"\n",n.createElement(t.p,null,"Of course there are plenty of situations when you need to return or modify ",n.createElement(t.code,null,"self")," directly. Let's enumerate the common scenarios."),"\n",n.createElement(t.h3,{id:"method-chaining",style:{position:"relative"}},n.createElement(t.a,{href:"#method-chaining","aria-label":"method chaining permalink",className:"anchor before"},n.createElement(t.span,{dangerouslySetInnerHTML:{__html:'<svg aria-hidden="true" height="20" version="1.1" viewBox="0 0 16 16" width="20"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>'}})),"Method chaining"),"\n",n.createElement(t.p,null,"When working with classes, you might want to implement a ",n.createElement(t.a,{href:"https://en.wikipedia.org/wiki/Method_chaining",target:"_blank",rel:"noopener noreferrer"},"method chaining"),". Such practice is useful to chain multiple method calls on the same instance, without storing the intermediate results."),"\n",n.createElement(t.p,null,"The following example is implementing a simple Stack data structure. You can ",n.createElement(t.em,null,"push")," or ",n.createElement(t.em,null,"pop")," elements in the stack. Let's take a look:"),"\n",n.createElement(c.Code,{codeConfig:a,northPanel:{tabs:[""],active:"",heightRatio:1},files:[{name:"",focus:"",code:{lines:[{tokens:[{content:"class ",props:{style:{color:"#C678DD"}}},{content:"Stack",props:{style:{color:"#E5C07B"}}},{content:"<",props:{style:{color:"#ABB2BF"}}},{content:"Element",props:{style:{color:"#E5C07B"}}},{content:"> {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" fileprivate var",props:{style:{color:"#C678DD"}}},{content:" elements = [",props:{style:{color:"#ABB2BF"}}},{content:"Element",props:{style:{color:"#E5C07B"}}},{content:"]()",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" @discardableResult",props:{style:{color:"#C678DD"}}}]},{tokens:[{content:" func ",props:{style:{color:"#C678DD"}}},{content:"push",props:{style:{color:"#61AFEF"}}},{content:"(",props:{style:{color:"#ABB2BF"}}},{content:"_ ",props:{style:{color:"#61AFEF"}}},{content:"element",props:{style:{color:"#ABB2BF",fontStyle:"italic"}}},{content:": ",props:{style:{color:"#ABB2BF"}}},{content:"Element",props:{style:{color:"#E5C07B"}}},{content:") -> Stack {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" elements.",props:{style:{color:"#ABB2BF"}}},{content:"append",props:{style:{color:"#56B6C2"}}},{content:"(element)",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" return ",props:{style:{color:"#C678DD"}}},{content:"self",props:{style:{color:"#E5C07B"}}}]},{tokens:[{content:" }",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" func ",props:{style:{color:"#C678DD"}}},{content:"pop",props:{style:{color:"#61AFEF"}}},{content:"() -> ",props:{style:{color:"#ABB2BF"}}},{content:"Element",props:{style:{color:"#E5C07B"}}},{content:"? {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" return",props:{style:{color:"#C678DD"}}},{content:" elements.",props:{style:{color:"#ABB2BF"}}},{content:"popLast",props:{style:{color:"#56B6C2"}}},{content:"()",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" }",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" ",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" func ",props:{style:{color:"#C678DD"}}},{content:"printElements",props:{style:{color:"#61AFEF"}}},{content:"() {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" print",props:{style:{color:"#56B6C2"}}},{content:"(elements)",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" }",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"}",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"var",props:{style:{color:"#C678DD"}}},{content:" numbers = Stack<",props:{style:{color:"#ABB2BF"}}},{content:"Int",props:{style:{color:"#E5C07B"}}},{content:">()",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"numbers",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" .",props:{style:{color:"#ABB2BF"}}},{content:"push",props:{style:{color:"#61AFEF"}}},{content:"(",props:{style:{color:"#ABB2BF"}}},{content:"8",props:{style:{color:"#D19A66"}}},{content:")",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" .",props:{style:{color:"#ABB2BF"}}},{content:"push",props:{style:{color:"#61AFEF"}}},{content:"(",props:{style:{color:"#ABB2BF"}}},{content:"10",props:{style:{color:"#D19A66"}}},{content:")",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" .",props:{style:{color:"#ABB2BF"}}},{content:"push",props:{style:{color:"#61AFEF"}}},{content:"(",props:{style:{color:"#ABB2BF"}}},{content:"2",props:{style:{color:"#D19A66"}}},{content:")",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"numbers.",props:{style:{color:"#ABB2BF"}}},{content:"printElements",props:{style:{color:"#61AFEF"}}},{content:"() ",props:{style:{color:"#ABB2BF"}}},{content:"// => [8, 10, 2]",props:{style:{color:"#7F848E",fontStyle:"italic"}}}]}],lang:"swift"},annotations:[]}]}),"\n",n.createElement(t.p,null,"The method ",n.createElement(t.code,null,"push(:)")," returns the current instance ",n.createElement(t.code,null,"self"),". This enables the method chaining calls to push multiple elements ",n.createElement(t.code,null,"8"),", ",n.createElement(t.code,null,"10")," and ",n.createElement(t.code,null,"2")," at once into the stack.\nNotice that ",n.createElement(t.code,null,"@discardableResult")," attribute for ",n.createElement(t.code,null,"push(:)")," method suppresses the warning that the result of the last method call in the chain is unused."),"\n",n.createElement(t.h3,{id:"enumeration-case",style:{position:"relative"}}
1,n.createElement(t.a,{href:"#enumeration-case","aria-label":"enumeration case permalink",className:"anchor before"},n.createElement(t.span,{dangerouslySetInnerHTML:{__html:'<svg aria-hidden="true" height="20" version="1.1" viewBox="0 0 16 16" width="20"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>'}})),"Enumeration case"),"\n",n.createElement(t.p,null,"To find what case holds the enumeration within its method, you can easily query ",n.createElement(t.code,null,"self")," property with a ",n.createElement(t.code,null,"switch")," statement."),"\n",n.createElement(t.p,null,"For example, let's get a string message that describes the enumeration cases:"),"\n",n.createElement(c.Code,{codeConfig:a,northPanel:{tabs:[""],active:"",heightRatio:1},files:[{name:"",focus:"",code:{lines:[{tokens:[{content:"enum ",props:{style:{color:"#C678DD"}}},{content:"Activity",props:{style:{color:"#E5C07B"}}},{content:" {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" case",props:{style:{color:"#C678DD"}}},{content:" sleep",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" case",props:{style:{color:"#C678DD"}}},{content:" code",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" case",props:{style:{color:"#C678DD"}}},{content:" learn",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" case",props:{style:{color:"#C678DD"}}},{content:" procrastinate",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" func ",props:{style:{color:"#C678DD"}}},{content:"getOccupation",props:{style:{color:"#61AFEF"}}},{content:"() -> ",props:{style:{color:"#ABB2BF"}}},{content:"String",props:{style:{color:"#E5C07B"}}},{content:" {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" switch ",props:{style:{color:"#C678DD"}}},{content:"self",props:{style:{color:"#E5C07B"}}},{content:" {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" case",props:{style:{color:"#C678DD"}}},{content:" .",props:{style:{color:"#ABB2BF"}}},{content:"sleep",props:{style:{color:"#E06C75"}}},{content:":",props:{style:{color:"#C678DD"}}}]},{tokens:[{content:" return ",props:{style:{color:"#C678DD"}}},{content:'"Sleeping"',props:{style:{color:"#98C379"}}}]},{tokens:[{content:" case",props:{style:{color:"#C678DD"}}},{content:" .",props:{style:{color:"#ABB2BF"}}},{content:"code",props:{style:{color:"#E06C75"}}},{content:":",props:{style:{color:"#C678DD"}}}]},{tokens:[{content:" return ",props:{style:{color:"#C678DD"}}},{content:'"Coding"',props:{style:{color:"#98C379"}}}]},{tokens:[{content:" case",props:{style:{color:"#C678DD"}}},{content:" .",props:{style:{color:"#ABB2BF"}}},{content:"learn",props:{style:{color:"#E06C75"}}},{content:":",props:{style:{color:"#C678DD"}}}]},{tokens:[{content:" return ",props:{style:{color:"#C678DD"}}},{content:'"Reading a book"',props:{style:{color:"#98C379"}}}]},{tokens:[{content:" default:",props:{style:{color:"#C678DD"}}}]},{tokens:[{content:" return ",props:{style:{color:"#C678DD"}}},{content:'"Enjoying laziness"',props:{style:{color:"#98C379"}}}]},{tokens:[{content:" } ",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" }",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"}",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"let",props:{style:{color:"#C678DD"}}},{content:" improving = Activity.",props:{style:{color:"#ABB2BF"}}},{content:"learn",props:{style:{color:"#E06C75"}}}]},{tokens:[{content:"print",props:{style:{color:"#56B6C2"}}},{content:"(improving.",props:{style:{color:"#ABB2BF"}}},{content:"getOccupation",props:{style:{color:"#61AFEF"}}},{content:"()) ",props:{style:{color:"#ABB2BF"}}},{content:'// => "Reading a book"',props:{style:{color:"#7F848E",fontStyle:"italic"}}}]}],lang:"swift"},annotations:[]}]}),"\n",n.createElement(t.p,null,"The method ",n.createElement(t.code,null,"getOccupation()")," accesses ",n.createElement(t.code,null,"self")," to determine the current enumeration case."),"\n",n.createElement(t.h3,{id:"new-structure-instance",style:{position:"relative"}}
1,n.createElement(t.a,{href:"#new-structure-instance","aria-label":"new structure instance permalink",className:"anchor before"},n.createElement(t.span,{dangerouslySetInnerHTML:{__html:'<svg aria-hidden="true" height="20" version="1.1" viewBox="0 0 16 16" width="20"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>'}})),"New structure instance"),"\n",n.createElement(t.p,null,"In a structure you can dynamically modify the current instance by assigning to ",n.createElement(t.code,null,"self")," a new value:"),"\n",n.createElement(c.Code,{codeConfig:a,northPanel:{tabs:[""],active:"",heightRatio:1},files:[{name:"",focus:"",code:{lines:[{tokens:[{content:"struct ",props:{style:{color:"#C678DD"}}},{content:"Movement",props:{style:{color:"#E5C07B"}}},{content:" {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" var",props:{style:{color:"#C678DD"}}},{content:" speed: ",props:{style:{color:"#ABB2BF"}}},{content:"Int",props:{style:{color:"#E5C07B"}}}]},{tokens:[{content:"",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" mutating func ",props:{style:{color:"#C678DD"}}},{content:"stop",props:{style:{color:"#61AFEF"}}},{content:"() {",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" self",props:{style:{color:"#E5C07B"}}},{content:" = ",props:{style:{color:"#ABB2BF"}}},{content:"Movement",props:{style:{color:"#61AFEF"}}},{content:"(",props:{style:{color:"#ABB2BF"}}},{content:"speed",props:{style:{color:"#61AFEF"}}},{content:": ",props:{style:{color:"#ABB2BF"}}},{content:"0",props:{style:{color:"#D19A66"}}},{content:")",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:" }",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"}",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"var",props:{style:{color:"#C678DD"}}},{content:" run = ",props:{style:{color:"#ABB2BF"}}},{content:"Movement",props:{style:{color:"#61AFEF"}}},{content:"(",props:{style:{color:"#ABB2BF"}}},{content:"speed",props:{style:{color:"#61AFEF"}}},{content:": ",props:{style:{color:"#ABB2BF"}}},{content:"20",props:{style:{color:"#D19A66"}}},{content:")",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"print",props:{style:{color:"#56B6C2"}}},{content:"(run.",props:{style:{color:"#ABB2BF"}}},{content:"speed",props:{style:{color:"#E06C75"}}},{content:") ",props:{style:{color:"#ABB2BF"}}},{content:"// => 20",props:{style:{color:"#7F848E",fontStyle:"italic"}}}]},{tokens:[{content:"run.",props:{style:{color:"#ABB2BF"}}},{content:"stop",props:{style:{color:"#61AFEF"}}},{content:"() ",props:{style:{color:"#ABB2BF"}}}]},{tokens:[{content:"print",props:{style:{color:"#56B6C2"}}},{content:"(run.",props:{style:{color:"#ABB2BF"}}},{content:"speed",props:{style:{color:"#E06C75"}}},{content:") ",props:{style:{color:"#ABB2BF"}}},{content:"// => 0",props:{style:{color:"#7F848E",fontStyle:"italic"}}}]}],lang:"swift"},annotations:[]}]}),"\n",n.createElement(t.p,null,"In the mutating method ",n.createElement(t.code,null,"stop()")," the assignment ",n.createElement(t.code,null,"self = Movement(speed: 0)")," modifies the current instance to a new one."),"\n",n.createElement(t.h2,{id:"5-conclusion",style:{position:"relative"}},n.createElement(t.a,{href:"#5-conclusion","aria-label":"5 conclusion permalink",className:"anchor before"},n.createElement(t.span,{dangerouslySetInnerHTML:{__html:'<svg aria-hidden="true" height="20" version="1.1" viewBox="0 0 16 16" width="20"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>'}}
1)),"5. Conclusion"),"\n",n.createElement(t.p,null,n.createElement(t.code,null,"self")," is a property on the instance that refers to itself. It's used to access class, structure and enumeration instance within methods."),"\n",n.createElement(t.p,null,"When ",n.createElement(t.code,null,"self")," is accessed in a type method (",n.createElement(t.code,null,"static func")," or ",n.createElement(t.code,null,"class func"),"), it refers to the actual type (rather than an instance)."),"\n",n.createElement(t.p,null,"Swift allows to omit ",n.createElement(t.code,null,"self")," when you want to access instances properties. My advice is to rely on shortness and skip ",n.createElement(t.code,null,"self")," whenever possible."),"\n",n.createElement(t.p,null,"When a method parameter have the same name as instance property, you have to explicitly use ",n.createElement(t.code,null,"self.myVariable = myVariable")," to make a distinction.",n.createElement(t.br),"\n","Notice that method parameters have a priority over instance properties."),"\n",n.createElement(t.p,null,n.createElement(t.em,null,"Do you think ",n.createElement(t.code,null,"self")," should be omitted or kept? Feel free to write your opinion in the comments section below!")))}var i=function(e){void 0===e&&(e={});const{wrapper:t}=Object.assign({},(0,r.ah)(),e.components);return t?n.createElement(t,e,n.createElement(l,e)):l(e)};function p(e,t){throw new Error("Expected "+(t?"component":"object")+" `"+e+"` to be defined: you likely forgot to import, pass, or provide it.")}var d=o(12),u=o(5319),m=o(5893);function g(e){let{data:t,children:o}=e;const{featured:{popularPostsByCategory:n}}=t.site.siteMetadata,{mdx:r,popularPostsMarkdown:s}=t,c={...r.frontmatter,children:o,thumbnail:r.frontmatter.thumbnail.childImageSharp.gatsbyImageData,tableOfContents:r.tableOfContents},a=r.internal.contentFilePath.split("/").slice(-3).join("/"),l=s.edges.map(u.sN),i=n.map((e=>{let{category:t,slugs:o}=e;return{category:t,plainPosts:o.map((e=>l.find((t=>{let{slug:o}=t;return e===o}))))}}));return(0,m.jsx)(d.Z,{postRelativePath:a,post:c,popularPostsByCategory:i})}function y(e){return n.createElement(g,e,n.createElement(i,e))}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.