Does Haskell support object oriented programming

后端 未结 5 640
栀梦
栀梦 2020-12-13 00:03

Does it support concepts like separation of declaration and implementation (interfaces and classes in Java)?

How about restricting access (like access modifiers in J

5条回答
  •  眼角桃花
    2020-12-13 01:03

    I just discovered sth by accident, see https://github.com/complyue/ooh for a runnable program with full machinery (full code is still extremely short though).

    I never imagined OO stylish program can be written in Haskel in such a way so straight forward!

    Constructing object:

    !o <- classC $^ (777, "hahah")
    

    Calling direct methods:

      cx0 <- o $. getNumC $ ()
      o $. setNumC $ 888
    

    Calling base methods:

      bx0 <- (o $.. cast'C'as'B) getNumB ()
      (o $.. cast'C'as'B) setNumB 999
    

    Method arguments have to be uncurried as prototyped here, I suppose it's the preferable flavor when writing OO style code though.

    While the object class definition appears verbose as hand-crafted for now, I believe Template Haskell has much to offer for better syntax sugar.

提交回复
热议问题