Bil*_*ill 26 traits mixins swift
斯威夫特是否有一种混合特征的方式,斯卡拉?关于使用扩展来向现有类添加协议的Swift小册子的部分非常接近.但是,由于协议不能包含实现,因此不能将其用于将代码混合到类中.还有另外一种方法吗?
模拟混合的一种方法是使用通用函数来提供实现
例如,使用这些协议
protocol Named {
func GetName() -> String
}
protocol NamedExtension {
func GetLowercaseName() -> String
func GetUppercaseName() -> String
}
Run Code Online (Sandbox Code Playgroud)
我想要一些类来实现GetName()和使用混合所以他们也得到GetLowercaseName()和 GetUppercaseName()没有实现它们
这是NamedExtension在自由函数中的实现
func GetLowercaseNameImpl<T:Named>(obj:T) -> String {
return obj.GetName().lowercaseString
}
func GetUppercaseNameImpl<T:Named>(obj:T) -> String {
return obj.GetName().uppercaseString
}
Run Code Online (Sandbox Code Playgroud)
和扩展 Int
extension Int : Named {
func GetName() -> String {
return "Int"
}
}
extension Int : NamedExtension {
// use provided implementation
func GetLowercaseName() -> String {
return GetLowercaseNameImpl(self)
}
func GetUppercaseName() -> String {
return GetUppercaseNameImpl(self)
}
}
Run Code Online (Sandbox Code Playgroud)
我可以使用
1.GetName() // result Int
1.GetUppercaseName() // result "INT"
1.GetLowercaseName() // result "int"
Run Code Online (Sandbox Code Playgroud)
我不知道斯卡拉,但你告诉我它是什么,可以同时创建protocol和extension扩展类型添加"伪特征"的行为.
例如:
protocol IsGreaterThan
{
func isGreaterThan(other:Int) -> Bool
func isNotGreaterThan(other:Int) -> Bool
}
extension Int : IsGreaterThan
{
func isGreaterThan(other:Int) -> Bool
{
return self > other
}
func isNotGreaterThan(other:Int) -> Bool
{
return !isGreaterThan(other)
}
}
Run Code Online (Sandbox Code Playgroud)
真正的腿筋是现在如何限制泛型.我认为他们将在即将到来的Swift版本中有所改进.
| 归档时间: |
|
| 查看次数: |
5503 次 |
| 最近记录: |