我一直在尝试弄清楚如何用wxHaskell调整staticText元素的大小以适应它们的内容。据我所知,这是wxWidgets中的默认行为,但wxHaskell包装器特别禁用了此行为。然而,创建新元素的库代码让我非常困惑。有人能解释一下这段代码是做什么的吗?
staticText :: Window a -> [Prop (StaticText ())] -> IO (StaticText ())
staticText parent props
= feed2 props 0 $
initialWindow $ \id rect ->
initialText $ \txt -> \props flags ->
do t <- staticTextCreate parent id txt rect flags {- (wxALIGN_LEFT + wxST_NO_AUTORESIZE) -}
set t props
return t我知道feed2 x y f = f x y,而且initialWindow的类型签名是
initialWindow :: (Id -> Rect -> [Prop (Window w)] -> Style -> a) -> [Prop (Window w)] -> Style -> ainitialText的签名是
initialText :: Textual w => (String -> [Prop w] -> a) -> [Prop w] -> a但我就是不能接受所有的lambdas。
发布于 2012-11-18 06:48:00
在Haskell中,一切都是嵌套的\txt props flags -> do {...}!\txt -> \props flags -> do {...}和lambda一样,实际上都是
\txt -> \props -> \flags -> do {...}这里有一点令人困惑的是,\txt props flags似乎对许多论点都有影响:
initialText :: ...=> (String -> [Prop w] -> a) -> ...似乎有一个双参数函数,我们给它一个三参数的lambda。但请记住:实际上,每个函数只有一个参数,其他的都是通过来完成的。在本例中,a又是一个函数类型,因此它实际上应该读作
initialText :: Textual w => (String -> [Prop w] -> b -> c) -> [Prop w] -> b -> c这并不是乐趣的结束。initialWindow的参数似乎必须有很少的参数,2而不是4,但事实并非如此:我们只给了initialWindow第一个参数,结果是一个函数接受更多的参数,首先是[Prop w]参数(在本例中,是initialWindow签名中的[Prop(Window w)] ),然后它返回a,我们将其重写为b->c;在这种情况下,initialWindow需要的是Style -> a。
因此,此应用程序中initialText的实际签名为
(String -> [Prop(Window w)] -> Style -> c) -> [Prop w] -> Style -> c发布于 2012-11-18 07:38:15
我没有使用过的WX库,似乎在内部使用了一种奇怪的回调或延续传递方式。这是一个令人困惑的影子props,让我重新命名这个笨蛋:
staticText1 :: Window a -> [Prop (StaticText ())] -> IO (StaticText ())
staticText1 parent propsTopLevel
= feed2 propsTopLevel 0 $
initialWindow $ \id rect ->
initialText $ \txt -> \propsParam flags ->
do t <- staticTextCreate parent id txt rect flags
set t propsParam
return t如果没有($),我可以使用括号:
staticText2 ::Window a ->属性(StaticText ()) -> IO (StaticText ()) staticText2 parent propsTopLevel = feed2 propsTopLevel 0 (initialWindow (\id rect -> initialText (\txt -> \ props参数标志-> do t <- staticTextCreate parent id txt rect标志集t属性返回t)
\text -> \props flags ->之类的lambda可以命名为:
staticText3 :: Window a -> [Prop (StaticText ())] -> IO (StaticText ())
staticText3 parent propsTopLevel = initialWindow myWindow propsTopLevel 0
where makeWindow id rect = initialText myText
where myText txt propsParam flags = do
t <- staticTextCreate parent id txt rect flags
set t propsParam
return t在staticText3中,我使用嵌套词法作用域作为参数名称。让我说得更清楚一点:
staticText4 :: Window a -> [Prop (StaticText ())] -> IO (StaticText ())
staticText4 = makeWindowTextStatic where
makeWindowTextStatic parent propsTopLevel = initialWindow (makeTextStatic parent) propsTopLevel 0
makeTextStatic parent id rect = initialText (makeStatic parent id rect)
makeStatic parent id rect txt propsParam flags = do
t <- staticTextCreate parent id txt rect flags
set t propsParam
return t这是否足够清晰,可以跟上流程?我还没有试图理解initialWindow和initialText本身。
https://stackoverflow.com/questions/13435417
复制相似问题