You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Ren classes will be inspired by C# and TypeScript classes. They are declarable wherever types can be declared, and used wherever types are used.
Constructors
Classes can contain one or more constructors, which are functions that receive parameters and an implicit this variable, and must return an instance of the class (it can be different from this).
Class members
Classes can contain fields, which mirror the syntax of struct type fields, and can also contain an initializer.
They can contain methods, with the syntax <type> <name>(<...params>) => <body>. This will need ambiguity resolution logic with fields.
Classes can contain properties, with the syntax <type> <name> { get => <body> set => <body with value> }. Either get or set are optional.
Shorthand properties are of the form <type> <name> { get set } where set is optional, in which case an initializer is required: <type> <name> { get } = <value> or in the constructor(s).
Inheritance
Classes can inherit from other classes using the extends keyword, and implement interfaces using the implements keyword. Classes labelled sealed cannot be overridden.
Access modifiers
Any member of a class can be private, meaning that it is only accessible from inside the class. Class members are private by default.
Any member of a class can be protected, meaning that it is only accessible from the class and subclasses.
Any member of a class can be public, meaning that it is accessible anywhere the class is imported.
Abstract classes/members
Methods and properties can be abstract, in which case the class must be abstract. Abstract members may omit bodies, but any non-abstract child class must implement them. For abstract members with bodies, child classes can implement them using the super keyword.
Virtual members
Methods and properties can be virtual, allowing subclasses to override them. Any class wishing to override a member must use the override keyword. Methods and properties not labelled virtual cannot be overridden. (We may use a new keyword like C# to explicitly hide members)
Static stuff
Static members are not a thing in this language because there are far simpler alternatives.
Assignability with classes
Any value is assignable to a class type if its type is the class or one of the classes child types.
Because of this, it is possible for the runtime type of an instance to be more specific than its compile time type.
This is contrary to primitive values, which will be coerced to the compile time type.
The is operator and getType() method are required to inspect the runtime type of an instance.
An important thing that differentiates classes from other types is that classes are not structural types. Assignability is determined nominally, and the structure does not matter.
An interesting thing here is that class instances are assignable to struct types if the fields match, but not the other way around.
Using classes
Classes are instantiated using the new keyword followed by the class name and a set of arguments that match one of the constructors.
This is an expression that returns an instance of the class corresponding to the chosen constructor.
Methods and properties can access class members using the special this value, which always points to the current instance of the class.
If this is omitted, class members are last in the precedence order for resolving ambiguities, thus the order is:
scope variables (recursively upwards to the top scope of the function)
module-scoped names
class members
super is a special form of this that allows access to ancestors' members.
super() in a constructor allows parent constructors to be called. super must be called at some point during any child constructor.
New syntactic features:
New Tokens:
CLASS ::= class
SUPER ::= super
EXTENDS ::= extends
IMPLEMENTS ::= implements
CONSTRUCTOR ::= constructor
GET ::= get
SET ::= set
SEALED ::= sealed
PRIVATE ::= private
PROTECTED ::= protected
PUBLIC ::= public
ABSTRACT ::= abstract
VIRTUAL ::= virtual
OVERRIDE ::= override
IS ::= is
NEW ::= new
VALUE ::= value
Classes
Dependencies: Interfaces, Overloads, Extensions, Assignables
Description
Ren classes will be inspired by C# and TypeScript classes. They are declarable wherever types can be declared, and used wherever types are used.
Constructors
Classes can contain one or more constructors, which are functions that receive parameters and an implicit
this
variable, and must return an instance of the class (it can be different fromthis
).Class members
<type> <name>(<...params>) => <body>
. This will need ambiguity resolution logic with fields.<type> <name> { get => <body> set => <body with value> }
. Either get or set are optional.<type> <name> { get set }
where set is optional, in which case an initializer is required:<type> <name> { get } = <value>
or in the constructor(s).Inheritance
Classes can inherit from other classes using the extends keyword, and implement interfaces using the implements keyword. Classes labelled sealed cannot be overridden.
Access modifiers
Abstract classes/members
Methods and properties can be abstract, in which case the class must be abstract. Abstract members may omit bodies, but any non-abstract child class must implement them. For abstract members with bodies, child classes can implement them using the super keyword.
Virtual members
Methods and properties can be virtual, allowing subclasses to override them. Any class wishing to override a member must use the override keyword. Methods and properties not labelled virtual cannot be overridden. (We may use a
new
keyword like C# to explicitly hide members)Static stuff
Static members are not a thing in this language because there are far simpler alternatives.
Assignability with classes
is
operator andgetType()
method are required to inspect the runtime type of an instance.Using classes
new
keyword followed by the class name and a set of arguments that match one of the constructors.this
value, which always points to the current instance of the class.this
is omitted, class members are last in the precedence order for resolving ambiguities, thus the order is:super
is a special form ofthis
that allows access to ancestors' members.super()
in a constructor allows parent constructors to be called.super
must be called at some point during any child constructor.New syntactic features:
New Tokens:
New NonTerminals:
Extended NonTerminals:
New semantic features:
Transformation:
The text was updated successfully, but these errors were encountered: