What def, val, and var fields in Scala traits look like after they’re compiled (including the classes that extend them)
I generally have a pretty good feel for how Scala traits work, and how they can be used for different needs. As one example, a few years ago I learned that it’s best to define abstract fields in traits using
def. But there are still a few things I wonder about.
Today I had a few free moments and I decided to look at what happens under the covers when you use
var fields in traits, and then mix-in or extend those traits with classes. So I created some examples, compiled them with
scalac -Xprint:all, and then decompiled them with JAD to see what everything looks like under the covers.
I was initially going to write a summary here, but if you want to know how things work under the hood, I think it helps to work through the examples, so for today I’ll leave that as an exercise for the reader.
Java Design Patterns FAQ: Can you provide an example of the Factory Pattern in Java?
Sure. In this article we'll look at a small-but-complete example of the Factory Pattern ("Factory Design Pattern") implemented in Java.
This is a page from my book, Functional Programming, Simplified
When I update the Scala Cookbook, I need to update Recipe 8.2, “How to use abstract and concrete fields in Scala traits.” That recipe is written particularly with an OOP developer in mind, and I didn’t write about handling the same situation from an FP perspective.
Here are a few notes about using Scala traits as mixins, specifically:
- The order in which mixed-in traits are constructed
- The order in which overridden methods in traits are called when multiple traits are mixed in
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 8.1, “How to use a Scala trait as an interface.”
You’re used to creating interfaces in other languages like Java and want to create something like that in Scala.
You can use a Scala
trait just like a Java
interface. As with interfaces, just declare the methods in your trait that you want extending classes to implement:
Scala FAQ: How do I override the default numeric type that Scala assigns when I declare an
Double val or var field?
If you assign
1 to a variable, Scala assigns it the type
Android FAQ: How do I write a custom
View class in Android? (Or, how do I extend a
If you’re interested in seeing a Scala version of Oracle’s JavaFX “Hello, world” application, here you go:
This page contains a collection of Scala method examples. I created many of these examples while I was writing the Scala Cookbook. Unlike the Cookbook, where I explain these examples in great detail, on this page I’m just sharing many of the examples so you can use this as a method/function reference page. (The Cookbook contains more examples than this page, and explains them in detail.)
Summary: A discussion of the Strategy Design Pattern using Java source code examples.
The Strategy Design Pattern consists of a number of related algorithms encapsulated in a driver class often named Context. A user or a client program typically selects the algorithm they want to use, although the Context class may also select the algorithm automatically.
The intent of the Strategy Pattern is to make the algorithms easily interchangeable, and provide a means to choose the appropriate algorithm at a particular time.