archives

Structural Typing in .NET through Type-Parameters

I have a question associated to some functionality I want to implement in a compiler for .NET. I originally called this 'duck typing' but after reading up more on type systems, that's incorrect.

The basic idea follows what's called structural typing; the main aim is to implement such a thing through type-parameter extensions. Naturally since this isn't something implemented at the low-level Common Intermediate Language, I'll have to handle the functionality myself.

Essentially you would define the requirements of a given type-parameter on the constraints of that type.

The information would be encoded within a series of interfaces which represent the various member constraints. Call sites that utilize the functionality would emit a type for every generic closure (a specific set of types closed in a specific instance vs. the full set of possible types, is this term proper?) of the generic type they use. I could go into details of how this will be handled on a functional level, but that's for a different discussion.

The question I have is: is structural typing useful for .NET Languages? The concept would be statically typed in that the compiler generates the corresponding types to handle call dispatch. Assemblies built using a constraint-ignorant compiler (like C#) would be processed at run-time when the first call is made. This isn't 'dynamic' typing for the obvious fact that no change to the run-time state of the assembly can affect the object-structure linking and subsequent call dispatch.

Insight welcome.