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 and Namespaces
Namespaces
Emojicode uses namespaces to avoid problems with equally named types. If you, for instance, defined a type called π½ and you needed to import another package, which exports a type named π½ too, there would be a problem. To prevent this, you can import the package into another namespace.
In Emojicode types are not members of namespaces, but are available through namespaces. This also implies that a type may be reachable through several namespaces. The name of a namespace is always an emoji.
By default, that is when you specify no namespace, you access types through the namespace π . You do this all the time, for instance when declaring a variable of string type:
ππ text π‘
When writing only π‘ the compiler assumes that the type is in the default namespace π .
You can, of course, explicitly refer to a type in a namespace using the namespace accessor πΆ:
ππ text πΆπ π‘
This denotes that we want the type π‘ reachable in the namespace π .
This syntax can be used everywhere a type is expected. For example, to instantiate the type π in the namespace π₯, we would use:
ππΆππ₯βοΈ
Namespacing When Declaring a Type
By default, when you declare a type it will be available through the default namespace π . It is possible to specify another namespace with the namespace accessor though:
π πΆπ¨π©βπΌ π
π
This declares a class π©βπΌ that will be reachable through the namespace π¨.
Type Expectations
Emojicode uses so-called type expectations for type inference. Whenever an expression, whose type is determined by type inference, is evaluated, type expectations are considered to determine the type. This mainly concerns literals.
When you call a method, for instance, the types of the parameters become type expectations. If, for example, you defined a method that takes one argument of type π‘ and you call that method, the first argument will be expected to be a string. Another example would be a variable assignment. If you have declared a variable of a certain type, the compiler will expect this type when assigning to the variable.
Built-In Types
These types are built right into the language and are not defined in any package.
βͺ Something
βͺ is special as all types are compatible to it. This means that you can, for instance, store a value of any type into a variable of type βͺ:
ππ surprise βͺ
π€Anything, anything, anythingπ€ β‘οΈ πsurprise
1004 β‘οΈ πsurprise
You cannot call any methods on βͺ and you cannot cast to βͺ at run-time.
π΅ Someobject
All instances of classes are compatible to π΅, but value type instances are not.
βΌοΈ No Return
βΌοΈ represents the return type of a method or closure that returns nothing. It has no use other than that.
π² Type Casting
Type casting is a way to determine whether a value is of a given type at run-time and to treat the value as an instance of this type.
Type casting is implemented with the π² statement:
cast βΆ π² expression type-expr
value is the value to be casted to type. If value can be casted to type value is returned as type. If value canβt be casted to type no value is returned. π² therefore returns an optional.
π² txt π‘ π Tries to cast txt to π‘
π² a π π Tries to cast a to π
π² b π₯ ππ₯ππ¦ππ¬π₯ππ‘ππππ’π―π π Tries to cast b to π₯ ππ₯ππ¦ππ¬π₯ππ‘ππππ’π―π
Donβt confuse type casting with type conversion. You canβt cast π’ to π―. You need to use a suitable conversion method instead.
βοΈ Size of Type Instance
The βοΈ expression allows you to determine the number of bytes an instance of a provided type will take up at runtime:
size-of βΆ βοΈ type
The following, for example, prints the size of an integer.
π π‘ βοΈπ’ βοΈβοΈ
Syntax
type-expr βΆ β«οΈ | type-from-expr | type | this
type βΆ [π¬] [β΄οΈ] type-main | π¨ type type | βͺ | βΌοΈ
type-main βΆ variable | callable-type | type-identifier generic-arguments
type-main βΆ π± types π± | π΅ | type-value
type-identifier βΆ πΆ type-emoji type-emoji | type-emoji
types βΆ type types | type
type-emoji βΆ οΏ’βͺ οΏ’π΅ οΏ’π¬ οΏ’π± οΏ’πΆ emoji-id