绝妙的“def”声明翻译成C语言?

发布于 2024-09-11 08:58:13 字数 677 浏览 8 评论 0原文

我知道这听起来完全荒谬,但相信我,我想要像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

动听の歌 2024-09-18 08:58:13

您要求使用静态类型语言进行动态类型。 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.

她如夕阳 2024-09-18 08:58:13

在 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.

睫毛上残留的泪 2024-09-18 08:58:13

基本上,你不应该在 C 中这样做。但是,根据我的理解,如果你愿意,你可以将你的变量声明为 void*。您需要将其转换回适当的数据类型才能使用它。自从我编写 C 以来已经有一段时间了,因此以下语法可能不是 100% 正确的语法,但您可以这样做:

void* variable = "12345";
printf((char*) variable);

这种方法的缺点是您需要知道变量的真正类型才能将其强制转换回正确的类型,如果你输入错误,你的应用程序将会崩溃。据我了解,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:

void* variable = "12345";
printf((char*) variable);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文