top of page

Scala Extension methods via implicit classes

  • Mar 23, 2015
  • 1 min read

Updated: Jun 18, 2018



In this post i will demonstrate a nice trick to add “extension methods” for a given class.

Note: The feature is based on implicit class feature and available only in Scala 2.10



A class Cat has a name and a list of kittens. in this example we add a method named “hasKittens()” to the Cat class. This is done by creating the implicit class SmellyCat which implicitly wraps our Cat instance when calling “hasKittens()”.

The following test shows the usage of this feature:



To use the extended function you need to import the class (import implicits._).

Note: implicit class cannot be on the top level of the project and must be wrapped inside an object.

 
 
 

Comments


The Latest Posts:

  • _AIEngineering
  • _AIEngineering

From Co-Pilot to Full Automation: How Wix Is Embedding AI Agents Across an Engineering Org at Scale

Kickstart your engineering career at Wix

Kickstart your engineering career at Wix

How We Built a Zero-Downtime Database Migration Service at Wix

Kickstart your engineering career at Wix

Kickstart your engineering career at Wix

We Ran 250 AI Agent Evals to Find Out if Skills Beat Docs. The Answer Is More Complicated Than We Expected

bottom of page