为什么 PureScript 的 Prelude 中的 Unit {} 在 JavaScript 中?

Why is Unit in PureScript's Prelude {} in JavaScript?

我是 FP 和类型级编程的初学者。 最近学习了VoidUnit

Prelude 的 unit 在 JavaScript 中定义为 {}

"use strict";

exports.unit = {};

我的问题是"Why not null but {}?" 也许这是一个微不足道的问题,但我想了解它的哲学。

根据我的理解,unit对应JavaScript中的null。 例如,我可以在 JavaScript.

中调用不带参数的函数
// hello :: Void -> String
function hello () {
  return "hello"
}

const h1 = hello() // "hello"
// However, I do not have any members of `Void` in PureScript, so I cannot call like above.

如果我必须指定 hello 函数的一些参数,我选择 null 而不是 {}

// hello :: forall a. a -> String
function hello (a) {
  return "hello"
}

// 'hello :: Unit -> String'-like
const h1 = hello(null) // "hello"

// undefined also works, but weird to me
const h2 = hello(undefined)

// also works, but weird
const h3 = hello(42)
const h4 = hello({})
const h5 = hello([])

如果 unit 代表副作用,可能是 undefined 或什么 null

// 'log1 :: String -> Effect Void'-like
function log1 (s) {
  return s => () => console.log(s) // console.log return undefined
}
// however, function must return a value


// 'log2 :: String -> Effect Unit'-like
function log2 (s) {
  return s => () => {
    console.log(s) // side-effect
    return null
  }
}

// foreign Effect.Console.log (ECMAScript-style)
function log3 (s) {
  return s => () => {
    console.log(s)
    return {} // weird to me; it seems like 'return 42'
  }
}

我是不是漏掉了什么?

您为 Unit 使用什么值实际上并不重要。 {} 是一个相当随意的选择 - undefinednull,或者只是不返回值都可以,如果您在 FFI 中编写一些东西。由于 Unit 应该只有一个居民,因此从来没有检查它的实际运行时值的时间。

自从选择 {} 以来已经有很长一段时间了 - 这可能是一个历史事故,是所有非 Prim PS 值被构造为时遗留下来的匿名对象。