A Snippet of Dotty

opaque type IArray[T] = Array[T]object IArray {
implied arrayOps {
inline def (arr: IArray[T]) apply[T] (n: Int): T =
(arr: Array[T]).apply(n)
inline def (arr: IArray[T]) length[T] : Int =
(arr: Array[T]).length
}
def apply[T: ClassTag](xs: T*): IArray[T] = Array(xs: _*)
...
}

Opaque Types

opaque type IArray[T] = Array[T]
case class Foo(b: Bar) extends AnyVal
def apply[T: ClassTag](xs: T*): IArray[T] = Array(xs: _*)

Implied Instances

object IArray {
implied arrayOps {
inline def (arr: IArray[T]) apply[T] (n: Int): T =
object IArray {
implicit class arrayOps(arr: IArray[T]) {
def apply[T](n: Int): T = ...
}
}

Extension Methods

    inline def (arr: IArray[T]) apply[T] (n: Int): T = 
(arr: Array[T]).apply(n)
inline def (arr: IArray[T]) length[T] : Int =
(arr: Array[T]).length
def (arr: IArray[T]) apply[T] (n: Int): T

Inline

    inline def (arr: IArray[T]) apply[T] (n: Int): T = 
(arr: Array[T]).apply(n)
inline def (arr: IArray[T]) length[T] : Int =
(arr: Array[T]).length

Wrapping it Up

  • opaque lets you easily create type aliases that really hide their underlying types, so you can write more strongly-typed code.
  • Those aliases are inherently low-overhead, with no boxing.
  • You can use inline to reduce the overhead of functions on those opaque types, sometimes to zero.
  • You can use implied to make functions available for a type.
  • The extension-method syntax lets you add new “methods” to existing types from the outside, without the syntactic complexity of the old implicit class mechanism.

--

--

--

Lifelong programmer and software architect, specializing in online social tools and (nowadays) Scala. Architect of Querki (“leading the small data revolution”).

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Step 7: Windows Fundamentals

Transferring Data with AWS Data Sync

Terraform remote state — sharing information across state files

Blog Application in Django

Automated API testing with Postman

GCP — Deploying React App with .NET Web API on GKE

Lucky Lion— Lucky Friday Tournament#3

Checklist: Getting Started with Mobile Mapping Image Blurring

cloud-vs-on-premise-image-blurring-mobile-mapping

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mark "Justin" Waks

Mark "Justin" Waks

Lifelong programmer and software architect, specializing in online social tools and (nowadays) Scala. Architect of Querki (“leading the small data revolution”).

More from Medium

sbt Library Dependencies et al

Currying, the constructor of functions

SLF4J Logging in Spark-Scala

Unleash the Syntactic Sugar in Scala