I&#x27;m going to repost something I had said in response to this article on another site. I think it&#x27;s an awesome example of what can happen when you create a language with extensibility in mind.<p>-------------------<p>Scala solves this problem really well with typeclasses.<p>You can define a typeclass like<p><pre><code>    case class Refrigerated[T](thing: T) &#x2F;&#x2F; this is just something holding things that have been refrigerated. 
    trait Refrigeratable[T] { &#x2F;&#x2F; this is the typeclass
      def refrigerate(thing: T): Refrigerated[T]
    }
</code></pre>
Now we have a refrigerated object that holds some object that&#x27;s been refrigerated, and it looks like I&#x27;m about to do some class composition, but I&#x27;m not. Bear with me.<p>Now, we have some sort of data class<p><pre><code>    case class Pillow(fluffiness: Int, warmness: Int)
</code></pre>
Pretty straight forward, we now have a Pillow with measurable fluffiness and warmth<p>Next, lets implement refrigerated for pillow.<p><pre><code>    implicit val pillowRefrigerable: Refrigerable[Pillow] = new Refrigerable[Pillow] {
      def refrigerate(pillow: Pillow): Refrigerated[T] = 
        Refrigerated(pillow.copy(warmness = pillow.warmness &#x2F; 2))
    }
</code></pre>
So there&#x27;s an implementation of Refrigerable for pillow. Refrigerating a pillow just cuts the warmness in half.<p>So actually using the Refrigerable stuff. There&#x27;s two ways to go about this. One way is to just use the Refrigerable instance to refrigerate the pillow.<p><pre><code>    def refrigeratePillow(pillow: Pillow): Refrigerated[Pillow] = pillowRefrigerable.refrigerate(pillow)
</code></pre>
Nothing special there. You could basically do that in any language. So how does scala do it better? Well, we can define a refrigerate method for anything Refrigerable like this:<p><pre><code>    def refrigerate[T](thing: T)(implicit refrigerableInstance: Refrigerable[T]): Refrigerated[T] = 
      refrigerableInstance.refrigerate(thing)
</code></pre>
Now this is really cool. Anything type that we have a Refrigerable instance for, can be used as an argument for refrigerate. And we don&#x27;t have to explicitly state what Refrigerable instance we&#x27;re using. Calling it looks like<p><pre><code>    val refrigeratedThing = refrigerate(anythingWithARefrigerableInstanceHere)
</code></pre>
Okay. We still haven&#x27;t gained a whole lot. All we&#x27;ve done so far, is make it so we don&#x27;t have to explicitly state what Refrigerable to use. So let&#x27;s go an extra step. Let&#x27;s make it so we just call refrigerate on the Object itself rather than passing the object as an argument to a method. This is possible, because scala let&#x27;s us make extension methods for generics.<p><pre><code>    implicit class RefrigerableOps[T](thing: T)(implicit refrigerableInstance: Refrigerable[T]) {
      def refrigerate: Refrigerated[T] = refrigerableInstance.refrigerate(thing)
    }
</code></pre>
Okay, now this is really cool. We&#x27;ve defined a method for any type for which we have Refrigerable in scope. This means we can now refrigerate our pillow more easily:<p><pre><code>    val refrigeratedPillow = myPillow.refrigerate
</code></pre>
What?! That&#x27;s great! And it gets better. Now that we&#x27;ve implemented this boilerplate, giving other things this wonderful syntax is easy. Let&#x27;s say I want to refrigerate my cousin, because she&#x27;s too hot.<p><pre><code>    case class Cousin(hotness: Int)
    implicit val refrigerableCousin: Refrigerable[Cousin] = new Refrigerable[Cousin] {
      def refrigerate(cousin: Cousin) = 
        Refrigerated(cousin.copy(hotness = cousin.hotness - 1))
    }
</code></pre>
Now we can make our cousin less hot! Great!<p><pre><code>    val refrigeratedCousin = cousin.refrigerate

</code></pre>
This is basically a good way to make things open to extension without subclassing it&#x27;s awesome!<p>You can even do more to simplify making the Refrigerable instances. You can effectively make them one liners.