XTCLANG GUIDES
How to define Classes in Ecstasy
xtclang
class
object
class keywords
Authored by: Abby Sassel, Cameron Purdy
Last edited: @February 8, 2024
About this guide
Learn how to define Ecstasy classes, and use them idiomatically in your programs.
Topics:
- Introduction to classes in the Ecstasy language.
- Outline different kinds of class definitions and their purpose.
Target Audience
If you would like to gain a high level understanding of classes and how to work with them, this introduction to Ecstasy classes is for you!
It is helpful to have an understanding of Ecstasy’s syntax, structure and execution model prior to using this guide. For a primer, please refer to earlier guides here.
Classes
Classes are a tool for organization, essential for order and clarity.
They are to your code what files and folders are to your hard drive. Good hard drive organization is an art; clear file names, relevant file types and easily examinable file contents result in a drive that is easy to reason about and navigate. The same goes for code organization.
On a hard drive, information is stored in files, and those files are organized into directories -- because that is how operating systems store things. Similarly, in an class-based Object Oriented language like Ecstasy, everything is part of a class because that is how a class-based Object Oriented language organizes things. Classes are simple organizational tools in your programming toolkit.
To extend the analogy, think of a module
as the root directory on a hard drive, and a package
as a sub-directory within it. Both modules and packages are classes, but they also provide the hierarchical organization that is similar to the directory structure on a hard drive.
Defining a class
This guide aims to introduce you to the Ecstasy language, providing a foundation rather than an exhaustive syntax reference. Whilst many of the syntax choices in Ecstasy were purposefully based on Java and C#, there are some syntactic differences. Let's explore those similarity and differences now.
So how is a class defined?
// (1) (2)
class ClassName<type parameters>
// (3)
(building-blocks) {
// (4)
// class contents
}
- The class' form includes the keyword (
class
,const
,enum
, etc.) used to define it, as well as the optional -- and rarely-used -- modifierspublic
,private
,protected
, andstatic
. - The class' name is a simple (one part) name; the exception to this rule is the name for a module, which may be qualified like an Internet domain name, such as
MyApp.mycompany.com
. - The class' building blocks may include annotations, a super class, a list of implemented and/or delegated interfaces, and incorporated mixins. (Annotations on the class are placed in front of the class form keyword.)
- The class' contents may include: type parameters, constructors, methods, functions, properties, constants, typedefs, and classes.
Let's take a look at how building blocks are used. We'll start with the well-known class, HashMap
, from the Ecstasy core library:
class HashMap<Key extends Hashable, Value>
extends HasherMap<Key, Value>
implements Replicable
incorporates CopyableMap.ReplicableCopier<Key, Value>
Everything here would seem quite at home in either Java (HashMap<K,V>
) or C# (Dictionary<TKey,TValue>
), except for the incorporates
clause. While designing mixins is not a beginner-level topic, you can think of the incorporates
clause as being similar to the extends
clause in some ways: extends
specifies a named super-class that this class inherits from, while incorporates
specifies a mixin that has functionality that we want to assimilate in a Borg-like manner.
Stylistically, CamelCase is used for class and type names, and type parameters use natural names like "Key
", instead of a first letter thereof, and they do not have an extra "T" glued to the start of their name. There's a very important point hidden here, so let's make it obvious: There is no conceptual difference in Ecstasy between the type of Key
and the type of Int
. That's why they are written out in the same manner, capitalized in the same manner, and so on. They are both "named types", and in Ecstasy it would be a bit strange and confusing to arbitrarily assign some weird naming convention for either one of them.
Ecstasy does not have an "int
" type, nor any other "primitive" types; all Ecstasy values are objects, including things as simple as bits, bytes, booleans, characters, and integers. And as we discussed earlier, those objects must all have classes from which they are instantiated. And since they're just normal classes, the names are written out in the same manner as other classes, capitalized in the same manner, and so on.
Class keywords
In addition to module
and package
, Ecstasy uses six other form keywords to define classes:
interface
- a form of an abstract class that describes a set of properties and methods; as such, it is a reusable building block for building other classes. The term "interface" here has much the same meaning as when it is used in the term application programming interface (API). The use of this keyword in Ecstasy is consistent with its use in Java and C#.mixin
- a class that can be mixed into another class; a class can incorporate a mixin. A mixin is a re-usable building block that can be added to other classes -- even classes that weren't designed to have such functionality added to them.const
- a specialized form of a class that is deeply immutable by the time it completes construction.enum
- aconst
class that defines a list of enumerated values. The use of this keyword in Ecstasy is consistent with its use in Java and C#.service
- a class that defines a (i) new domain of mutability, which is (ii) asynchronous, (iii) concurrent, and (iv) has an explicitly manageable lifecycle. The closest analog for a service is a Web Worker in JavaScript, or a Process in Erlang.class
- the catch-all used to define any class that isn't covered by the above keywords. The use of this keyword in Ecstasy is consistent with its use in C++, Java, and C#.
This guide covered the basics of defining and using classes in Ecstasy. We hope this foundational knowledge equips you to use classes in your own programs and organize your code more effectively.
EXPLORE & LEARN
Ready to learn more?
Check out our latest guides, blogs and tutorials!