<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Scala Snippets]]></title><description><![CDATA[Scala Snippets]]></description><link>https://scalasnippets.dev</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1747936704461/019d103b-189d-47ff-8f21-1209370cb9a0.png</url><title>Scala Snippets</title><link>https://scalasnippets.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Thu, 16 Apr 2026 01:39:57 GMT</lastBuildDate><atom:link href="https://scalasnippets.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Scala for Beginners: How to Define Variables]]></title><description><![CDATA[Learn how to declare values and variables the idiomatic way in Scala — immutability first.

Introduction
Before you build classes, objects, or APIs, you need to understand the most basic building block: variables.
Scala makes a big deal about immutab...]]></description><link>https://scalasnippets.dev/scala-for-beginners-how-to-define-variables</link><guid isPermaLink="true">https://scalasnippets.dev/scala-for-beginners-how-to-define-variables</guid><category><![CDATA[Scala]]></category><category><![CDATA[learn coding]]></category><category><![CDATA[development]]></category><category><![CDATA[Blogging]]></category><category><![CDATA[post]]></category><category><![CDATA[Scala-Basics]]></category><category><![CDATA[eLearning]]></category><category><![CDATA[code snippets]]></category><dc:creator><![CDATA[Saiprasad Vyawahare]]></dc:creator><pubDate>Sun, 29 Jun 2025 07:05:38 GMT</pubDate><content:encoded><![CDATA[<h1 id="heading-learn-how-to-declare-values-and-variables-the-idiomatic-way-in-scala-immutability-first"><em>Learn how to declare values and variables the idiomatic way in Scala — immutability first.</em></h1>
<hr />
<h2 id="heading-introduction">Introduction</h2>
<p>Before you build classes, objects, or APIs, you need to understand the most basic building block: <strong>variables</strong>.</p>
<p>Scala makes a big deal about <strong>immutability</strong>, and this starts with how you declare data.</p>
<p>Let’s walk through the difference between <code>val</code> and <code>var</code> — and when to use them.</p>
<hr />
<h2 id="heading-val-immutable-value"><code>val</code> – Immutable Value</h2>
<pre><code class="lang-scala"><span class="hljs-keyword">val</span> name = <span class="hljs-string">"Alice"</span>
<span class="hljs-keyword">val</span> age = <span class="hljs-number">30</span>
</code></pre>
<ul>
<li>✅ Think of <code>val</code> like a final variable in Java or a <code>const</code> in JavaScript.</li>
<li>❌ You <strong>cannot reassign</strong> a <code>val</code>.</li>
</ul>
<pre><code class="lang-scala"><span class="hljs-comment">// This will cause a compilation error:</span>
name = <span class="hljs-string">"Bob"</span> <span class="hljs-comment">// ❌ reassignment to val</span>
</code></pre>
<ul>
<li>✅ Use val by default — it makes your code safer and easier to reason about.</li>
</ul>
<h2 id="heading-var-mutable-variable"><code>var</code> – Mutable Variable</h2>
<pre><code class="lang-scala"><span class="hljs-keyword">var</span> count = <span class="hljs-number">1</span>
count = count + <span class="hljs-number">1</span>
</code></pre>
<ul>
<li>You can reassign var as needed.</li>
</ul>
<h2 id="heading-type-inference-and-explicit-types">Type Inference (and Explicit Types)</h2>
<p>Scala can often <strong>infer the type</strong> of a value based on the assigned data:</p>
<pre><code class="lang-scala"><span class="hljs-keyword">val</span> city = <span class="hljs-string">"Mumbai"</span>   <span class="hljs-comment">// Inferred as String</span>
<span class="hljs-keyword">val</span> score = <span class="hljs-number">4.5</span>       <span class="hljs-comment">// Inferred as Double</span>
</code></pre>
<p>But you can <strong>also write the type explicitly</strong> when you want to be more specific or improve readability:</p>
<pre><code class="lang-scala"><span class="hljs-keyword">val</span> city: <span class="hljs-type">String</span> = <span class="hljs-string">"Mumbai"</span>
<span class="hljs-keyword">val</span> score: <span class="hljs-type">Double</span> = <span class="hljs-number">4.5</span>
</code></pre>
<p>This is especially useful in function signatures, public APIs, or when collaborating in a team.</p>
<h2 id="heading-summary">Summary</h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Keyword</td><td>Mutable?</td><td>Recommended?</td><td>Use When</td></tr>
</thead>
<tbody>
<tr>
<td><code>val</code></td><td>❌ No</td><td>✅ Yes (default)</td><td>When the value should not change</td></tr>
<tr>
<td><code>var</code></td><td>✅ Yes</td><td>❌ Only when needed</td><td>When you need to reassign the value</td></tr>
</tbody>
</table>
</div>]]></content:encoded></item><item><title><![CDATA[Setting Up Scala in IntelliJ IDEA (Step-by-Step for Beginners)]]></title><description><![CDATA[Setting Up Scala in IntelliJ IDEA (Step-by-Step for Beginners)
Ready to go beyond online editors? Let’s set up Scala on your machine using IntelliJ IDEA.

🚀 Why IntelliJ IDEA?
JetBrains IntelliJ IDEA is one of the most powerful and developer-friendl...]]></description><link>https://scalasnippets.dev/setting-up-scala-in-intellij-idea-step-by-step-for-beginners</link><guid isPermaLink="true">https://scalasnippets.dev/setting-up-scala-in-intellij-idea-step-by-step-for-beginners</guid><category><![CDATA[intellij]]></category><category><![CDATA[software development]]></category><category><![CDATA[Scala]]></category><category><![CDATA[setup]]></category><category><![CDATA[intellij idea]]></category><category><![CDATA[sbt]]></category><category><![CDATA[Scala-Basics]]></category><dc:creator><![CDATA[Saiprasad Vyawahare]]></dc:creator><pubDate>Fri, 06 Jun 2025 18:38:08 GMT</pubDate><content:encoded><![CDATA[<h1 id="heading-setting-up-scala-in-intellij-idea-step-by-step-for-beginners">Setting Up Scala in IntelliJ IDEA (Step-by-Step for Beginners)</h1>
<p><em>Ready to go beyond online editors? Let’s set up Scala on your machine using IntelliJ IDEA.</em></p>
<hr />
<h2 id="heading-why-intellij-idea">🚀 Why IntelliJ IDEA?</h2>
<p><a target="_blank" href="https://www.jetbrains.com/idea/">JetBrains IntelliJ IDEA</a> is one of the most powerful and developer-friendly IDEs out there, especially for JVM-based languages like Java and Scala.</p>
<p>It offers:</p>
<ul>
<li>✅ Smart autocompletion and refactoring</li>
<li>✅ Built-in sbt support</li>
<li>✅ Great integration with Scala worksheets and test frameworks</li>
</ul>
<hr />
<h2 id="heading-prerequisites">🧰 Prerequisites</h2>
<p>Before we start, make sure you have:</p>
<ul>
<li>✅ <strong>Java SDK</strong> installed (JDK 8 or above)</li>
<li>✅ Internet connection to download IntelliJ and plugins</li>
</ul>
<hr />
<h2 id="heading-step-1-download-and-install-intellij-idea">🧱 Step 1: Download and Install IntelliJ IDEA</h2>
<p>👉 Go to <a target="_blank" href="https://www.jetbrains.com/idea/download">https://www.jetbrains.com/idea/download</a><br />Download the <strong>Community Edition</strong> (it’s free and supports Scala with plugins).</p>
<p>Install it like any normal app.</p>
<hr />
<h2 id="heading-step-2-install-the-scala-plugin">🔌 Step 2: Install the Scala Plugin</h2>
<ol>
<li>Open IntelliJ IDEA</li>
<li>Go to <code>Settings</code> (or <code>Preferences</code> on macOS)</li>
<li>Navigate to <strong>Plugins</strong></li>
<li>Search for <strong>Scala</strong></li>
<li>Click <strong>Install</strong></li>
<li>Restart the IDE</li>
</ol>
<p>✅ Now IntelliJ knows how to compile and run Scala code!</p>
<hr />
<h2 id="heading-step-3-create-a-new-scala-project">📦 Step 3: Create a New Scala Project</h2>
<ol>
<li>Click on <strong>New Project</strong></li>
<li>Choose <strong>Scala</strong> from the left menu</li>
<li>Under <strong>Build System</strong>, select <strong>sbt</strong></li>
<li>Set your project name, location, and Scala version</li>
<li>Click <strong>Finish</strong></li>
</ol>
<p>Wait for sbt to download dependencies — it might take a minute.</p>
<hr />
<h2 id="heading-step-4-add-a-simple-scala-file">📄 Step 4: Add a Simple Scala File</h2>
<p>Inside <code>src/main/scala</code>, right-click → <code>New</code> → <code>Scala Class</code> → Name it <code>HelloWorld</code></p>
<p>Paste the following code:</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">HelloWorld</span> </span>{
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span></span>(args: <span class="hljs-type">Array</span>[<span class="hljs-type">String</span>]): <span class="hljs-type">Unit</span> = {
    println(<span class="hljs-string">"Hello from IntelliJ!"</span>)
  }
}
</code></pre>
<h2 id="heading-step-5-run-your-code">▶️ Step 5: Run Your Code</h2>
<ul>
<li>Right-click inside the main method or on the file</li>
<li>Choose Run <code>HelloWorld</code></li>
</ul>
<p>✅ You should see the output in the console:
<code>Hello from IntelliJ!</code></p>
<h2 id="heading-bonus-try-a-scala-worksheet">🧪 Bonus: Try a Scala Worksheet</h2>
<p>Scala Worksheets allow you to write and evaluate code interactively.
To create one:</p>
<ul>
<li>Right-click on your <code>src</code> folder</li>
<li>Go to <code>New</code> → <code>Scala Worksheet</code></li>
<li>Name it <code>Playground</code></li>
</ul>
<p>Try typing:</p>
<pre><code class="lang-scala"><span class="hljs-keyword">val</span> name = <span class="hljs-string">"Scala"</span>
<span class="hljs-keyword">val</span> version = <span class="hljs-number">3.3</span>
<span class="hljs-string">s"Hello <span class="hljs-subst">$name</span> <span class="hljs-subst">$version</span>"</span>
</code></pre>
<p>You’ll see the result instantly on the right!</p>
<h2 id="heading-thats-it">🎯 That’s It!</h2>
<p>Now you have a full Scala development environment running locally with IntelliJ IDEA.</p>
<h2 id="heading-whats-next">🔍 What’s Next?</h2>
<p>Now that you’ve seen how easy it is to run Scala, here’s what you can do next:</p>
<p>👉 <a target="_blank" href="https://scalasnippets.dev/scala-for-beginners-how-to-define-variables">How to Define Variables</a></p>
]]></content:encoded></item><item><title><![CDATA[Getting Started with Scala: Your First Snippet]]></title><description><![CDATA[A friendly Scala intro for developers who just want to write their first few lines of code.
Scala might look intimidating at first, but let’s change that — one clean snippet at a time.

👋 Why Scala?
Scala combines the power of object-oriented and fu...]]></description><link>https://scalasnippets.dev/getting-started-with-scala-your-first-snippet</link><guid isPermaLink="true">https://scalasnippets.dev/getting-started-with-scala-your-first-snippet</guid><category><![CDATA[scalasnippets]]></category><category><![CDATA[Scala]]></category><category><![CDATA[Functional Programming]]></category><category><![CDATA[getting started]]></category><category><![CDATA[First Blog]]></category><category><![CDATA[#FirstBlogPost]]></category><category><![CDATA[learning]]></category><dc:creator><![CDATA[Saiprasad Vyawahare]]></dc:creator><pubDate>Thu, 22 May 2025 16:52:31 GMT</pubDate><content:encoded><![CDATA[<p>A friendly Scala intro for developers who just want to write their first few lines of code.</p>
<p><em>Scala might look intimidating at first, but let’s change that — one clean snippet at a time.</em></p>
<hr />
<h2 id="heading-why-scala">👋 Why Scala?</h2>
<p>Scala combines the power of object-oriented and functional programming in one language. It runs on the JVM, making it a great fit for scalable, high-performance apps — but more importantly, <strong>you can just start small</strong>.</p>
<hr />
<h2 id="heading-lets-get-started-a-simple-hello-world">🛠️ Let’s Get Started — A Simple Hello World</h2>
<p>Here’s how you write your first Scala program:</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">HelloWorld</span> </span>{
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span></span>(args: <span class="hljs-type">Array</span>[<span class="hljs-type">String</span>]): <span class="hljs-type">Unit</span> = {
    println(<span class="hljs-string">"Hello, Scala!"</span>)
  }
}
</code></pre>
<h2 id="heading-whats-happening">🧠 What’s Happening?</h2>
<ul>
<li><code>object</code> is a singleton — like a class with only one instance.</li>
<li><code>main</code> is the entry point (just like in Java).</li>
<li><code>println(...)</code> prints to the console.</li>
</ul>
<h2 id="heading-to-run-this">✅ To run this:</h2>
<ul>
<li>Use the Scala REPL (via terminal or sbt console)</li>
<li>Or paste it into Scastie: https://scastie.scala-lang.org and press ▶️ <code>Run</code>. That’s it!</li>
</ul>
<h2 id="heading-whats-next">🔍 What’s Next?</h2>
<p>Now that you’ve seen how easy it is to run Scala, here’s what you can do next:</p>
<p>👉 <a target="_blank" href="https://scalasnippets.dev/setting-up-scala-in-intellij-idea-step-by-step-for-beginners">Install Scala in IntelliJ IDEA</a> – set up a real development environment</p>
]]></content:encoded></item></channel></rss>