java - Is there a language that supports implicit composite types at variable declaration?
问题描述
This question is more general and extends to typed non-OOP languages, but just to have code to demonstrate my intention I'm going to stick to a standard curly-brace language like C#.
The OOP languages I've seen support both composite interfaces
interface ISyntaxNode : ISyntax, INode {}
ISyntaxNode node;
// Do stuff with node
And also some notion of composite class
class SyntaxNode {
// Members are public/private/partially exposed/whatever
ISyntax code;
INode node;
}
SyntaxNode node;
// Do stuff with node
But in both cases one needs to explicitly define the composite type and how it might behave before using it. Are there any languages that support some notion of implicit composite typing? E.g.
ISyntax INode node;
This would be a bit more useful/flexible in a language with a notion of implicit interfaces so that you could later define something like ISyntaxNode
and still have node
be one of those without any changes to the code, but I'm curious if that's a pattern anyone has seen in the wild. My google-foo has come up a little short trying to answer this question.
解决方案
Scala的复合类型与您所描述的非常相似。复合类型允许在声明变量/参数时枚举类型
val myVal: Type1 with Type2 with TypeN = new Type1 with Type2 with TypeN
推荐阅读
- sql - 在 bigQuery 中迭代表行
- java - 如何在不更改值顺序的情况下反转 TreeMap 的键顺序?
- php - 我该如何处理流明异常?
- java - Google Play 游戏服务登录问题
- flutter - 嘈杂的登录颤振谷歌地图
- javascript - 在 vue 中动态导入实例?
- ios - 为什么将 Int 枚举作为字典键,生成与 Int 作为字典键不同的 json 字符串?
- php - 标签不与复选框一起使用 - 不保存选中/未选中
- javascript - 在 php ajax 中选择选项过滤器后,导出到 Excel Pdf 在数据表中不起作用
- sql - 有什么方法可以为 GROUP BY 和 ORDER BY 中使用的列命名?