V2EX  ›  英汉词典

Typeclass

释义 Definition

typeclass(也常写作 type class)指一种编程语言(尤其是 Haskell、PureScript 等函数式语言)中的机制:用来定义一组“类型必须实现的行为/接口”(如某些函数或运算),并允许不同类型通过“实例(instance)”来提供各自的实现,从而实现多态与代码复用。(在其他语境中也可能泛指“类型的分类/类”,但最常见的是函数式编程里的这个概念。)

发音 Pronunciation

/ˈtaɪpˌklæs/

例句 Examples

Typeclasses let you write one function that works for many types.
类型类让你能写一个函数,却适用于多种类型。

By defining a Show instance for a custom data type, the typeclass system enables consistent string formatting across the program while keeping the formatting rules separate from the core logic.
通过为自定义数据类型定义一个 Show 实例,类型类系统可以在整个程序中提供一致的字符串格式化,同时把格式化规则与核心逻辑分离开来。

词源 Etymology

typeclasstype(类型) + class(类/分类) 组合而来。该术语在 Haskell 早期发展中被系统化使用,用来表示“按能力/行为对类型进行分组”的抽象机制:同一个 typeclass 表示一组操作的契约,不同类型通过 instance 来满足这个契约。

相关词 Related Words

文学/作品中的用例 Works

  • Haskell 98 Language and Libraries: The Revised Report(Haskell 语言报告中系统描述 type classes 及其语义)
  • Programming in Haskell — Graham Hutton(以教学方式讲解 typeclass、实例与常见类如 EqOrdShow
  • Learn You a Haskell for Great Good! — Miran Lipovača(通俗介绍 typeclass 与函数式编程模式)
  • Real World Haskell — Bryan O'Sullivan, Don Stewart, John Goerzen(在工程实践中大量使用并讨论 typeclass)
关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   946 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 10ms · UTC 17:32 · PVG 01:32 · LAX 09:32 · JFK 12:32
♥ Do have faith in what you're doing.