2017-10-12 99 views
0

我非常新Scala中遇到的奇怪的代码片段:它构造函数重载吗?

class EndpointMapper[A](m: Method, e: Endpoint[A]) extends Endpoint[A] { self => 

    /** 
    * Maps this endpoint to either `A => Output[B]` or `A => Future[Output[B]]`. 
    */ 
    final def apply(mapper: Mapper[A]): Endpoint[mapper.Out] = mapper(self) 

    final def apply(input: Input): Endpoint.Result[A] = 
    if (input.request.method == m) e(input) 
    else EndpointResult.Skipped 

    final override def toString: String = s"${ m.toString.toUpperCase } /${ e.toString }" 
} 

这是一类,这样我就可以创建它的一个实例。 apply怎么样?

我可以使用EndpointMapper(mapper)就像调用apply方法的函数吗?

如何区分两个应用程序或编译器如何知道应该调用哪个apply

回答

2

您不能致电EndPointWrapper(mapper) - 因为apply必须在伴随对象而不是类中定义。你可以做的是呼叫epw(mapper),其中epwEndPointWrapper的一个实例。

如何区别两个应用程序或编译器如何知道,他应该调用哪个应用程序?

它根据参数的类型知道要调用哪个版本的apply,就像其他任何重载方法一样。