We use cookies and other tracking technologies to improve your browsing experience on our site, analyze site traffic, and understand where our audience is coming from. To find out more, please read our privacy policy.

By choosing 'I Accept', you consent to our use of cookies and other tracking technologies.

We use cookies and other tracking technologies to improve your browsing experience on our site, analyze site traffic, and understand where our audience is coming from. To find out more, please read our privacy policy.

By choosing 'I Accept', you consent to our use of cookies and other tracking technologies. Less

We use cookies and other tracking technologies... More

Login or register
to apply for this job!

Login or register to start contributing with an article!

Login or register
to see more jobs from this company!

Login or register
to boost this post!

Show some love to the author of this blog by giving their post some rocket fuel 🚀.

Login or register to search for your ideal job!

Login or register to start working on this issue!

Engineers who find a new job through Functional Works average a 15% increase in salary 🚀

Blog hero image

Extending instances on creation for debugging

Anton Fagerberg 16 March, 2018 | 1 min read

A neat trick you can do in Scala is extending an object (instance of an existing class) on the fly when you instantiate it. This comes in very handy when debugging code you do not own yourself.

As an example, I was working with Javas DigestInputStream to calculate a MD5 checksum on the fly from an InputStream. I wanted to know how the MD5 checksum changed during each partial read of the stream, that is when the read method of DigestInputStream is invoked, and invoke my own method with a local variable.

Here’s what the code looked like:

def myMethod(/* parameters */) = { /* Some implementation */ } 
val localVariable = someThing;
val messageDigest = MessageDigest.getInstance("MD5")
val digestInputStream = new DigestInputStream(inputStream, messageDigest)

The implementation for DigestInputStream is not in my code base, meaning I can’t put things in the read method. You can place a break point on it and invoke code on the fly from your IDE, but then you get into the problem of localVariable and myMethod being out of scope.

Here’s where extending the instance on creation comes in handy. It means is that you can override the read method on your custom instance, invoke the local method myMethod using the local variable localVariable in it and then pass everything to super.read to keep the existing functionality intact.

def myMethod(/* parameters */) = { /* Some implementation */ } 
val localVariable = someThing;
val messageDigest = MessageDigest.getInstance("MD5")
val digestInputStream = new DigestInputStream(inputStream, messageDigest) {
  override def read(b: Array[Byte], off: Int, len: Int) = {
    myMethod(localVariable) // Yay!
    super.read(b, off, len)
  }
}

Alternatively, you can put a break point in your overridden read method to get everything in scope and debug with your IDE.

Happy debugging!

Originally published on www.antonfagerberg.com

Author's avatar
Anton Fagerberg
Long time Scala developer, functional programming enthusiast, Øredev program committee
    OCaml
    Elm
    Erlang
    Elixir
    F#
    Scala
    Rust
    Clojure
    play framework
    Haskell

Related Jobs

Related Issues

WorksHub / client
  • Open
  • 0
  • 0
  • Intermediate
  • Clojure
  • $50
WorksHub / client
WorksHub / client
  • Started
  • 0
  • 4
  • Intermediate
  • Clojure
  • $100
WorksHub / client
  • 1
  • 0
  • Intermediate
  • Clojure
WorksHub / client
  • 1
  • 0
  • Intermediate
  • Clojure
WorksHub / client
  • 1
  • 0
  • Intermediate
  • Clojure
WorksHub / client
  • Open
  • 0
  • 0
  • Intermediate
  • Clojure
cosmwasm / wasmd
  • 1
  • 2
  • Intermediate
  • Go
cosmwasm / wasmd
  • Started
  • 0
  • 1
  • Intermediate
  • Go
cosmwasm / wasmd
  • Started
  • 0
  • 1
  • Intermediate
  • Go

Get hired!

Sign up now and apply for roles at companies that interest you.

Engineers who find a new job through Functional Works average a 15% increase in salary.

Get Started with