'never' 无法在构造函数中设置属性
'never' properties cannot be set inside the counstructor
所以,问题在标题中。我使用 'never' 关键字在我的 类 中声明了一些属性,因此我可以在构造函数中只设置一次这些属性的值。但是,我收到以下错误:
无法访问用于写入的字段或标识符 %name%
有问题的代码示例:
class TreeAbility
{
public var id(default, never):String;
public var maxLvl(default, never):Int;
public function new(id:String, maxLvl:Int)
{
Assert.assert(maxLvl > 0);
this.id = id; (*)
this.maxLvl = maxLvl; (*)
this.currentLvl = 0;
}
}
标有(*)的行抛出访问错误
我相信 never 写 属性 意味着 writing/setting 永远不允许使用变量,即使在构造函数中也是如此。参见:https://haxe.org/manual/class-field-property.html
也许您正在寻找 Haxe 4 中出现的 final 关键字。例如字段,它只允许从 class 构造函数赋值给变量.此处确认:https://haxe.org/download/version/4.0.0-preview.2/ and https://github.com/HaxeFoundation/haxe/issues/6584
所以,问题在标题中。我使用 'never' 关键字在我的 类 中声明了一些属性,因此我可以在构造函数中只设置一次这些属性的值。但是,我收到以下错误:
无法访问用于写入的字段或标识符 %name%
有问题的代码示例:
class TreeAbility
{
public var id(default, never):String;
public var maxLvl(default, never):Int;
public function new(id:String, maxLvl:Int)
{
Assert.assert(maxLvl > 0);
this.id = id; (*)
this.maxLvl = maxLvl; (*)
this.currentLvl = 0;
}
}
标有(*)的行抛出访问错误
我相信 never 写 属性 意味着 writing/setting 永远不允许使用变量,即使在构造函数中也是如此。参见:https://haxe.org/manual/class-field-property.html
也许您正在寻找 Haxe 4 中出现的 final 关键字。例如字段,它只允许从 class 构造函数赋值给变量.此处确认:https://haxe.org/download/version/4.0.0-preview.2/ and https://github.com/HaxeFoundation/haxe/issues/6584