Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在swift中使用泛型定义类型别名_Swift_Generics_Type Alias - Fatal编程技术网

如何在swift中使用泛型定义类型别名

如何在swift中使用泛型定义类型别名,swift,generics,type-alias,Swift,Generics,Type Alias,我正在研究swift中的谓词转换函数: infix operator ⋀ {} infix operator ∨ {} prefix operator ¬ {} infix operator ⟹ {} func ⋀ <T>(A : T->Bool , B: T->Bool)->T->Bool{ return { x in A(x) && B(x)} } func ∨ <T>(A : T->Bool , B: T-

我正在研究swift中的谓词转换函数:

infix operator ⋀ {}
infix operator ∨ {}
prefix operator ¬ {}
infix operator ⟹ {}


func ⋀ <T>(A : T->Bool , B: T->Bool)->T->Bool{
    return { x in A(x) && B(x)}
}

func ∨ <T>(A : T->Bool , B: T->Bool)->T->Bool{
    return { x in A(x) || B(x)}
}

prefix func ¬ <T>(A : T->Bool)->T->Bool{
    return { x in !A(x)}
}


func ⟹ <T>(A : T->Bool , B: T->Bool)->T->Bool{
    return { x in (¬A ∨ B)(x)}
}

func TRUE<T>()->T->Bool{
    return{x in true}
}

func FALSE<T>()->T->Bool{
    return{x in false}
}

infix operator ∘ {}
func ∘ <X,Y,Z>(f:X->Y, g:Y->Z)->X->Z{
    return{ x in g(f(x))}
}

func PTRANS<X,Y> (f:X->Y)->(Y->Bool)->(X->Bool){
    return {q in (f ∘ q) }
}
我试过:

typealias PRED = <T>(T->Bool)
typealias PRED=(T->Bool)
但我收到以下错误消息:

只有语法类型可以是泛型的

尝试:
typealias PRED=(T->Bool)
在语法上似乎不正确,导致:

TypeAlias声明中应为

如何为
T
上的谓词声明类似于
Array
typealias
typealias PRED=T->Bool


任何想法?

至少在现在, Type Audias<代码>不支持泛型类型别名,如C++ <代码>,使用< /Cord>Trime> < /P> 但是你可以用下面的方法来做

struct Pred<A> {
    typealias t = A -> Bool
}
let gt2: Pred<Int>.t = { n in n > 2 }
struct Pred{
类型别名t=A->Bool
}
设gt2:Pred.t={n in n>2}

您不能这样做。这也可能是重复的。“我觉得以前有人问过我这个问题。”“克劳德,好糖。”。你在github上有这些吗?谢谢你的帮助@findall。我试试这个
struct Pred<A> {
    typealias t = A -> Bool
}
let gt2: Pred<Int>.t = { n in n > 2 }