Scala Tutorial

Basics

Control Statements

OOP Concepts

Parameterized - Type

Exceptions

Scala Annotation

Methods

String

Scala Packages

Scala Trait

Collections

Scala Options

Miscellaneous Topics

Scala Keywords

Scala, like other programming languages, reserves a set of keywords for its syntactic structure. These keywords have special meanings and cannot be used as identifiers (unless they are surrounded by backticks, as described in the previous answer).

Here's a list of keywords in Scala:

abstract    case      do         else       extends    false      final      finally    for
forSome    if        implicit   import     lazy       match      new        null       object
override   package   private    protected  return     sealed     super      this       throw
trait      true      try        type       val        var        while      with       yield

In addition to these keywords, there are a few contextual keywords. These are reserved only in specific contexts and can be used as identifiers in other situations. Some of them include:

  • then: Used in the context of the new control syntax introduced in Scala 3.
  • export: Another keyword introduced in Scala 3 for re-exporting members.
  • given, as, using: Used for implicits in Scala 3.

Contextual keywords are designed to minimize breaking changes while still allowing the language to evolve.

Remember that if you need to use any of these keywords as an identifier (for example, when integrating with a Java library where a method name might clash with a Scala keyword), you can enclose the name in backticks:

val `yield` = "I'm a variable named yield!"

However, it's generally best to avoid such cases unless absolutely necessary, as it can make the code harder to read.

  1. Scala Reserved Words:

    Reserved words are not actively used in the language but are reserved for future use. They may have been reserved for compatibility with future versions of Scala.

  2. Keywords in Scala Programming:

    Keywords are words that have a specific meaning and functionality in the current version of Scala. They are actively used and play a crucial role in the language.

  3. Scala Language Keyword Reference:

    Scala has a set of keywords that have specific meanings. These include val, var, def, class, object, if, else, while, for, and more.

  4. How to Use Scala Keywords:

    Keywords are used as part of the language syntax. For example:

    val x: Int = 42
    def myFunction(): Unit = {
      // Function body
    }
    
  5. Common Scala Programming Keywords:

    Some common keywords include val, var, def, class, object, if, else, while, for, match, case, trait, try, catch, finally, and more.

  6. Scala Keyword Usage Examples:

    Examples of using Scala keywords:

    val x: Int = 42
    
    def myFunction(): Unit = {
      // Function body
    }
    
    class MyClass {
      // Class definition
    }
    
    object MyObject {
      // Object definition
    }
    
    if (x > 0) {
      // Code block for positive condition
    } else {
      // Code block for negative condition
    }
    
  7. Reserved Words in Scala:

    Reserved words in Scala are not actively used, and their meaning may be defined in future versions of the language.