The Language Reference & Guide
- Welcome to Emojicode
- Syntax
- The Basics
- Literals
- Variables and Assignment
- Control Flow
- Classes & Value Types
- Overloading
- Operators
- Optionals
- Errors
- Inheritance and Overriding
- Protocols
- Enumerations
- Types and Namespaces
- Types as Values
- Documentation
- Generics
- Callables
- Packages
- Threads
- Safe and Unsafe Code
- Memory Management
- References
- Appendix: The Emojicode Compiler
Types as Values
Emojicode allows you to pass types as value. You can then call type methods on these types or instantiate them.
The Type of a Type Value
Naturally, if you treat a type as value, this value must have a type as well. This type has the same name as the type the value represents but prefixed with the emoji, with which the type was defined. Letβs take a look at these examples:
ππ the_integer_type ππ’
ππ a_class_type ππ‘
In the example above two variables were declared. the_integer_type
which can hold the type π’ and a_class_type
which can hold the type π‘.
Note that you cannot use any built-in type like βͺοΈ or π΅ or optionals or errors as value. Furthermore, the type must always be prefixed with the appropriate emoji. A value type, for example, must be prefixed with π or the compiler will raise an error.
type-value βΆ type-value-type-emoji type
type-value-type-emoji βΆ π | π | π¦ | π
Creating Type Values
Now let us create a type value. Type values are created exactly the same way their types are declared. So in order to populate our variables we can write this:
ππ’ β‘οΈ πthe_integer_type
ππ‘ β‘οΈ πa_class_type
Voila!
Compatibility of Type Values
With the exception of classes, the type of two type values are only compatible if they are identical. The type of a class type value, however, is compatible with the type of the type value of its superclass.
The following is therefore correct:
π π· ππ
π πΊ π· ππ
π π
ππ a_flower_type ππ·
ππΊ β‘οΈ πa_flower_type
π
Using Type Values
You can use type values whenever a type expression is expected with β¬οΈ:
type-from-expr βΆ β¬οΈ expression
expresssion must naturally evaluate to a type value.
π
is a shortcut for β¬οΈπ
when a type expression is expected.
The following example stores three different type values in a list, instantiates them at run-time and calls a method on these instances:
π π π
π π ππ
βοΈ π π
π π€Iβm a fish.π€βοΈ
π
π
π π‘ π π
π π π ‴οΈπβοΈ π
βοΈ βοΈ π π
π π€Iβm a blowfish.π€βοΈ
π
π
π π π π
π π π ‴οΈπβοΈ π
βοΈ βοΈ π π
π π€Iβm a whale.π€βοΈ
π
π
π π π π
π π π ‴οΈπβοΈ π
βοΈ βοΈ π π
π π€Iβm a tropical fish.π€βοΈ
π
π
π π
π¨ππ ππ‘ ππ ππ π β‘οΈ classes
π class classes π
πβ¬οΈ class πβοΈ β‘οΈ fish
π fishβοΈ
π
π