如何让模块消费者定义ELM中的类型



我已经在我的榆树项目中创建了一个表模块。

每列都有一个getter函数,该功能定义了它们如何从对行建模的记录中检索数据。

因此,一排看起来像:

{ name = "bananas"
, price = 5
}

,这些列看起来像这样:

[ { title = "Item Name"
  , get = .name
  }
, { title = "Price in pennies"
  , get = .price >> (*) 100 >> toString
  }
]

这意味着列get函数的类型注释是: Row -> String

我遇到的问题是,我想使该表模块成为具有自己的"行"类型的其他项目的可重复使用组件。我如何允许模块的消费者指定行类型别名而不删除所有类型注释?

我是Elm的新手

而不是使用自己的类型Row,您可以引入一个类型变量(通常a,但任何低案例名称都可以使用,例如,Elm-Sortable the in eLM-Sortable中的data

您可以创建类似的东西:

type alias Column a =
    { get : a -> String
    , title : String
    }
type alias Columns a =
    List (Column a)
viewTable : Columns a -> List a -> Html msg

最新更新