绝妙的“def”声明翻译成C语言?
我知道这听起来完全荒谬,但相信我,我想要像 php 中的“$variable”或 groovy 中的“def”这样的东西,通过我的方法是自动变量“数据类型”识别实现< /strong> 转换为 C 语言。
例如:
“def”是类型名称的替换。在变量定义中,它用于指示您不关心类型。在变量定义中,必须显式提供类型名称或使用“def”进行替换。这是使 Groovy 解析器可检测变量定义所必需的。
def dynamic = 1
dynamic = "I am a String stored in a variable of dynamic type"
或者
让我们尝试创建一个包含字符串的变量和一个包含数字的变量:
<?php
$txt="Hello World!";
$x=16;
?>
PHP 是一种松散类型语言 在 PHP 中,在向变量添加值之前不需要声明变量。 在上面的示例中,您会发现不必告诉 PHP 变量的数据类型。 PHP 根据变量的值自动将变量转换为正确的数据类型。 在强类型编程语言中,您必须在使用变量之前声明(定义)变量的类型和名称。
i know this sounds totally ridiculous at the moment but trust me, i want something like "$variable" in php or "def" in groovy, by means of my approach is an automatic variable "data type" identification to IMPLEMENT into c language.
for example:
"def" is a replacement for a type name. In variable definitions it is used to indicate that you don't care about the type. In variable definitions it is mandatory to either provide a type name explicitly or to use "def" in replacement. This is needed to the make variable definitions detectable for the Groovy parser.
def dynamic = 1
dynamic = "I am a String stored in a variable of dynamic type"
OR
Let's try creating a variable containing a string, and a variable containing a number:
<?php
$txt="Hello World!";
$x=16;
?>
PHP is a Loosely Typed Language
In PHP, a variable does not need to be declared before adding a value to it.
In the example above, you see that you do not have to tell PHP which data type the variable is.
PHP automatically converts the variable to the correct data type, depending on its value.
In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您要求使用静态类型语言进行动态类型。 C 既没有动态类型,也没有类型推断,因此不存在。使用 C 实现标记类型系统是完全可能的,并且这是由数百种带有 C 解释器的语言完成的,例如 Python、PHP、Perl 等 - 但就 C 而言,一切仍然是静态类型的。然而,C 并不是那么强类型,因为您可以在不转换实际数据的情况下进行指针转换。
如果您想要一种类似于 C、编译为机器代码并具有类型推断的语言,可以使用 D 及其 auto关键字的重新解释。
You're asking for dynamic typing in a statically typed language. C has neither dynamic typing nor type inference, so this doesn't exist. It's quite possible to implement a tagged type system using C, and this is done by hundreds of languages with C interpreters, such as Python, PHP, Perl and so on - but as far as C is concerned, everything is still statically typed. C is not all that strongly typed, however, as you can cast pointers about without converting actual data.
If you want a language that resembles C, compiles to machine code, and has type inference, there's D with its reinterpretation of the auto keyword.
在 C 中没有办法做你想做的事。你需要在声明变量之前知道它的类型,并且需要在使用变量之前声明它。
There is no way of doing what you want in C. You need to know the type of a variable before you declare it, and you need to declare a variable before you use it.
基本上,你不应该在 C 中这样做。但是,根据我的理解,如果你愿意,你可以将你的变量声明为 void*。您需要将其转换回适当的数据类型才能使用它。自从我编写 C 以来已经有一段时间了,因此以下语法可能不是 100% 正确的语法,但您可以这样做:
这种方法的缺点是您需要知道变量的真正类型才能将其强制转换回正确的类型,如果你输入错误,你的应用程序将会崩溃。据我了解,Objective-C 中的 id 类型基本上是一个包含 void* 类型和一些运行时类型信息的结构,但它只能指向 Objective-C 类,因此转换是安全的。
Basically, you shouldn't do this in C. But, from what I understand, if you want to you can by declaring your variable as a void*. You will need to cast it back to the appropriate datatype in order to use it. It's been a little while since I wrote C so the following may not be 100% correct syntax but you could do something like this:
The downside to this approach is that you need to know what type the variable really is to cast it back to the correct type and if you get it wrong your application will come crashing down. From what I understand the id type in Objective-C is basically a struct that includes a void* type and some runtime type information but it can only point to objective-c classes so casting will be safe.