Skip to content

Commit

Permalink
Use final where possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
player-03 committed Jun 16, 2024
1 parent 9eb395d commit d031d20
Show file tree
Hide file tree
Showing 18 changed files with 190 additions and 190 deletions.
4 changes: 2 additions & 2 deletions src/echoes/ComponentStorage.hx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class ComponentStorage<T> {
}

public function remove(entity:Entity):Void {
var removedComponent:Null<T> = get(entity);
final removedComponent:Null<T> = get(entity);

#if (echoes_storage == "Map")
storage.remove(entity.id);
Expand Down Expand Up @@ -253,7 +253,7 @@ abstract EntityComponents(ComponentTypes) from ComponentTypes {
* might be `["StdTypes.Bool" => true, "String" => "Hello World"]`.
*/
@:to private inline function toMap():Map<String, Dynamic> {
var entity:Entity = switch(components.indexOf(cast this)) {
final entity:Entity = switch(components.indexOf(cast this)) {
case -1:
throw "This EntityComponents instance was disposed.";
case x:
Expand Down
14 changes: 7 additions & 7 deletions src/echoes/Echoes.hx
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ class Echoes {
*
* To search the full tree, use `activeSystems.find()`.
*/
public static var activeSystems(default, null):SystemList = {
var activeSystems:SystemList = new SystemList();
public static final activeSystems:SystemList = {
final activeSystems:SystemList = new SystemList();
activeSystems.__activate__();
activeSystems.clock.maxTime = 1;
activeSystems;
Expand Down Expand Up @@ -127,8 +127,8 @@ class Echoes {
* Updates all active systems.
*/
public static function update():Void {
var startTime:Float = haxe.Timer.stamp();
var dt:Float = startTime - lastUpdate;
final startTime:Float = haxe.Timer.stamp();
final dt:Float = startTime - lastUpdate;
lastUpdate = startTime;

activeSystems.__update__(dt);
Expand Down Expand Up @@ -192,8 +192,8 @@ class Echoes {
* @see `getView()` to automatically activate the view.
*/
public static #if !macro macro #end function getInactiveView(componentTypes:Array<ExprOf<Class<Any>>>):Expr {
var componentComplexTypes:Array<ComplexType> = [for(type in componentTypes) type.parseClassExpr()];
var viewName:String = componentComplexTypes.getViewName();
final componentComplexTypes:Array<ComplexType> = [for(type in componentTypes) type.parseClassExpr()];
final viewName:String = componentComplexTypes.getViewName();
componentComplexTypes.createViewType();

return macro $i{ viewName }.instance;
Expand All @@ -212,7 +212,7 @@ class Echoes {
* ```
*/
public static #if !macro macro #end function getView(componentTypes:Array<ExprOf<Class<Any>>>):Expr {
var view:Expr = getInactiveView(componentTypes);
final view:Expr = getInactiveView(componentTypes);

return macro {
$view.activate();
Expand Down
4 changes: 2 additions & 2 deletions src/echoes/Entity.hx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ abstract Entity(Int) {
* A destroyed entity's ID will go in this pool, and will then be reassigned
* to the next entity to be created.
*/
private static var idPool:Array<Int> = [];
private static final idPool:Array<Int> = [];

/**
* Whether this entity is active. If false, it may also be destroyed.
Expand Down Expand Up @@ -93,7 +93,7 @@ abstract Entity(Int) {
* you'll have to call `activate()`.
*/
public inline function new(?active:Bool = true) {
var id:Null<Int> = idPool.pop();
final id:Null<Int> = idPool.pop();

this = id != null ? id : nextId++;

Expand Down
6 changes: 3 additions & 3 deletions src/echoes/System.hx
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,9 @@ class System {
* Returns a view that will activate and deactivate when the system does.
*/
public macro function getLinkedView(self:Expr, componentTypes:Array<ExprOf<Class<Any>>>):Expr {
var view:Expr = Echoes.getInactiveView(componentTypes);
final view:Expr = Echoes.getInactiveView(componentTypes);
return macro {
var self = $self;
final self = $self;
self.onActivate.push($view.activate);
self.onDeactivate.push($view.deactivate);
$view;
Expand All @@ -227,7 +227,7 @@ private class ChildSystem extends System {

private override function __update__(dt:Float):Void {
#if echoes_profiling
var __timestamp__ = Date.now().getTime();
final __timestamp__ = Date.now().getTime();
#end

runUpdateListeners(dt);
Expand Down
12 changes: 6 additions & 6 deletions src/echoes/SystemList.hx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SystemList extends System {
return clock.paused = value;
}

private var systems:Array<System> = [];
private final systems:Array<System> = [];

public function new(?name:String = "SystemList", ?clock:Clock, ?priority:Int = 0) {
super(priority);
Expand Down Expand Up @@ -67,7 +67,7 @@ class SystemList extends System {

private function __recalculateOrder__(system:System):Void {
if(systems.remove(system)) {
var index:Int = Lambda.findIndex(systems, existingSystem ->
final index:Int = Lambda.findIndex(systems, existingSystem ->
existingSystem.priority < system.priority);

if(index >= 0) {
Expand All @@ -80,7 +80,7 @@ class SystemList extends System {

private override function __update__(dt:Float):Void {
#if echoes_profiling
var startTime:Float = haxe.Timer.stamp();
final startTime:Float = haxe.Timer.stamp();
#end

__dt__ = dt;
Expand Down Expand Up @@ -108,7 +108,7 @@ class SystemList extends System {
system.parent.remove(system);
}

var index:Int = Lambda.findIndex(systems, existingSystem ->
final index:Int = Lambda.findIndex(systems, existingSystem ->
existingSystem.priority < system.priority);

if(index >= 0) {
Expand Down Expand Up @@ -148,7 +148,7 @@ class SystemList extends System {
*/
public override function find<T:System>(systemType:Class<T>):Null<T> {
for(child in systems) {
var result:Null<T> = child.find(systemType);
final result:Null<T> = child.find(systemType);

if(result != null) {
return result;
Expand All @@ -159,7 +159,7 @@ class SystemList extends System {
}

public override function getStatistics():SystemDetails {
var result:SystemDetails = super.getStatistics();
final result:SystemDetails = super.getStatistics();
result.children = [for(system in systems) system.getStatistics()];
return result;
}
Expand Down
14 changes: 7 additions & 7 deletions src/echoes/macro/ComponentStorageBuilder.hx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using haxe.macro.ComplexTypeTools;
class ComponentStorageBuilder {
public static inline final PREFIX:String = "ComponentStorage_";

private static var storageCache:Map<String, TypeDefinition> = new Map();
private static final storageCache:Map<String, TypeDefinition> = new Map();

private static var registered:Bool = false;

Expand All @@ -25,20 +25,20 @@ class ComponentStorageBuilder {
public static function getComponentStorageName(componentComplexType:ComplexType):String {
componentComplexType = componentComplexType.followComplexType();

var error:String = componentComplexType.getReservedComponentMessage();
final error:String = componentComplexType.getReservedComponentMessage();
if(error != null) {
Context.error(error, Context.currentPos());
}

var storageTypeName:String = PREFIX + componentComplexType.toIdentifier();
final storageTypeName:String = PREFIX + componentComplexType.toIdentifier();
if(storageCache.exists(storageTypeName)) {
return storageTypeName;
}

var componentTypeName:String = new Printer().printComplexType(componentComplexType);
var storageTypePath:TypePath = { pack: [], name: storageTypeName };
var storageType:ComplexType = TPath(storageTypePath);
var def:TypeDefinition = macro class $storageTypeName extends echoes.ComponentStorage<$componentComplexType> {
final componentTypeName:String = new Printer().printComplexType(componentComplexType);
final storageTypePath:TypePath = { pack: [], name: storageTypeName };
final storageType:ComplexType = TPath(storageTypePath);
final def:TypeDefinition = macro class $storageTypeName extends echoes.ComponentStorage<$componentComplexType> {
public static final instance:$storageType = new $storageTypePath();

private function new() {
Expand Down
40 changes: 20 additions & 20 deletions src/echoes/macro/EntityTemplateBuilder.hx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class EntityTemplateBuilder {
//=====================

//Get information about the `abstract` being built.
var type:AbstractType = switch(Context.getLocalType()) {
final type:AbstractType = switch(Context.getLocalType()) {
case TInst(_.get().kind => KAbstractImpl(_.get() => type), _):
type;
default:
Expand All @@ -42,7 +42,7 @@ class EntityTemplateBuilder {
}

//Get information about parent types.
var parents:Array<{ complexType:ComplexType, abstractType:AbstractType }> = [];
final parents:Array<{ complexType:ComplexType, abstractType:AbstractType }> = [];
var nextParent:Type = type.type;
for(_ in 0...100) {
switch(nextParent.follow()) {
Expand Down Expand Up @@ -87,22 +87,22 @@ class EntityTemplateBuilder {
* Contains a temporary `storage` value, which is the identifier for the
* `ComponentStorage` for this type.
*/
var parameters:Array<FunctionArg & { storage:String }> = [];
final parameters:Array<FunctionArg & { storage:String }> = [];

/**
* Arguments to pass to `applyTemplateToSelf()`.
*/
var arguments:Array<Expr> = [];
final arguments:Array<Expr> = [];

/**
* Arguments to pass to the super type's `applyTemplateToSelf()`.
*/
var superArguments:Array<Expr> = [];
final superArguments:Array<Expr> = [];

/**
* The types marked as optional that don't yet have a default value.
*/
var optionalValuesRemaining:Map<String, ComplexType> = [];
final optionalValuesRemaining:Map<String, ComplexType> = [];

/**
* Adds the given value to `parameters` and `arguments` unless it's
Expand All @@ -117,7 +117,7 @@ class EntityTemplateBuilder {
name = n;
type = t.followComplexType();
default:
var fieldChain:Null<String> = param.printFieldChain();
final fieldChain:Null<String> = param.printFieldChain();
if(fieldChain != null) {
try {
type = fieldChain.getType().followMono().toComplexType();
Expand All @@ -132,7 +132,7 @@ class EntityTemplateBuilder {
}
}

var storage:String = type.getComponentStorageName();
final storage:String = type.getComponentStorageName();
var existingName:String = null;
for(existing in parameters) {
if(existing.storage == storage) {
Expand Down Expand Up @@ -188,8 +188,8 @@ class EntityTemplateBuilder {

//Allow converting to all parent types.
for(parent in parents) {
var name:String = "to" + parent.abstractType.name;
var parentType:ComplexType = parent.complexType;
final name:String = "to" + parent.abstractType.name;
final parentType:ComplexType = parent.complexType;
fields.pushFields(macro class ToParent {
/**
* Caution: in C++, this converts `null` to `0`. Specifically,
Expand All @@ -204,7 +204,7 @@ class EntityTemplateBuilder {
}

//Process the component variables.
var knownComponents:Array<FunctionArg> = [];
final knownComponents:Array<FunctionArg> = [];
for(field in fields) {
if(field.access != null && field.access.contains(AStatic)) {
continue;
Expand Down Expand Up @@ -258,8 +258,8 @@ class EntityTemplateBuilder {
//update the metadata to make it match, but since macro order is
//unspecified, a child type may have already been built using
//the wrong metadata.)
var storage:String = componentType.getComponentStorageName();
var parameter:FunctionArg = parameters.find(p -> p.storage == storage);
final storage:String = componentType.getComponentStorageName();
final parameter:FunctionArg = parameters.find(p -> p.storage == storage);
if(parameter != null) {
if(parameter.opt) {
optionalValuesRemaining.remove(storage);
Expand All @@ -282,8 +282,8 @@ class EntityTemplateBuilder {
//Convert the field to a property, and remove the expression.
field.kind = FProp("get", "set", macro:Null<$componentType>, null);

var getter:String = "get_" + field.name;
var setter:String = "set_" + field.name;
final getter:String = "get_" + field.name;
final setter:String = "set_" + field.name;

fields.pushFields(macro class Accessors {
private inline function $getter():Null<$componentType> {
Expand All @@ -298,9 +298,9 @@ class EntityTemplateBuilder {
}

if(!optionalValuesRemaining.empty()) {
var missing:Array<String> = [for(type in optionalValuesRemaining)
final missing:Array<String> = [for(type in optionalValuesRemaining)
new Printer().printComplexType(type)];
var s:String = missing.length == 1 ? "" : "s";
final s:String = missing.length == 1 ? "" : "s";

Context.fatalError('Missing default value$s for the following component type$s: '
+ missing.join(", "), Context.currentPos());
Expand All @@ -313,7 +313,7 @@ class EntityTemplateBuilder {
* An ordered list of parameters taken by the constructor and the
* "apply" functions. This version no longer exposes `storage`.
*/
var parameters:Array<FunctionArg> = [for(p in parameters) p];
final parameters:Array<FunctionArg> = [for(p in parameters) p];

/**
* Adds `parameters` to each function in the given type definition, then
Expand All @@ -335,7 +335,7 @@ class EntityTemplateBuilder {
}

//Add the constructor and `applyTemplateTo()`.
var templateType:ComplexType = TPath({ pack: [], name: type.name });
final templateType:ComplexType = TPath({ pack: [], name: type.name });
addFunctions(macro class Constructor {
public static inline function applyTemplateTo(entity:echoes.Entity):$templateType {
(cast entity:$templateType).applyTemplateToSelf($a{ arguments });
Expand All @@ -353,7 +353,7 @@ class EntityTemplateBuilder {
//Prepare the `applyTemplateToSelf()` function. Set the components in
//order of priority: values passed by the user, then known values, then
//inherited values.
var applyToSelfExprs:Array<Expr> = [];
final applyToSelfExprs:Array<Expr> = [];
for(parameter in parameters) {
applyToSelfExprs.push(macro this.addIfMissing($i{ parameter.name }));
}
Expand Down
Loading

0 comments on commit d031d20

Please sign in to comment.