gpt4 book ai didi

associations - Elm 中的建模关联导致依赖循环

转载 作者:行者123 更新时间:2023-12-05 00:49:47 24 4
gpt4 key购买 nike

我的数据库由一个项目表和一个相关广告表组成。

我希望我的 elm 应用程序显示一个带有相关广告的项目,或者一个带有父项目信息的广告。

为了满足这些接口(interface)需求,我希望在 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

看起来像定义 type alias在同一个模块中不会使编译器停止,但这对我来说不是一个令人满意的解决方案,因为我不希望每个 Model我的应用程序最终出现在同一个文件中。

有没有办法在不定义两个类型别名 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)
|> ...

这种方法很有效,也为随后的内存提供了机会

关于associations - Elm 中的建模关联导致依赖循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46873682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com