为什么“类型别名”是在另一个模块中以相同方式定义的类型?

Why `type alias` a type that is defined the same way in another module?

例如,来自 Task type in the Task module 的文档:

type alias Task err ok =
  Task err ok

或来源:

type alias Task x a =
  Platform.Task x a

我认为答案是可以从定义为自己的模块中公开它。这是正确的吗?


更新:
Chad Gilbert 提出了一个很好的观点,上面提到了 Platform primitives, such as the Task and ProcessId types, whose constructors are never used there, but it makes sense why these would be grouped at such a central place. Although his answer doesn't explain why they are aliased in their respective modules (see Process.Id 和 Task.Task)。

我想如果没有别名,任何人都试图使用模块 Task and Process would have to import these specific types (i.e., Platform.Task, Platform.ProcessId) because they are not imported by default (see Elm's default imports)。


UPDATE_2:
另一个例子是 Value in the module Json.Decode module:

-- From the docs:
type alias Value = 
    Value

-- From the source:
type alias Value = JsEncode.Value

我认为这证明了我上面的假设,但我不太愿意回答我的问题,因为我是 Elm 的新手,很容易出错。

来自 Platform.Task 上的文档:

Head over to the documentation for the Task module for more information on this. It is only defined here because it is a platform primitive

那里定义为:

type Task err ok = Task

... 这并不能告诉我们太多信息。 Task 是一种不透明类型,它的内部 Task 构造函数从未被使用过。它是 Elm 架构中的基本原语之一,根据上面的评论,似乎只在 Platform 中定义,因为它是一组方便的以平台为中心的原语。

简而言之,我认为当您希望界面的结构不同于您希望组织实施的方式时,您会这样做。

出于多种原因,您可以这样做,例如:

  1. 您希望从多个模块公开相同的类型,因为这样对您的包的使用者来说更有意义。同时你必须有一个单一的实现,因为从工程的角度来看这是有意义的(比如 Json.Encode/Decode.Value)。

  2. 你的类型无论如何都是不透明的(意味着构造函数没有暴露),你只是需要它或者希望它被定义在一个不同于它应该从哪里导入的地方。 我认为 Platform.Task 就是这种情况:显然,Evan 想将所有平台基元组织在一个文件中。但是,从消费者的角度来看,导入 Platform.elm 会很奇怪。感觉就像是向库的使用者公开了实现细节。