Trait/Object

com.swoval.reflect

Duck

Related Docs: object Duck | package reflect

Permalink

trait Duck[T, D] extends Any

Converts an instance of type T into an instance of a trait of type D using the structural information of D and T to extract the methods of D from T.

Example:

import com.swoval.reflect.Duck
def using[T, R](t: => T)(f: T => R)(implicit duck: Duck[T, AutoCloseable]) = {
  val instance = t
  try f(instance) finally duck(instance).close()
}
class Foo {
  def doSomeWork(): Unit = println("Doing work!")
  def close(): Unit = println("Closing foo.")
}
using(new Foo)(f => f.doSomeWork()) // prints "Doing work!\nClosing foo."
T

the generic type that is being converted to D

D

A generic type that must represent a trait (or java interface)

Linear Supertypes
Any
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Duck
  2. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Abstract Value Members

  1. abstract def apply(t: T): D

    Permalink

    Duck types an instance of T to a trait D if T structurally implements D.

  2. abstract def getClass(): Class[_]

    Permalink
    Definition Classes
    Any

Concrete Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    Any
  6. def hashCode(): Int

    Permalink
    Definition Classes
    Any
  7. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  8. def toString(): String

    Permalink
    Definition Classes
    Any

Inherited from Any

Ungrouped