仅输入接口

时间:2018-07-18 19:17:25

标签: programming-languages dsl

有人碰巧知道某个程序概念是否以某种语言退出,该语言只允许构建仅指定满足其实现所需的数据类型的接口?

换句话说,实现它的对象/类的命名约定是不相关的。成功的实现取决于属性类型是否满足接口的类型定义要求。

例如,假设我有一个程序创建了一堆类型别名。我可能希望这样做:

# Revenue of the transaction
type Revenue = Float

# Id of the Transaction
type TransactionId = String

interface Transaction {
  Revenue
  TransactionId
}

# This compiles...
type MyCustomTransaction implements Transaction {
   saleAmount: Revenue
   id: TransactionId
   myCustomProperty: Boolean
}

1 个答案:

答案 0 :(得分:1)

您所描述的是协议或抽象类的不太有用的版本,它们定义了所需的接口。通过仅定义类型而不要求定义名称,该接口是无用的。

这样想吧。我告诉您,某些对象符合pusher = pusherclient.Pusher(appkey, daemon=False) pusher.connection.bind('pusher:connection_established', connect_handler) pusher.connect() # No need for a while True as the underlying non-daemon thread will prevent the process # from dying automatically. 接口。您仅知道该对象包含一个TransactionRevenue的属性。但是,您不知道如何访问它们以及它们的含义。那你甚至可以使用该界面吗?

有一个类似于您所说的类型化元组的想法,这是元素组,不一定要像类型(Int,Int,String)那样命名为(5,5,“ Hello”)。但是,由于元组是有序的,因此知道类型会提供一个“隐式”接口,因为您知道其中包含的元素的数量和类型。

如果您有兴趣,可以在这里阅读有关协议和抽象类的更多信息: https://en.wikipedia.org/wiki/Abstract_type

https://en.wikipedia.org/wiki/Concept_(generic_programming)

https://en.wikipedia.org/wiki/Protocol_(object-oriented_programming)