Scala:为什么我们需要在包对象中保留类型成员?
Scala: Why do we need to keep type members in the package object?
当我研究Twitter Finatra
(v2.1.1) 资源时,我发现了一个非常奇怪的架构。他们将 type
放入 package object
中。请参阅以下示例:
package object marshalling {
@deprecated("MessageBodyManager is an internal class. Use the HttpMockResponses trait to gain access to a testResponseBuilder", "")
type MessageBodyManager = com.twitter.finatra.http.internal.marshalling.MessageBodyManager
@deprecated("Use com.twitter.finatra.http.marshalling.DefaultMessageBodyReader", "")
type DefaultMessageBodyReader = com.twitter.finatra.http.marshalling.DefaultMessageBodyReader
// ...
}
或者这个
package object filters {
@deprecated("Use com.twitter.finatra.http.filters.AccessLoggingFilter", "")
type AccessLoggingFilter = com.twitter.finatra.http.filters.AccessLoggingFilter[Request]
@deprecated("Use com.twitter.finatra.http.filters.CommonFilters", "")
type CommonFilters = com.twitter.finatra.http.filters.CommonFilters
// ...
}
我不明白这么奇怪的设计的目的是什么。为什么我们需要像示例中那样创建类型别名(类型成员)?只是说 class 已弃用?
这是在 refactoring commit. You can see there that multiple classes have been moved to new packages, and the type aliases and deprecations were added. For example, DefaultMessageBodyReader
was here 中完成的,然后被移入了内部包。在原来的位置保留了别名和弃用。
当我研究Twitter Finatra
(v2.1.1) 资源时,我发现了一个非常奇怪的架构。他们将 type
放入 package object
中。请参阅以下示例:
package object marshalling {
@deprecated("MessageBodyManager is an internal class. Use the HttpMockResponses trait to gain access to a testResponseBuilder", "")
type MessageBodyManager = com.twitter.finatra.http.internal.marshalling.MessageBodyManager
@deprecated("Use com.twitter.finatra.http.marshalling.DefaultMessageBodyReader", "")
type DefaultMessageBodyReader = com.twitter.finatra.http.marshalling.DefaultMessageBodyReader
// ...
}
或者这个
package object filters {
@deprecated("Use com.twitter.finatra.http.filters.AccessLoggingFilter", "")
type AccessLoggingFilter = com.twitter.finatra.http.filters.AccessLoggingFilter[Request]
@deprecated("Use com.twitter.finatra.http.filters.CommonFilters", "")
type CommonFilters = com.twitter.finatra.http.filters.CommonFilters
// ...
}
我不明白这么奇怪的设计的目的是什么。为什么我们需要像示例中那样创建类型别名(类型成员)?只是说 class 已弃用?
这是在 refactoring commit. You can see there that multiple classes have been moved to new packages, and the type aliases and deprecations were added. For example, DefaultMessageBodyReader
was here 中完成的,然后被移入了内部包。在原来的位置保留了别名和弃用。