"what does it mean to instantiate a trait?" Code Answer


i'm reasonably new to scala as well, but i think the example that you have above is creating a new anonymous object (like you can do with java), and then attaching the trait to the new anonymous type.

since scala traits can contain implementation code, you basically get the behavior of the trait for free, assuming the anonymous class satisfies any requirements of the trait (like having values set, or defining "abstract" methods).

so what you are doing here is not instantiating the trait directly, but rather creating a suitable object for the trait to attach itself to so you can use the trait's functionality without needing to define a class that extends the trait.

does that answer your question?

By Arci on September 30 2022
Only authorized users can answer the Search term. Please sign in first, or register a free account.