首页 > 解决方案 > 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.

标签: javac#

解决方案


Scala的复合类型与您所描述的非常相似。复合类型允许在声明变量/参数时枚举类型

val myVal: Type1 with Type2 with TypeN = new Type1 with Type2 with TypeN

推荐阅读