Scala object that extends Java class -



Scala object that extends Java class -

an old trick used in previous java projects create e.g. fileutils class offered helper functions mutual file operations needed project , not covered e.g. org.apache.commons.io.fileutils. hence custom fileutils extend org.apache.commons.io.fileutils , offer functions well.

now seek same in scala apache helper functions not seen through fileutils scala object, wrong here?

import org.apache.commons.io.{ fileutils => apachefileutils } object fileutils extends apachefileutils { // ... additional helper methods } val content = fileutils.readfiletostring(new file("/tmp/whatever.txt"))

here compiler complains readfiletostring not fellow member of scala fileutils of apachefileutils , extend ...

the scala equivalent of class static methods object, in scala terms, static components of fileutils seen

object fileutils { def readfile(s:string) = ??? ... }

and in scala, can't extend object. illegal:

object object b extends // not type

therefore object fileutils extends apachefileutils gives access class-level definitions of apachefileutils (that except base of operations object methods equals , hashcode, have none)

you might find scala offers more elegant ways of providing extensions. have @ 'pimp library' pattern starting point.

to apply pattern example:

// definition of "pimped" methods import java.io.file class richfile(file:file) { def readtostring():string = ??? } // companion object defines implicit conversion object richfile { implicit def filetorichfile(f:file):richfile = new richfile(f) }

// usage

import richfile._ val content = new file("/tmp/whatever.txt").readtostring

scala

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

Set Up Of Common Name Of SSL Certificate To Protect Plesk Panel -