Menu

Reflection

Will Pittenger

Overview

Type reflection in ASIL is done via the Type and TypeInstance classes. (TypeInstance contains a Type reference and is used to differentiate between various types of references and value instances. Variables like ref int and int share the same Type instance, but different TypeInstance instances.)

A variable declared using a shortcut versus a variable declared without a shortcut

It doesn't matter if you declare a variable with or without a type shortcut. Both var int i and var i% share the same Type instance.

The type member in every type

Every type, including primitives, has a special member called type. (If you need "type" as the name of a member or method, you'll need to declare it with the new keyword.) This is a TypeInstance instance and can be compared with a Type instance. You can explicitly access the Type instance with "type.declaration".

Accessing a Type instance without a instance of the type

Unlike other languages, ASIL doesn't need a special operator to turn a type name into the type used for reflection. If the parser encounters a type name where a type name wasn't expected, it tries the corresponding Type instance instead. So "int = 2.type.declaration" is a true expression. This includes the identifier for a generic type parameter. The Type class will be comparable to those in other languages.

[Type aliases]

Type aliases pose a special challenge. A TypeAlias has a class very similar to TypeInstance called TypeAlias. However, a TypeAlias can be based on another TypeAlias as long as there are no circular references.


Related

Wiki: Home
Wiki: Type aliases
Wiki: keywords-new

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.