F# 在类型别名中重用类型别名
所以前几天当我看到F# 在类型别名中重用类型别名,f#,F#,所以前几天当我看到 type Foo<'a> = abstract foo : Unit -> 'a type IsFoo<'a,'b when 'a :> Foo<'b>> = 'a 然后,我可以使用FooThenBar作为缩写,表示存在将“foo”映射到“bar”和将“bar”映射到我指定的任何类型的函数 (我不是Haskell专家,必须得到帮助才能创建这个松散等效的场景) 这是原始函数 > f :: (Bar bar b, F
type Foo<'a> =
abstract foo : Unit -> 'a
type IsFoo<'a,'b when 'a :> Foo<'b>> = 'a
然后,我可以使用FooThenBar作为缩写,表示存在将“foo”映射到“bar”和将“bar”映射到我指定的任何类型的函数
(我不是Haskell专家,必须得到帮助才能创建这个松散等效的场景)
这是原始函数
> f :: (Bar bar b, Foo foo bar) => foo -> b
> f x = bar (foo x)
这使用了新的推断成分
> f' :: (FooThenBar foo bar b) => foo -> b
> f' x = bar (foo x)
如果你不知道,
X>
可以简化为
(如果你不需要'a
的句柄),那么也值得指出。所以,如果你有let f(x:IsFoo)=…
你可以写let f(x:IsFoo)=…
。而且让f(x:IsFooThenBar)=…
可以是让f(x:Foo)=…
看:我的例子很琐碎…所以我知道我可以做一个完整的陈述,关键是如果我有可以承载原子约束的别名,我可以把它们组合成更复杂的别名吗…而不需要重复它们如果你不知道,X>
可以简化为
(如果你不需要'a
的句柄),那么也值得指出。所以,如果你有let f(x:IsFoo)=…
你可以写let f(x:IsFoo)=…
。而且让f(x:IsFooThenBar)=…
可以是让f(x:#Foo)=…
看:我的例子很琐碎…所以我知道我可以做一个完整的陈述,关键是如果有别名可以承载原子约束,我可以把它们组合成更复杂的别名吗…而不需要重复它们
type IsFooThenBar<'a,'b,'c when 'a :> Foo<'b> and 'b :> Bar<'c>> =
IsFoo<'a,IsBar<'b,'c>>
type IsFooThenBar2<'a,'b,'c> = IsFoo<'a,IsBar<'b,'c>>
let f (x : IsFoo<_,IsBar<_,string>>) =
x.foo().bar()
let f2 (x : IsFooThenBar<_,_,string>) =
x.foo().bar()
> class Foo a b | a -> b where
> foo :: a -> b
>
> class Bar a b | a -> b where
> bar :: a -> b
> class (Foo foo bar,Bar bar a) => FooThenBar foo bar a where
> f :: (Bar bar b, Foo foo bar) => foo -> b
> f x = bar (foo x)
> f' :: (FooThenBar foo bar b) => foo -> b
> f' x = bar (foo x)