저는 스칼라로 제네릭 프로그래밍을하고 싶습니다. 아래 코드에서 설명한대로 CC
클래스의 인스턴스를 만드는 방법을 알아 내려고 노력하고 있습니다. 나는 정의 된 인터페이스를 사용하는 자신 만의 특별한 OrderBook
클래스를 생성하고자하는 사용자를 강제하기 위해 다음 동반자 개체에서이 특성의 숨겨진 구현을 ...스칼라에서 제네릭 형식의 인스턴스를 만드시겠습니까?
/** Trait defining the interface for an `OrderBook`.
*
* @tparam O type of `Order` stored in the order book.
* @tparam CC type of collection used to store `Order` instances.
*/
trait OrderBook[O <: Order, CC <: collection.GenMap[UUID, O]] {
/** All `Orders` contained in an `OrderBook` should be for the same `Tradable`. */
def tradable: Tradable
/** Add an `Order` to the `OrderBook`.
*
* @param order the `Order` that should be added to the `OrderBook`.
*/
def add(order: O): Unit
/** Filter the `OrderBook` and return those `Order` instances satisfying the given predicate.
*
* @param p predicate defining desirable `Order` characteristics.
* @return collection of `Order` instances satisfying the given predicate.
*/
def filter(p: (O) => Boolean): Option[collection.GenIterable[O]] = {
val filteredOrders = existingOrders.filter { case (_, order) => p(order) }
if (filteredOrders.nonEmpty) Some(filteredOrders.values) else None
}
/** Find the first `Order` in the `OrderBook` that satisfies the given predicate.
*
* @param p predicate defining desirable `Order` characteristics.
* @return `None` if no `Order` in the `OrderBook` satisfies the predicate; `Some(order)` otherwise.
*/
def find(p: (O) => Boolean): Option[O] = existingOrders.find { case (_, order) => p(order) } match {
case Some((_, order)) => Some(order)
case None => None
}
/** Return the head `Order` of the `OrderBook`.
*
* @return `None` if the `OrderBook` is empty; `Some(order)` otherwise.
*/
def headOption: Option[O] = existingOrders.values.headOption
/** Remove and return the head `Order` of the `OrderBook`.
*
* @return `None` if the `OrderBook` is empty; `Some(order)` otherwise.
*/
def remove(): Option[O] = {
headOption match {
case Some(order) => remove(order.uuid)
case None => None
}
}
/** Remove and return an existing `Order` from the `OrderBook`.
*
* @param uuid the `UUID` for the order that should be removed from the `OrderBook`.
* @return `None` if the `uuid` is not found in the `OrderBook`; `Some(order)` otherwise.
*/
def remove(uuid: UUID): Option[O]
/* Underlying collection of `Order` instances. */
protected def existingOrders: CC
}
을 다음과 같은 추상적 인 특성을 정의 ... 그리고 한 특성에서 구체적인 구현에서 하위 클래스로 분류하기보다는. 여기에 동반자 개체 ... 내가 MutableOrderBook
내 구현 유형 CC
의 빈 인스턴스를 생성하는 방법을 알아낼 싶습니다
object OrderBook {
import scala.collection.mutable
import scala.collection.parallel
def apply[O <: Order, CC <: mutable.Map[UUID, O]](tradable: Tradable): OrderBook[O, CC] = {
new MutableOrderBook[O, CC](tradable)
}
def apply[O <: Order, CC <: parallel.mutable.ParMap[UUID, O]](tradable: Tradable): OrderBook[O, CC] = {
new ParallelMutableOrderBook[O, CC](tradable)
}
private class MutableOrderBook[O <: Order, CC <: mutable.Map[UUID, O]](val tradable: Tradable)
extends OrderBook[O, CC] {
/** Add an `Order` to the `OrderBook`.
*
* @param order the `Order` that should be added to the `OrderBook`.
*/
def add(order: O): Unit = {
require(order.tradable == tradable) // can be disabled by compiler?
existingOrders(order.uuid) = order
}
/** Remove and return an existing `Order` from the `OrderBook`.
*
* @param uuid the `UUID` for the order that should be removed from the `OrderBook`.
* @return `None` if the `uuid` is not found in the `OrderBook`; `Some(order)` otherwise.
*/
def remove(uuid: UUID): Option[O] = existingOrders.remove(uuid)
/* Underlying collection of `Order` instances. */
protected val existingOrders: CC = ??? // I want this to be an empty instance of type CC!
}
private class ParallelMutableOrderBook[O <: Order, CC <: parallel.mutable.ParMap[UUID, O]](val tradable: Tradable)
extends OrderBook[O, CC] {
/// details omitted for brevity!
}
}
이다. 이것이 반성없이 이루어질 수 있기를 희망합니다. 리플렉션이 필요한 경우이 사용 사례에 리플렉션을 사용하지 않는 방법에 대한 제안을 열어 두겠습니다. 생각?
특성에'CC <: collection.GenMap [UUID, O]]'유형이 있고 오브젝트에'CC <: mutable.Map [UUID, O]'가있는 이유는 무엇입니까? – Samar
@samer 컴패니언 개체가 팩토리가되도록 apply 메서드를 오버로드하고 싶습니다. 유형 범위는 내가 오버로드 할 계획입니다. 나는 이것을 보여주기 위해 객체를 업데이트했습니다 ... – davidrpugh