accessor

How to force calling methods to leave parentheses off accessor (getter) methods

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 5.6, “How to force calling methods to leave parentheses off accessor (getter) methods.”

Problem

You want to enforce a coding style where getter/accessor methods can’t have parentheses when they are invoked.

Solution

Define your getter/accessor method without parentheses after the method name:

How to prevent getter and setter methods from being generated in Scala classes

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 4.7, “How to prevent getter and setter methods from being generated in Scala classes.”

Problem

When you define a class field as a var, Scala automatically generates getter and setter methods for the field, and defining a field as a val automatically generates a getter method, but you don’t want either a getter or setter.

How to override default accessors and mutators in Scala classes

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 4.6, “How to override default accessors and mutators in Scala classes.”

Problem

You want to override the getter or setter methods that Scala generates for you.