具体类型的通用函数特化

huangapple go评论106阅读模式
英文:

Generic function specialization for a concrete type

问题

这个示例中,我正在尝试创建一些颜色转换工具:我希望确保每个实现了ColorSpace特质的类型都可以轻松地转换为任何其他类型。为了实现这一点,我要求显式实现函数from_xyzinto_xyz(因为XYZ是色度学中使用的标准颜色空间),以便所有其他转换(from_color<T>into_color<T>)都可以使用XYZ作为中间步骤。在某些情况下,这是多余的,更具体的实现是可取的:在颜色空间和XYZ之间进行转换,或者例如在RGBHSL之间进行直接从彼此派生的转换。
有没有一种方法可以为具体类型专门实现通用函数?

英文:

In this example I'm trying to make some color conversion utils: I'd like to guarantee that every type that implements the ColorSpace trait can be easily converted to any other such type. To achieve this I ask to implement explicitly the functions from_xyz and into_xyz (since XYZ is the standard color space used in colorimetry) so that all other conversions (from_color<T> and into_color<T>) could use XYZ as an intermadiate step. In some cases this is redundant and more specific implementations are desirable: converting between a color space and XYZ or, for example, converting between RGB and HSL that derive directly from each other.
Is there a way to specialize a generic function implementation for a concrete type?

答案1

得分: 1

你正在寻找专门化

在撰写本文时,它不稳定,仅在夜间版本中可用,并且存在多个问题,因此不建议在其当前形式下使用。考虑到问题已经存在的时间,它不太可能在短期内稳定下来。

英文:

You're looking for specialization.

At the time of writing, it's not stable, only available on nightly, and has multiple issues, so it's inadvisable to use it in its current form. Given the amount of time the issue has been open, it's unlikely to be stabilized anytime soon.

huangapple
  • 本文由 发表于 2023年8月9日 18:31:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76866889.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定