Tag Archives: type system

Static inheritance of an interface

The more I dive into implementing Skila the more I appreciate the role of the type system. Yet another interesting case is with generics — how to make them compile-once and be efficient for both complex and native types? Say, you would like to implement “Vector<T>” — one operation which has to be executed is copying the value.

In C++ world you define template and when using it, given type appears viable (or not), however it goes with the price of compilation of each instantiation of the template. In C# you can specify constraint that “T” has to inherit from something like “ICopyable”. But native types do not inherit from anything, so how to make them fit into a template?

I noticed that generics supports object oriented programming, but is not dependent of it. So sure, you can express such constraint, but you can specify another one — making “T” to provide all methods (or extension functions) declared in “ICopyable”.

This makes type such as “Int” fit for “Vector”:

class Vector<T>
  where T : static ICopyable;
...

Note the keyword “static”, “T” is no longer checked against given inheritance, but against providing a set of methods.

The methods are checked when “Vector” is instantiated, if you would like to make the code more explicit specify static inheritance when defining the class:

class Foo : static ICopyable
...

This does not add regular inheritance, instead it enforces the given suite of the methods when defining the class “Foo”, not when calling for “Vector<Foo>”.

Tagged , , ,

Type system — a job for Sisyphus

Motivated by Nimrod I changed my C#-like model to C++-like one (the desire of fast execution also played role, watch fascinating lecture “Modern C++: What You Need to Know” by Herb Sutter). It was crazy move, I know, but at the same time very educational, since I really didn’t designed type system for Skila, but rather borrowed it.

Hard way and depressing (I fixed one thing, just to discover two more flaws) I learned how many issues sound type system involves. Take for example “this” pointer — feature in C#, bug in C++ (yeah, pointer to stack just asks for trouble). A function with interface as parameter — feature in C#, bug in C++ (because of value slicing). And so on and on…

When I came to generic types with constraints my head was already spinning. What worse the syntax enriched with nullable marker is even more cluttered than in C++.

func foo(x ref ~ptr? Vector<Int>)

does not look especially charming, doesn’t it? Explanation: function takes mutable nullable pointer to view (read only) of “Vector” passed by reference.

So honestly I am not sure I will keep this type model, yet some simple tasks in C# are not available exactly because of the type system, like managing the layout of the data in memory, and those simple tasks hurt the performance badly.

I just hope I am not searching for Holy Grail… Except for this, the good news is I’ve done it! Ahead of me is polishing, refactoring and writing more test cases.

Tagged , ,