From 9718b8a454d250d86c55fe348723d514978c27b7 Mon Sep 17 00:00:00 2001 From: Joseph Cloutier Date: Fri, 12 Jan 2024 15:12:24 -0500 Subject: [PATCH] Improve `lime.tools.Platform` type safety. We shouldn't have to cast it every time! --- src/lime/tools/HXProject.hx | 8 ++++---- src/lime/tools/Platform.hx | 8 +++++++- tools/CommandLineTools.hx | 28 ++++++++++++++-------------- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/lime/tools/HXProject.hx b/src/lime/tools/HXProject.hx index 9c814bdef..cc326a659 100644 --- a/src/lime/tools/HXProject.hx +++ b/src/lime/tools/HXProject.hx @@ -87,7 +87,7 @@ class HXProject extends Script var outputFile = args[1]; HXProject._command = inputData.command; - HXProject._target = cast inputData.target; + HXProject._target = inputData.target; HXProject._debug = inputData.debug; HXProject._targetFlags = inputData.targetFlags; HXProject._templatePaths = inputData.templatePaths; @@ -742,7 +742,7 @@ class HXProject extends Script defines.set("uwp", "1"); defines.set("winjs", "1"); } - else if (platformType == DESKTOP && target != cast System.hostPlatform) + else if (platformType == DESKTOP && target != System.hostPlatform) { defines.set("native", "1"); @@ -834,7 +834,7 @@ class HXProject extends Script { if (_target == null) { - _target = cast System.hostPlatform; + _target = System.hostPlatform; } if (_targetFlags == null) @@ -1040,7 +1040,7 @@ class HXProject extends Script // Getters & Setters private function get_host():Platform { - return cast System.hostPlatform; + return System.hostPlatform; } private function get_templateContext():Dynamic diff --git a/src/lime/tools/Platform.hx b/src/lime/tools/Platform.hx index 899ea7636..d799467d1 100644 --- a/src/lime/tools/Platform.hx +++ b/src/lime/tools/Platform.hx @@ -1,6 +1,6 @@ package lime.tools; -#if (haxe_ver >= 4.0) enum #else @:enum #end abstract Platform(String) +#if (haxe_ver >= 4.0) enum #else @:enum #end abstract Platform(String) from hxp.HostPlatform { var AIR = "air"; var ANDROID = "android"; @@ -24,4 +24,10 @@ package lime.tools; var EMSCRIPTEN = "emscripten"; var TVOS = "tvos"; var CUSTOM = null; + + @:op(A == B) @:commutative + private inline function equalsHostPlatform(hostPlatform:hxp.HostPlatform):Bool + { + return this == hostPlatform; + } } diff --git a/tools/CommandLineTools.hx b/tools/CommandLineTools.hx index f28d71fc1..af7aab780 100644 --- a/tools/CommandLineTools.hx +++ b/tools/CommandLineTools.hx @@ -231,31 +231,31 @@ class CommandLineTools switch (targetName) { case "cpp": - target = cast System.hostPlatform; + target = System.hostPlatform; targetFlags.set("cpp", ""); case "neko": - target = cast System.hostPlatform; + target = System.hostPlatform; targetFlags.set("neko", ""); case "hl", "hashlink": - target = cast System.hostPlatform; + target = System.hostPlatform; targetFlags.set("hl", ""); case "cppia": - target = cast System.hostPlatform; + target = System.hostPlatform; targetFlags.set("cppia", ""); case "java": - target = cast System.hostPlatform; + target = System.hostPlatform; targetFlags.set("java", ""); case "nodejs": - target = cast System.hostPlatform; + target = System.hostPlatform; targetFlags.set("nodejs", ""); case "cs": - target = cast System.hostPlatform; + target = System.hostPlatform; targetFlags.set("cs", ""); case "iphone", "iphoneos": @@ -1511,31 +1511,31 @@ class CommandLineTools switch (targetName) { case "cpp": - target = cast System.hostPlatform; + target = System.hostPlatform; targetFlags.set("cpp", ""); case "neko": - target = cast System.hostPlatform; + target = System.hostPlatform; targetFlags.set("neko", ""); case "hl", "hashlink": - target = cast System.hostPlatform; + target = System.hostPlatform; targetFlags.set("hl", ""); case "cppia": - target = cast System.hostPlatform; + target = System.hostPlatform; targetFlags.set("cppia", ""); case "java": - target = cast System.hostPlatform; + target = System.hostPlatform; targetFlags.set("java", ""); case "nodejs": - target = cast System.hostPlatform; + target = System.hostPlatform; targetFlags.set("nodejs", ""); case "cs": - target = cast System.hostPlatform; + target = System.hostPlatform; targetFlags.set("cs", ""); case "iphone", "iphoneos":