{"slug": "integers-as-types", "title": "Integers as types", "summary": "The article discusses a design consideration for the ALFE programming language, where the author explores the possibility of treating integers as types to enable fixed-length arrays indexed by non-integer types like Boolean. The author proposes using integer literal type names (e.g., \"2\") or difference types (e.g., \"3-1\") to represent array indices, though this introduces parsing ambiguities and conceptual differences from standard integer values. Ultimately, the idea aims to allow compile-time checking of array indices and support for range-based types.", "body_md": "In C++ there are two kinds of things you can use as template arguments. One is the obvious one, types, but less well known is that integers (and some other values) can also be used. Early on in the design of ALFE's type system I decided to simplify things by having template arguments all be type constructors. If you really need to use an integer (or indeed any other value) as a template argument you can wrap it in a type.\nHowever, I recently thought of another feature I'd like ALFE fixed-length arrays to have - the ability to be indexed by a type other than Integer\n. For example, we might want to have an array indexed by a Boolean\n:\nNow we could just convert the Boolean\nto an Integer\nwhen indexing the array:\nBut that seems ugly especially if we're indexing barData\nin a lot of places. We could turn it into a helper method but then it's less obvious to callers that it's an array access. Also this is rather more unwieldy if our array is indexed by an Enumeration type.\nWhat is the name of the type of an array that takes Boolean\nand returns Foo\n? By analogy with the function type Foo(Boolean)\nwe would expect it to be Foo[Boolean]\nwhich is quite nice (though makes the sequence type [Boolean]\neven more irregular - maybe that should be renamed as Boolean[]\n?)\nSo if Foo[Boolean]\nis a array of Foo\nindexed by Boolean\nthen Foo[2]\nshould be an array of Foo\nindexed by a type called \"2\n\", which must then be an integral type with values 0\nand 1\n. So perhaps integers are types after all. Integer literal type names can't be used everywhere that normal type names can occur, though - it would lead to too many parsing ambiguities. So we'd need another name for the type \"2\n\", perhaps \"LessThan<Class{Int n=2;}>\n\", \"()[2].Indexer\n\" or \"Zero.Successor.Successor\n\".\nOf course, if you want that you'll almost certainly also want ranges. We can kill both those birds (and many others) while still keeping the same meaning for Foo[2]\nby creating difference types: \"A-B\n\" is a type whose values are those that are in A\nbut not in B\n. So the type \"3-1\n\" would allow the values 1\nand 2\n. Unfortunately 3-1\nis not the same as 2\n(though the two types do have the same cardinality).\nAlternatively, the idea that the type \"2\n\" should be a type with a single value (the value 2\n) also has some beauty especially when combined with sum types so that you could create a type that can contain some arbitrary subset of integers and have that checked at compile time.", "url": "https://wpnews.pro/news/integers-as-types", "canonical_source": "https://www.reenigne.org/blog/integers-as-types/", "published_at": "2016-02-19 16:00:06+00:00", "updated_at": "2026-05-23 13:10:19.070424+00:00", "lang": "en", "topics": ["developer-tools", "research"], "entities": ["ALFE", "C++"], "alternates": {"html": "https://wpnews.pro/news/integers-as-types", "markdown": "https://wpnews.pro/news/integers-as-types.md", "text": "https://wpnews.pro/news/integers-as-types.txt", "jsonld": "https://wpnews.pro/news/integers-as-types.jsonld"}}