是否有一种具有动态甚至弱类型的编译编程语言?
我想知道是否有一种编程语言可以编译为具有动态和/或弱类型功能的机器代码/二进制文件(不是由虚拟机执行的字节码,在考虑类型时这是完全不同的),例如:考虑
一种编译语言,其中:
- 变量不需要声明
- 变量可以在运行时创建
- 函数可以返回不同类型的值
问题:
- 有这样的编程语言吗?
- 为什么/为什么不?
我认为动态但强类型的编译语言确实有意义,但这可能吗?
I wondered if there is a programming language which compiles to machine code/binary that features dynamic and/or weak typing (not bytecode then executed by a VM, that's something completely different when considering typing), e.g:
Think of a compiled language where:
- Variables don't need to be declared
- Variables can be created during runtime
- Functions can return values of different types
Questions:
- Is there such a programming language?
- Why / Why not?
I think that a dynamically yet strong typed, compiled language would really sense, but is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我不知道有哪种语言具有这些功能。不过,我可以想到两个具有重要子集的东西:
I don't know of any language that has exactly those capabilities. I can think of two that have a significant subset, though:
Elixir 就是这样做的。动态变量类型的灵活性有助于进行热代码更新(Erlang 就是为此设计的)。文件被编译为在 BEAM(Erlang/Elixir VM)上运行。
Elixir does this. The flexibility of dynamic variable typing helps with doing hot-code updates (for which Erlang was designed). Files are compiled to run on the BEAM, the Erlang/Elixir VM.
C/C++ 都使用
void*
间接支持动态类型。 C++ 示例:在 C++17 中,也可以使用
std::any
:当然,鸭子类型在 C/C++ 中很少使用或需要,并且这两个选项都有问题 (< code>void* 是不安全的,
std::any
是一个巨大的性能瓶颈)。您可能正在寻找的另一个示例是 JavaScript 的 V8 引擎。它是一个 JIT 编译器,这意味着源代码被编译为字节码,然后在运行时编译为机器代码,尽管这是对用户隐藏的。
C/C++ both indirectly support dynamic typing using
void*
. C++ example:In C++17,
std::any
can be used as well:Of course, duck typing is rarely used or needed in C/C++, and both of these options have issues (
void*
is unsafe,std::any
is a huge performance bottleneck).Another example of what you may be looking for is the V8 engine for JavaScript. It is a JIT compiler, meaning the source code is compiled to bytecode and then machine code at runtime, although this is hidden from the user.
Python 到 C 可能需要这些标准。
用Python编写。
将 Python 编译为可执行文件。请参阅将简单 Python 脚本转换为 Windows 可执行文件的过程 。另请参阅编写从 Python 到 C 的代码转换器?
Python to C probably needs these criteria.
Write in Python.
Compile Python to Executable. See Process to convert simple Python script into Windows executable. Also see Writing code translator from Python to C?
我相信 Lisp 符合这个描述。
http://en.wikipedia.org/wiki/Common_Lisp
I believe Lisp fits that description.
http://en.wikipedia.org/wiki/Common_Lisp
是的,这是可能的。请参阅Julia。它是一种动态语言(您可以编写没有类型的程序),但它永远不会在虚拟机上运行。它在运行时将程序编译为本机代码(JIT 编译)。
Yes, it is possible. See Julia. It is a dynamic language (you can write programs without types) but it never runs on a VM. It compiles the program to native code at runtime (JIT compilation).
Objective-C 可能具有您想要的一些属性。类可以在运行时打开和更改,并且您可以向对象发送任何类型的消息,无论它通常是否响应。通过这种方式,您可以实现鸭子类型,就像在 Ruby 中一样。类型
id
大致相当于void*
,可以赋予指定(否则未知)类型将遵守的约定的接口。Objective-C might have some of the properties you seek. Classes can be opened and altered in runtime, and you can send any kind of message to an object, whether it usually responds to it or not. In that way, you can implement duck typing, much like in Ruby. The type
id
, roughly equivalent to avoid*
, can be endowed with interfaces that specify a contract that the (otherwise unknown) type will adhere to.C# 4.0 具有许多(如果不是全部)这些特性。如果您确实想要本机机器代码,可以将字节码编译为机器代码 使用实用程序。
特别是,使用
dynamic
关键字允许对象及其成员在运行时动态绑定。查看 Anders Hejlsberg 的视频《C# 的未来》,了解入门知识:
http: //channel9.msdn.com/pdc2008/TL16/
C# 4.0 has many, if not all of these characteristics. If you really want native machine code, you can compile the bytecode down to machine code using a utility.
In particular, the use of the
dynamic
keyword allows objects and their members to be bound dynamically at runtime.Check out Anders Hejlsberg's video, The Future of C#, for a primer:
http://channel9.msdn.com/pdc2008/TL16/
Objective-C 具有您提到的许多功能:它编译为机器代码,并且针对对象实例有效地动态类型化。
id
类型可以存储任何类实例,Objective-C 使用消息传递而不是成员函数调用。可以在运行时创建/添加方法。 Objective-C运行时也可以在运行时合成类实例变量,但局部变量仍然需要声明(就像在C中一样)。C# 4.0 具有许多此类功能,只不过它被编译为 IL(字节码)并使用虚拟机(CLR)进行解释。然而,这提出了一个有趣的观点:如果字节码被及时编译为机器代码,这算不算?如果是这样,它不仅打开了通往任何 .Net 语言的大门,还打开了通往 Python 的大门(请参阅 PyPy 或 Unladed Swallow 或 IronPython)和 Ruby(请参阅 MacRuby 或 IronRuby)和许多其他动态类型语言,更不用说许多 LISP 变体了。
Objective-C has many of the features you mention: it compiles to machine code and is effectively dynamically typed with respect to object instances. The
id
type can store any class instance and Objective-C uses message passing instead of member function calls. Methods can be created/added at runtime. The Objective-C runtime can also synthesize class instance variables at runtime, but local variables still need to be declared (just as in C).C# 4.0 has many of these features, except that it is compiled to IL (bytecode) and interpreted using a virtual machine (the CLR). This brings up an interesting point, however: if bytecode is just-in-time compiled to machine code, does that count? If so, it opens to the door to not only any of the .Net languages, but Python (see PyPy or Unladed Swallow or IronPython) and Ruby (see MacRuby or IronRuby) and many other dynamically typed languages, not mention many LISP variants.
与 Lisp 类似,有 Factor,一种无变量的连接*语言默认、动态类型和灵活的对象系统。 Factor 代码可以在交互式解释器中运行,或者使用其
deploy
函数编译为本机可执行文件。* 基于无点函数堆栈
In a similar vein to Lisp, there is Factor, a concatenative* language with no variables by default, dynamic typing, and a flexible object system. Factor code can be run in the interactive interpreter, or compiled to a native executable using its
deploy
function.* point-free functional stack-based
VB 6 拥有大部分功能
VB 6 has most of that