加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

关联 – Elm中的建模关联导致依赖关系循环

发布时间:2020-12-14 04:54:37 所属栏目:百科 来源:网络整理
导读:我的数据库包含一个项目表和一个关联广告表. 我希望我的elm应用程序显示包含其关联广告的项目,或者显示包含父项目信息的广告. 为了满足这些接口需求,我本来希望在elm中编写以下两个模块,匹配我的API已发送的内容: module Item.Model exposing (..)import Ad
我的数据库包含一个项目表和一个关联广告表.

我希望我的elm应用程序显示包含其关联广告的项目,或者显示包含父项目信息的广告.

为了满足这些接口需求,我本来希望在elm中编写以下两个模块,匹配我的API已发送的内容:

module Item.Model exposing (..)

import Ad.Model exposing (Ad)

type alias Item =
   { ads : Maybe List Ad
   }

module Ad.Model exposing (..)

import Item.Model exposing (Item)

type alias Ad =
   { item : Maybe Item
   }

但是,此定义会导致以下依赖项循环错误:

ERROR in ./elm-admin/Main.elm
Module build failed: Error: Compiler process exited with error Compilation failed
Your dependencies form a cycle:

  ┌─────┐
  │    Item.Model
  │     ↓
  │    Ad.Model
  └─────┘

You may need to move some values to a new module to get rid of the cycle.

    at ChildProcess.<anonymous> (/opt/app/assets/node_modules/node-elm-compiler/index.js:141:27)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at maybeClose (internal/child_process.js:886:16)
    at Socket.<anonymous> (internal/child_process.js:342:11)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at Pipe._handle.close [as _onclose] (net.js:497:12)
 @ ./js/admin.js 3:12-44

看起来在同一模块中定义两个类型别名并不会使编译器停止,但这对我来说不是一个令人满意的解决方案,因为我不希望我的应用程序的每个模型都在同一个文件中.

有没有办法在没有在同一模块中定义两个类型别名Ad和Item的情况下解决这个问题?

解决方法

我无法想到你提出的问题的直接解决方案,但我不会以这种方式存储数据 – 我会使用指针代替

module Item.Model exposing (..)

type alias Items = Dict String Item

type alias Item =
   { ads : List String
   }
   -- you don't need a Maybe and a List 
   -- as the absence of data can be conveyed by the empty list

module Ad.Model exposing (..)

import Item.Model exposing (Item)

type alias Ads = Dict String Ad 

type alias Ad =
   { itemKey : Maybe String
   }

然后你就可以这样了

model.ads 
    |> L.filterMap (key -> Dict.get key model.items)
    |> ...

model.itemKey
    |> Maybe.andThen (itemKey -> Dict.get itemKey model.ads)
    |> ...

这种方法有效,并且随后也提供了备忘的机会

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读