Is there a compiled programming language with dynamic, maybe even weak typing?
Asked Answered
P

11

49

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?

Pavis answered 31/3, 2010 at 17:24 Comment(9)
@Robert Harvey: That's compiled to machine code? I thought it was compiled to bytecode first.Splashboard
@Platinum: You can get machine code using NGen.Bohannan
@Robert Harvey: Aw come on, that doesn't count. That's not intrinsic to the language. At this rate I could claim Java is a compiled language too, since there are .exe wrappers available for it as well. And why not a scripted language while I'm at it, surely there are conversion programs that will take a script and generate a binary as well?Splashboard
@Platinum: Not sure I get your point. The machine code you get from C# and NGen might actually be of higher quality than some of the direct-to-machine-code native code compilers out there. The jitter is capable of some pretty substantial optimizations. A script to exe hack can't even be remotely considered to be the same thing.Bohannan
@Robert Harvey: What the hell? Compiled languages are not defined by how optimized they are, but whether or not they are converted to machine code before being executed directly by the machine. C# does not fit this definition, as it is "compiled" into an intermediate bytecode, which is then interpreted by, yes, a just-in-time compiler. But it is still interpreted code. Same as Java. Different from C or C++ or other, actual compiled languages.Splashboard
@Platinum: I don't think so. Read up on NGen here: msdn.microsoft.com/en-us/library/ht8ecch6.aspx. NGen produces code that can be executed by the processor directly. No bytecode interpreter is required.Bohannan
@Robert Harvey: I'm a little hurt that you would strawman me like this. Read carefully: I didn't say NGen did not generate machine code. And that's not my issue anyway. My issue is that you have to use it at all, as one of MULTIPLE steps required to take C# code down to machine language. C# compiles to an intermediate language. The compiled machine code is not intrinsic to the language. In that sense it is EXACTLY like a script-to-.exe tool, hackish as those tools might be.Splashboard
@Platinum Azure - Every compiled programming language requires mulitple steps from source to executable, but the real problem with your second statement was the use of the word "interpreted". In the programming languages community, "interpreted" has a specific meaning that was valid for early versions of Java, but has never been valid for any version of C#. C# is a compiled language, regardless of whatever phases it passes through to get there.Tanning
I can't recommend a better example than Julia. It's most popular language that fits your descriptionCastara
L
37

I believe Lisp fits that description.

http://en.wikipedia.org/wiki/Common_Lisp

Lashaun answered 31/3, 2010 at 17:32 Comment(0)
S
7

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

Saintsimonianism answered 9/3, 2016 at 4:26 Comment(3)
What would the difference between compiled at runtime and interpreted languages be?Aloeswood
thats an apples/oranges comparison. runtime compilation (jit compilation) is a method interpreters use at runtime to improve performance. an interpreted language doesn't necessarily have to be jit compiled, but basically all runtimes will.Twana
Yaa Julia is great example of that. Also it has multiple dispatch.Castara
F
6

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 a void*, can be endowed with interfaces that specify a contract that the (otherwise unknown) type will adhere to.

Fail answered 31/3, 2010 at 17:32 Comment(0)
B
6

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/

Bohannan answered 31/3, 2010 at 17:33 Comment(1)
Technically this is incorrect. All that C# dynamic does is provide syntactic sugar over using Object as the type.Bridgework
R
4

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.

Rackley answered 31/3, 2010 at 17:33 Comment(0)
P
3

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

Poohpooh answered 31/10, 2013 at 4:52 Comment(0)
C
2

VB 6 has most of that

Coactive answered 31/3, 2010 at 17:24 Comment(2)
...but doesn't really compile to machine code (compiles to p-code).Bohannan
The default setting is to compile to native code. (You can change this in the project properties.)Coactive
H
1

I don't know of any language that has exactly those capabilities. I can think of two that have a significant subset, though:

  • D has type inference, garbage collection, and powerful metaprogramming facilities, yet compiles to efficient machine code. It does not have dynamic typing, however.
  • C# can be compiled directly to machine code via the mono project. C# has a similar feature set to D, but again without dynamic typing.
Heliochrome answered 31/3, 2010 at 17:33 Comment(0)
M
1

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.

Mortgage answered 23/5, 2022 at 12:18 Comment(0)
S
1

C/C++ both indirectly support dynamic typing using void*. C++ example:

#include <string>

int main() {
    void* x = malloc(sizeof(int))
    *(int*)x = 5;
    x = malloc(sizeof(std::string));
    *(std::string*x) = std::string("Hello world");
    free(x);
    return 0;
}

In C++17, std::any can be used as well:

#include <string>
#include <any>

int main() {
    std::any x = 5;
    x = std::string("Hello world");
    return 0;
}

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.

Scat answered 18/1, 2023 at 3:23 Comment(0)
E
0

Python to C probably needs these criteria.

  1. Write in Python.

  2. Compile Python to Executable. See Process to convert simple Python script into Windows executable. Also see Writing code translator from Python to C?

Eclat answered 31/3, 2010 at 17:58 Comment(5)
Aren't you talking about/aren't all these "compilers" linkers that just package the python binaries together with the script in one executable?Pavis
@sub: how is "packaging binaries ... in one executable" not a compiler? Please provide some definition that excludes creating one executable from source. And some do translate into C before compiling the binary, FWIW.Eclat
no it isn't. A compiler converts code to computer-readable binaries. Py2exe(and other systems) merely ships the Python executable alongside the Python script, thus, the original code still remains Python, and remains only executable by the Python executable. But there exist variations of the Python language that allow compilation to C, but it's never the original Python(like Cython)Cataract
@Manux: After I run py2exe, I have an .exe which is directly executable by windows. Correct? How does this fail to meet the criteria as stated in the question? What's important is not your understanding of compilation. What's important is that the question is vague, and a lot of solutions are possible.Eclat
@Eclat because It's packaged not compiledFarrahfarrand

© 2022 - 2024 — McMap. All rights reserved.