2017-01-15 1 views
0

무형의`Last` 유형 클래스를 탐색, 더 많거나 적은, shapelessLastLast 형 클래스 : 다음내가 밖으로 입력

import shapeless.{HList, HNil, ::} 

trait Last[H <: HList] { 
    type Out 
    def last(in: H): Out 
} 

, 나는, 내가 이해, 유형 클래스 인스턴스의 개 입력 LastHList을 위해 : 그러나

object Last { 
    type Aux[L <: HList, O] = Last[L] { type Out = O } 

    // arrived at the truly `last` item, i.e. `H` 
    implicit def singleLast[H]: Aux[H :: HNil, H] = new Last[H :: HNil] { 
    override type Out = H 
    override def last(in: H :: HNil): H = in.head 
    } 

    // I believe this is the inductive step 
    implicit def hlistLast[H, T <: HList, OutT] 
    (implicit lt : Last.Aux[T, OutT]): Aux[H :: T, OutT] = 
    new Last[H :: T] { 
     type Out = OutT 
     def apply(l : H :: T): Out = lt(l.tail) 
    } 
} 

가 컴파일에 실패 왜 이해가 안 :

[error] /Users/kevinmeredith/Workspace/shapeless-sandbox/src/ 
     main/scala/net/ops.scala:17: net.Last.Aux[T,OutT] does not take parameters 
[error]  def apply(l : H :: T): Out = lt(l.tail) 
[error]         ^
[error] one error found 
[error] (compile:compileIncremental) Compilation failed 

어떻게이 컴파일 타임 오류를 해결할 수 있습니까? 당신은 lastapply을 변경

trait Last[H <: HList] { 
    type Out 
    def apply(in: H): Out 
} 

하지만 hlistLast에 당신은 여전히 ​​(그것을 정의하고 lt에 그것을 사용하여 두) apply를 사용하려고 : 같은

답변

2

Last의 실제 무형의 구현은 보이는

def apply(l : H :: T): Out = lt(l.tail) 

존재하지 않을 때 lt.apply을 사용하려고하면 컴파일러 오류가 발생합니다. 컴파일러가 처음으로 last이 구현되지 않은 채로 남았다 고 말한 경우이 경우 더 유용했을 것입니다.

+0

흠. 어떻게 해결할 수 있습니까? –

+1

'hlistLast'에서'apply'를'last'로 이름을 바꾸거나'/ lastL '에서'apply'로 이름을 바꿉니다. –