首页 > 解决方案 > 给定一个类型,如何找到我需要实现的所有功能

问题描述

我正在阅读 Christopher Rackauckas 的优秀 Julia 博客Type-Dispatch Design: Post Object-Oriented Programming for Julia

在其中,他描述了“在面向对象的编程语言中,您如何按实现细节分组,在类型调度编程中,您如何按操作分组”。例如,在博客中有一个带有 AbstractPerson 的代码示例,它被描述为“一个人是有一个可以通过 get_name 获取的名字的人”。

所以,如果我正在使用一些使用某种类型的库,并且我想自己实现该类型,我如何列出我需要实现的所有功能?例如,如果我想创建自己的 Person 类型,我如何发现需要正确处理 get_name 函数?

Julia 是否会跟踪这些信息,如果是,我如何访问它?

标签: typesjulia

解决方案


What you are looking for is typically called an interface. Currently, interfaces are only informal in Julia. They are usually specified in the documentation of a package or Julia itself. For example, see here the interface definition for AbstractArray. Hence there is no built in function that lists all the mandatory methods that you must implement. However, interfaces might become first class in future Julia versions.

To answer your title question, you might want to take a look at methodswith(type). Note, though, that this won't give you all methods that can be called with an object of the given type (take a look at the optional keyword argument supertypes::Bool.


推荐阅读