Haxe 2 - Haxe 3迁移指南

2023-10-11 21:58
文章标签 指南 迁移 haxe

本文主要是介绍Haxe 2 - Haxe 3迁移指南,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

官网英文原文:http://haxe.org/manual/haxe3_migration

混合类型数组
症状: 编译错误 Arrays of mixed types are only allowed if the type is forced to Array<Dynamic>
解释:Haxe 2允许这样定义混合数组: [1, "foo"],编译器会自动推断其类型为Array<Dynamic>。Haxe 3不允许如此定义,除非数组被显式声明为Array<Dynamic>。
修正:使用Array<Dynamic>显式定义混合类型数组。包括以下几种情形:
赋值到类型为Array<Dynamic>的变量: var a:Array<Dynamic> = [1, "foo"];
赋值到类型为Array<Dynamic>的类成员域
将其作为需求Array<Dynamic>类型的函数参数
从声明返回类型为Array<Dynamic>的函数返回值
例程:

// haxe 2
var x = [1, "foo"];

// haxe 3
var x:Array<Dynamic> = [1, "foo"];

Callback关键字
症状:编译错误 callback syntax has changed to func.bind(args)
解释:Haxe 2 had a callback keyword which could be used for partial application. In haxe 3 the special field bind can be invoked on functions.
修正:Replace callback(func,args) with func.bind(args)
例程:

function add(a, b) return a + b;
// haxe 2
callback(add, 1);
// haxe 3
add.bind(1);

系统平台API的整理统一

症状:Class not found errors for the following packages:
cpp, cpp.io, cpp.net
neko, neko.db, neko.io, neko.net, neko.zip
php, php.db, php.io, php.net
解释:Haxe 3 generalized several APIs of sys-platforms in the sys package. The platform-specific versions from haxe 2 were removed.
修正:Use the platform-independent classes in sys, sys.db sys.io and sys.net instead.

Hash和IntHash类已移除

症状:编译错误 Hash has been removed, use Map instead or IntHash has been removed, use Map instead
解释:Haxe 2 provided Hash and IntHash as toplevel classes. In haxe 3 their implementations was moved to the haxe.ds package and a general-purpose Map type remains in the toplevel.
修正:
Replace Hash with Map, possibly adding an explicit String key if required
Replace IntHash with Map, possibly adding an explicit Int key if required
例程:

// haxe 2
var hash = new Hash();
var intHash = new IntHash();

// haxe 3
var hash = new Map();
var intHash = new Map();

hash.set("foo", 1);
intHash.set(1, 2);

haxe.rtti接口

症状:编译错误 Use @:generic instead of implementing haxe.Generic or Use @:rtti instead of implementing haxe.rtti
解释:Haxe 2 used the special interfaces haxe.rtti.Generic and haxe.rtti.Infos to enable particular behavior. In haxe 3 they have been replaced by @:generic and @:rtti metadata respectively.
修正:
Remove implements haxe.rtti.Generic and add @:generic metadata to the class
Remove implements haxe.rtti.Infos and add @:rtti metadata to the class
例程:

// haxe 2
class GenericClass extends haxe.rtti.Generic { }
class RttiClass extends haxe.rtti.Infos { }

// haxe 3
@:generic class GenericClass { }
@:rtti class RttiClass { }

内联变量的初始化

症状:编译错误 Variable initialization must be a constant value
解释:Haxe 2 allowed arbitrary expressions as initialization to inline variables. This could cause unexpected behavior because expressions with potential side-effects were injected to the place of their access. Haxe 3 restricts the allowed expressions to constant ones, i.e. those that have no side-effects.
修正:Use an inline function returning the value instead.
例程:

// haxe 2
static inline var x = [];

// haxe 3
static inline function x() {
    return [];
}

Int32
症状:编译错误 haxe.Int32 has been removed, use normal Int instead or errors related to readUInt30, readInt31, writeUInt30, writeInt31
解释:Haxe 3 provided the haxe.Int32 Api to enable 32-bit integer operations across all targets. This is no longer necessary for haxe 3, so the Api has been removed.
修正:
replace all occurences of Int32 with Int
replace readUInt30 and readInt31 with readInt32
replace writeUInt30 and writeInt32 with writeInt32

接口语法变化

症状:编译错误 Interfaces cannot implements another interface (use extends instead) or Unexpected ,
解释:In haxe 2 interfaces would use implements to extend other interfaces, and multiple extends and implements declaration were separated by a comma. In haxe 3 interfaces use extends instead and the separating comma is no longer allowed.
修正:
replace implements with extends on interface declarations
remove the comma separating multiple implements and extends declarations
例程:

// haxe 2
interface InterfaceSyntax { }
interface InterfaceSyntax2 implements InterfaceSyntax { }
class InterfaceSyntaxClass extends haxe.Template, implements InterfaceSyntax2 { }

// haxe 3
interface InterfaceSyntax { }
interface InterfaceSyntax2 extends InterfaceSyntax { }
class InterfaceSyntaxClass extends haxe.Template implements InterfaceSyntax2 { }

Macro reification changes

症状:Various compiler errors when using macro reification
解释:The reification syntax of haxe 2 has been changed for haxe 3. While now being more flexible and readable, it likely breaks haxe 2 reification code.
修正:
replace $(value) with $v{value}
例程:

// haxe 2
var v = macro $(1);

// haxe 3
var v = macro $v{1};
Order of import and using
症状:The compiler infers identifiers to the wrong type.
解释:Haxe 2 resolved unknown identifiers by checking the list of imported modules from top to bottom. Haxe 3 checks this list from bottom to top.
修正:Reverse the declarations of import and using in a file that causes problems.

属性的访问器
症状:编译错误 Custom property accessor is no longer supported, please use get/set
解释:In haxe 2 it was possible to use custom names for the getter and setter of a property. Haxe 3 enforces the naming scheme of get_propertyName and set_propertyName for the getter and setter respectively.
修正:Fixing this involves two steps per property:
use get and set in the property declaration, e.g. var prop(get,set):Int;
rename the accessor function to get_propertyName and set_propertyName for the getter and setter respectively
例程:

// haxe 2
var property(getProperty, setProperty):Int;
function getProperty() return 1
function setProperty(i) return 1

// haxe 3
var property(get, set):Int;
function get_property() { return 1; }
function set_property(i) {return 2; }

属性域的创建

症状:编译错误 This field cannot be accessed because it is not a real variable
解释:In haxe 2 properties always had a real field representation, even if they did not need one. In particular, this applied to pure getter/setter properties. Haxe 3 treats properties as real variables only if they have a default or null access.
修正:Add @:isVar to the property in question.
例程:

// haxe 2
var property(get, set):Int;
function get_property() { return property; }
function set_property(i) {return property = i; }

// haxe 3
@:isVar var property(get, set):Int;
function get_property() { return property; }
function set_property(i) {return property = i; }

字符串插值 interpolation

症状:编译错误 Std.format has been removed, use single quote 'string ${escape}' syntax instead
解释:Haxe 2 was using a haxe macro accessible as Std.format() for String interpolation. In haxe 3 single quotes syntax can be used instead.
修正:Remove the call to Std.format and enclose the String by ' instead of ".
例程:

var x = 12;
// haxe 2
var s = Std.format("x equals $x");
// haxe 3
var s = '$x * $x equals ${x * x}';
trace(s); // 12 * 12 equals 144

Switch的变化

症状:Various compiler errors or warnings in a switch expression
解释:By default Haxe 3 uses pattern matching. While this enables a concise and expressive way for many cases, it may introduce a few incompatibilities to the switch expression used in Haxe 2.
修正:Adding -D no-pattern-matching to the command line will disable pattern matching and retain the haxe 2 behavior. Otherwise it might be worthwhile to adjust the case expressions to conform with the new switch specification. This includes:
null patterns are not allowed and should be replaced by a guard-expression: case CTor("foo",x) if (x == null):
Unused variables will generate a warning because they might have a negative impact on the code output. They can be replaced with _ or an identifier starting with _ in case they are still refered to in documentation.
The | operator is used as or-operator and cannot be used to do in-case integer calculation anymore. These should be moved to e.g. an inline variable.
Some rare case-patterns may no longer be supported, in which case they can be replaced with a if .. else if ... else chain.
A compiler error Capture variables must be lower-case may indicate that a type name or enum constructor could not be found. Make sure that the intended type can be resolved.

若干Api变化

General:
haxe.BaseCode -> haxe.crypto.BaseCode
haxe.Md5 -> haxe.crypto.Md5
haxe.SHA1 -> haxe.crypto.Sha1
EReg.customReplace -> EReg.map
StringTools.isEOF -> StringTools.isEof
IntIter -> IntIterator
haxe.Stack -> haxe.CallStack
neko:
neko.zip.Reader -> haxe.zip.Reader
neko.zip.Writer -> haxe.zip.Writer

新的关键字

In order to support abstract types, an abstract keyword has been added to the language. If you use this as an identifier or a package name, the compiler will reject it in haxe3.

Tools for conversion of haxe2 code to haxe3 code.
Simon's library for getting a haxe2 code base running in haxe3 hx2compat, obviously this is always changing until haxe3 is released, this link is just added for developers working with haxe svn nightlies and can be useful as further guide to changes.

 

这篇关于Haxe 2 - Haxe 3迁移指南的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/191089

相关文章

Retrieval-based-Voice-Conversion-WebUI模型构建指南

一、模型介绍 Retrieval-based-Voice-Conversion-WebUI(简称 RVC)模型是一个基于 VITS(Variational Inference with adversarial learning for end-to-end Text-to-Speech)的简单易用的语音转换框架。 具有以下特点 简单易用:RVC 模型通过简单易用的网页界面,使得用户无需深入了

Java 创建图形用户界面(GUI)入门指南(Swing库 JFrame 类)概述

概述 基本概念 Java Swing 的架构 Java Swing 是一个为 Java 设计的 GUI 工具包,是 JAVA 基础类的一部分,基于 Java AWT 构建,提供了一系列轻量级、可定制的图形用户界面(GUI)组件。 与 AWT 相比,Swing 提供了许多比 AWT 更好的屏幕显示元素,更加灵活和可定制,具有更好的跨平台性能。 组件和容器 Java Swing 提供了许多

基于UE5和ROS2的激光雷达+深度RGBD相机小车的仿真指南(五):Blender锥桶建模

前言 本系列教程旨在使用UE5配置一个具备激光雷达+深度摄像机的仿真小车,并使用通过跨平台的方式进行ROS2和UE5仿真的通讯,达到小车自主导航的目的。本教程默认有ROS2导航及其gazebo仿真相关方面基础,Nav2相关的学习教程可以参考本人的其他博客Nav2代价地图实现和原理–Nav2源码解读之CostMap2D(上)-CSDN博客往期教程: 第一期:基于UE5和ROS2的激光雷达+深度RG

如何掌握面向对象编程的四大特性、Lambda 表达式及 I/O 流:全面指南

这里写目录标题 OOP语言的四大特性lambda输入/输出流(I/O流) OOP语言的四大特性 面向对象编程(OOP)是一种编程范式,它通过使用“对象”来组织代码。OOP 的四大特性是封装、继承、多态和抽象。这些特性帮助程序员更好地管理复杂的代码,使程序更易于理解和维护。 类-》实体的抽象类型 实体(属性,行为) -》 ADT(abstract data type) 属性-》成

CentOs7上Mysql快速迁移脚本

因公司业务需要,对原来在/usr/local/mysql/data目录下的数据迁移到/data/local/mysql/mysqlData。 原因是系统盘太小,只有20G,几下就快满了。 参考过几篇文章,基于大神们的思路,我封装成了.sh脚本。 步骤如下: 1) 先修改好/etc/my.cnf,        ##[mysqld]       ##datadir=/data/loc

CentOS下mysql数据库data目录迁移

https://my.oschina.net/u/873762/blog/180388        公司新上线一个资讯网站,独立主机,raid5,lamp架构。由于资讯网是面向小行业,初步估计一两年内访问量压力不大,故,在做服务器系统搭建的时候,只是简单分出一个独立的data区作为数据库和网站程序的专区,其他按照linux的默认分区。apache,mysql,php均使用yum安装(也尝试

Linux Centos 迁移Mysql 数据位置

转自:http://www.tuicool.com/articles/zmqIn2 由于业务量增加导致安装在系统盘(20G)磁盘空间被占满了, 现在进行数据库的迁移. Mysql 是通过 yum 安装的. Centos6.5Mysql5.1 yum 安装的 mysql 服务 查看 mysql 的安装路径 执行查询 SQL show variables like

使用条件变量实现线程同步:C++实战指南

使用条件变量实现线程同步:C++实战指南 在多线程编程中,线程同步是确保程序正确性和稳定性的关键。条件变量(condition variable)是一种强大的同步原语,用于在线程之间进行协调,避免数据竞争和死锁。本文将详细介绍如何在C++中使用条件变量实现线程同步,并提供完整的代码示例和详细的解释。 什么是条件变量? 条件变量是一种同步机制,允许线程在某个条件满足之前进入等待状态,并在条件满

Java 入门指南:Java 并发编程 —— 并发容器 ConcurrentLinkedDeque

文章目录 ConcurrentLinkedDeque特点构造方法常用方法使用示例注意事项 ConcurrentLinkedDeque ConcurrentLinkedDeque 是 Java 并发工具包(java.util.concurrent 包)中的一个线程安全的双端队列(Deque)实现,实现了 Deque 接口。它使用了链表结构,并且针对高并发环境进行了优化,非常适合

使用Nginx部署前端Vue项目的详细指南

在本文中,我们将详细介绍如何使用Nginx部署一个前端Vue项目。此过程涵盖Vue项目的构建、Nginx的安装与配置、以及最后的项目启动。下面是步骤的详细说明。 步骤 1: 准备你的Vue项目 确保你已经创建并构建了一个Vue项目。如果你尚未创建Vue项目,可以使用以下命令创建一个: # 安装Vue CLInpm install -g @vue/cli# 创建一个新的Vue项目vue c