英文:
Generic function specialization for a concrete type
问题
在这个示例中,我正在尝试创建一些颜色转换工具:我希望确保每个实现了ColorSpace
特质的类型都可以轻松地转换为任何其他类型。为了实现这一点,我要求显式实现函数from_xyz
和into_xyz
(因为XYZ
是色度学中使用的标准颜色空间),以便所有其他转换(from_color<T>
和into_color<T>
)都可以使用XYZ
作为中间步骤。在某些情况下,这是多余的,更具体的实现是可取的:在颜色空间和XYZ
之间进行转换,或者例如在RGB
和HSL
之间进行直接从彼此派生的转换。
有没有一种方法可以为具体类型专门实现通用函数?
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论