首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Haskell中表示Java Swing包容层次结构

在Haskell中表示Java Swing包容层次结构
EN

Code Review用户
提问于 2012-05-12 09:55:44
回答 1查看 181关注 0票数 6

Java桌面应用程序通过RPC公开其Swing GUI的类/包含层次结构。我正在构建一个连接到应用程序的Haskell客户端。目标是能够在Haskell端的数据结构上执行诸如“查找所有宽度小于w,位于屏幕矩形r内的按钮”之类的查询。

我不知道如何最好地表示结构,特别是在继承/子类型方面。我试过两种方法。第一项:

代码语言:javascript
运行
复制
data Component = Component
    {
        componentId::Int,
        pos::(Int,Int),
        dim::(Int,Int),
        componentType::ComponentType,
        children::[Component]
    }

data ComponentType =
     ButtonComponent Button
     -- other component types here
    |OtherComponent

data Button = Button
    {
        buttonText::Text
        -- other button attributes here
    }

我还在考虑使用存在型类型的第二解,以及可键入的实现“下传”的方法:

代码语言:javascript
运行
复制
data Component = Component
    {
        componentId::Int,
        pos::(Int,Int),
        dim::(Int,Int),        
        children::[AnyComponent]
    } deriving Typeable

data AnyComponent = forall a . ( Typeable a, HasComponent a ) => AnyComponent a 

class HasComponent a where
    base :: a -> Component
    downcast :: Typeable b => a -> Maybe b

instance HasComponent Component where
    base = id       
    downcast = cast      -- cast function is from Data.Typeable

instance HasComponent AnyComponent where
    base (AnyComponent a) = base a
    downcast (AnyComponent a) = cast a

data Button = Button 
    {
       buttonComponent:: Component,   -- pointer to "superclass" info
       buttonText::Text
       -- other button attributes here
    } deriving Typeable

instance HasComponent Button where
    base = buttonComponent
    downcast = cast 

什么样的解决方案才是更地道的Haskell?有没有更好的解决办法?

EN

回答 1

Code Review用户

回答已采纳

发布于 2013-04-09 13:09:26

编辑:用Haskell忠实地表示OO语义目前是不可能的。特别是,方法解析可以通过类型类和实例来模拟(但不是很容易,也不是很忠实)。这个非常丰富地介绍了当前的最佳实践,以及未来可能的发展。

这里有一种可能,类似于您的第一个解决方案:

代码语言:javascript
运行
复制
data ComponentClass a
    = ComponentClass {
        ...
        , comSubclass :: a
        ...
        }
type Component = ComponentClass ()

data ButtonClass a
    = ButtonClass {
        , buttonText :: Text
        , btSubclass :: a
        ...
        }
type Button = ComponentClass (ButtonClass ())

如果我认为一个简单的层次很适合我的问题域(但不一定要在另一种语言中反映OO ),我就会这么做。但是,如果需要跟踪深度层次结构或多个超类(如Python),则此策略不能很好地工作。

另一个缺点是,您不能将任意ComponentClass a解构为它的子类型a。只有事先知道a的类型,才能做到这一点。但是,您可以使用动态类型来解决这个问题,例如通过Typable

请注意,您还可以在向上的方向表示等级:

代码语言:javascript
运行
复制
data ButtonClass a
    = ButtonClass {
          btText :: Text
        , btSuperClass :: a
        }
type Button = ButtonClass ComponentClass

然后,您可以“向下类层次结构”构建每个对象,直到您发现最具体的类型来封装它。

还请注意,Gtk2Hs使用类似于第二种策略的类型化策略来表示Gtk的类层次。也许看一看它的源代码会很有说服力,甚至只是浏览它的文件上的黑客。

最后,用Exception实现的GHC等级非常有趣,尽管我不认为它对你的帮助不如上面的任何一个

票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/11715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档