Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Sedro
Dec 31, 2008

Lumpy posted:

As someone just starting to look into functional programing, which ones are the "impure" ones? Why are they only "functional-ish"? Why should I avoid them?
If we only included pure languages this would be a haskell thread

Adbot
ADBOT LOVES YOU

Sedro
Dec 31, 2008
For the most Robust, Scalable Enterprise Solution you should use a time service. http://www.timeapi.org/utc/mon?format=%25A

Sedro
Dec 31, 2008

Good Will Hrunting posted:

Is "cake pattern" the only way to mock out something like this? I need to mock out MaxmindDatabases.getConnectionTypeResult in a unit test but you can't mock objects. I guess another option is to create a companion object of the class and access it like that? I'm brand new to Scala so I really can't evaluate tradeoffs of such things too well.

I don't think the cake pattern will help here. If you're new to scala, you're better off pretending the cake pattern doesn't exist.

The solution is the same as other languages:
1. Create a trait (interface) containing the public API
2. Accept an instance of the trait instead of referencing the singleton object
3. Use a mock implementation in your tests

code:
trait DatabaseConnection {
  def getConnectionTypeResult(ipAddress: InetAddress): ConnectionTypeResponse
}

object MaxmindDatabases extends DatabaseConnection {
  lazy val CONNECTION_TYPE_READER: DatabaseReader = initializeDb(ConnectionTypeFile.fileName)

  def getConnectionTypeResult(ipAddress: InetAddress): ConnectionTypeResponse = {
    CONNECTION_TYPE_READER.connectionType(ipAddress)
  }

  def initializeDb(mmdbFile: String): DatabaseReader = {
    //bunch of useless crap
  }
}

class ThingThatUsesDatabase(dbs: DatabaseConnection)

// prod:
new ThingThatUsesDatabase(MaxmindDatabases)

// test:
new ThingThatUsesDatabase(new DatabaseConnection { /* mock stuff */ })

Sedro
Dec 31, 2008

Beamed posted:

Does Twitter use Scala anymore? IIRC they started moving away from it at some point.

They're still using it

  • Locked thread