Search Results

Search found 3 results on 1 pages for 'ebruchez'.

Page 1/1 | 1 

  • Implicit conversion between Scala collection types

    - by ebruchez
    I would like to implicitly convert between the Scala XML Elem object and another representation of an XML element, in my case dom4j Element. I wrote the following implicit conversions: implicit def elemToElement(e: Elem): Element = ... do conversion here ... implicit def elementToElem(e: Element): Elem = ... do conversion here ... So far so good, this works. Now I also need collections of said elements to convert both ways. First, do I absolutely need to write additional conversion methods? Things didn't seem to work if I didn't. I tried to write the following: implicit def elemTToElementT(t: Traversable[Elem]) = t map (elemToElement(_)) implicit def elementTToElemT(t: Traversable[Element]) = t map (elementToElem(_)) This doesn't look too ideal because if the conversion method takes a Traversable, then it also returns a Traversable. If I pass a List, I also get a Traversable out. So I assume the conversion should be parametrized somehow. So what's the standard way of writing these conversions in order to be as generic as possible?

    Read the article

  • Is it possible to use the ScalaTest BDD syntax in a JUnit environment?

    - by ebruchez
    I would like to describe tests in BDD style e.g. with FlatSpec but keep JUnit as a test runner. The ScalaTest Quick Start does not seem to show any example of this: http://www.scalatest.org/getting_started_with_junit_4 I first tried naively to write tests within @Test methods, but that doesn't work and the assertion is never tested: @Test def foobarBDDStyle { "The first name control" must "be valid" in { assert(isValid("name·1")) } // etc. } Is there any way to achieve this? It would be even better if regular tests can be mixed and matched with BDD-style tests.

    Read the article

  • Writing a generic function that can take a Writer as well as an OutputStream

    - by ebruchez
    I wrote a couple of functions that look like this: def myWrite(os: OutputStream) = {} def myWrite(w: Writer) = {} Now both are very similar and I thought I would try to write a single parametrized version of the function. I started with a type with the two methods that are common in the Java OutputStream and Writer: type Writable[T] = { def close() : Unit def write(cbuf: Array[T], off: Int, len: Int): Unit } One issue is that OutputStream writes Byte and Writer writes Char, so I parametrized the type with T. Then I write my function: def myWrite[T, A[T] <: Writable[T]](out: A[T]) = {} and try to use it: val w = new java.io.StringWriter() myWrite(w) Result: <console>:9: error: type mismatch; found : java.io.StringWriter required: ?A[ ?T ] Note that implicit conversions are not applicable because they are ambiguous: both method any2ArrowAssoc in object Predef of type [A](x: A)ArrowAssoc[A] and method any2Ensuring in object Predef of type [A](x: A)Ensuring[A] are possible conversion functions from java.io.StringWriter to ?A[ ?T ] myWrite(w) I tried a few other combinations of types and parameters, to no avail so far. My question is whether there is a way of achieving this at all, and if so how. (Note that the implementation of myWrite will need, internally, to know the type T that parametrizes the write() method, because it needs to create a buffer as in new ArrayT.)

    Read the article

1