简体   繁体   English

如何在Lua中用类型声明变量

[英]How to declare variables with a type in Lua

Is it possible to create variables to be a specific type in Lua? 是否可以在Lua中将变量创建为特定类型?

Eg int x = 4 例如, int x = 4

If this is not possible, is there at least some way to have a fake "type" shown before the variable so that anyone reading the code will know what type the variable is supposed to be? 如果不可能,那么是否至少有某种方法可以在变量前显示一个假的“类型”,以便任何阅读代码的人都可以知道该变量应该是什么类型?

Eg function addInt(int x=4, int y=5) , but x/y could still be any type of variable? 例如function addInt(int x=4, int y=5) ,但是x / y仍然可以是任何类型的变量吗? I find it much easier to type the variable's type before it rather than putting a comment at above the function to let any readers know what type of variable it is supposed to be. 我发现在变量之前键入变量的类型要容易得多,而不是在函数上方放置注释以使任何读者知道它应该是什么类型的变量。

The sole reason I'm asking isn't to limit the variable to a specific data type, but simply to have the ability to put a data type before the variable, whether it does anything or not, to let the reader know what type of variable that it is supposed to be without getting an error. 我要问的唯一原因不是将变量限制为特定的数据类型,而是仅仅具有将数据类型放在变量之前的能力(无论是否执行任何操作),以使读者知道哪种类型的数据。应该没有错误的变量。

You can do this using comments: 您可以使用注释来做到这一点:

local x = 4 -- int

function addInt(x --[[int]],
                y --[[int]] )

You can make the syntax a = int(5) from your other comment work using the following: 您可以使用以下命令使其他注释工作的语法为a = int(5)

function int(a) return a end
function string(a) return a end
function dictionary(a) return a end

a = int(5)
b = string "hello, world!"
c = dictionary({foo = "hey"})

Still, this doesn't really offer any benefits over a comment. 不过,与评论相比,这实际上并没有提供任何好处。

The only way I can think of to do this, would be by creating a custom type in C. 我能想到的唯一方法是在C中创建一个自定义类型。

Lua Integer type Lua整数类型

No. But I understand your goal is to improve understanding when reading and writing functions calls. 否。但是我了解您的目标是增进对读写函数调用的理解。

Stating the expected data type of parameters adds only a little in terms of giving a specification for the function. 陈述参数的预期数据类型仅在为函数指定规格方面增加了一点。 Also, some function parameters are polymorphic, accepting a specific value, or a function or table from which to obtain the value for a context in which the function operates. 另外,某些函数参数是多态的,接受特定值,或者是从函数或表中获取函数在其中运行的上下文的值。 See string.gsub , for example. 例如,参见string.gsub

When reading a function call, the only thing known at the call site is the name of the variable or field whose value is being invoked as a function (sometimes read as the "name" of the function) and the expressions being passed as actual parameters. 读取函数调用时,在调用站点上唯一知道的是变量或字段的名称,其值作为函数被调用(有时被读取为函数的“名称”),而表达式则作为实际参数传递。 It is sometimes helpful to refactor parameter expressions into named local variables to add to the readability. 将参数表达式重构为命名的局部变量有时会很有帮助,以增加可读性。

When writing a function call, the name of the function is key. 编写函数调用时,函数的名称为key。 The names of the formal parameters are also helpful. 形式参数的名称也很有用。 But still, names (like types) do not comprise much of a specification. 但是,名称(如类型)仍然没有包含很多规范。 The most help comes from embedded structured documentation used in conjunction with an IDE that infers the context of a name and performs content assistance and presentations of available documentation. 最大的帮助来自与IDE结合使用的嵌入式结构化文档,该文档可推断名称的上下文并执行内容帮助和可用文档的演示。

luadoc is one such a system of documentation. luadoc是这样一种文档系统。 You can write luadoc for function you declare. 您可以为声明的函数编写luadoc。

Eclipse Koneki LDT is one such an IDE. Eclipse Koneki LDT就是这样一种IDE。 Due to the dynamic nature of Lua, it is a difficult problem so LDT is not always as helpful as one would like. 由于Lua的动态特性,这是一个难题,因此LDT并不总是像人们希望的那样有用。 (To be clear, LDT does not use luadoc; It evolved its own embedded documentation system.) (很明显,LDT不使用luadoc;它发展了自己的嵌入式文档系统。)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM