The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Again About Traits in Scala

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Alexey Frishman

Posts: 1
Nickname: nucleo
Registered: Jul, 2010

Again About Traits in Scala Posted: Jul 21, 2010 11:45 AM
Reply to this message Reply
Advertisement
Seeing all these good words about traits I still cannot understand, what is it all about. I see many people say traits solve Composition vs. Inheritance problem, but again, I don't see the point... Can somebody explain me on the next example?

Let's say we have

trait Printing {
def print () {
// connect to printer
// upload document to printer
// ...
}
}

trait DatabasePersistable {
def save () {
// connect to database
// execute SQL query
// ...
}
}

class Document extends DatabasePersistable with Printing {
def open() {...}
def Pages getPages() {...}
...
}


And now I want to write a true unit test for the Document class (you know, without those heavy dependencies on the outside environment and resources). I need to cut off database and printing technologies dependencies for this purpose to test solely methods of the Document class. In Java I would use composition and just inject fake, mock, stub or something else instead. How do I do this in this Scala example?

P.S. Please, do not see this as critics of Java, Scala or inheritance, I just want to understand how this works.

Topic: Page numbers corrupted in Version 11 Previous Topic   Next Topic Topic: error: value scalatest is not a member of package org

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use