2017-04-16 71 views
2

在purescript,具有循環依賴記錄可以聲明如下用循環依賴創建記錄?

newtype User = User 
    { name :: String 
    , organization :: Organization 
    } 

    newtype Organization = Organization 
    { name :: String 
    , users :: List User 
    } 

但我還沒有想出如何實際創建purescript實例。在F#中,可以按如下方式創建實例。

我總是得到pam的值是未定義的。也許使用修復功能?

回答

0

我不認爲這是可能做到這一點在PureScript未做結構懶惰的某些部分:

import Data.Lazy (Lazy, defer) 

newtype User = User 
    { name :: String 
    , organization :: Lazy Organization 
    } 

newtype Organization = Organization 
    { name :: String 
    , users :: Array User 
    } 

fed :: Organization 
fed = 
    Organization 
    { name: "Federal Reserve" 
    , users: 
     [ User { name: "Pamela", organization: defer \_ -> fed } 
     , User { name: "Jillian", organization: defer \_ -> fed } 
     ] 
    } 
+0

謝謝!這就是我需要的。 –

+0

很好,不知道你的用例是否可以接受! –