sub
sub

Reputation: 2733

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

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:

Questions:

I think that a dynamically yet strong typed, compiled language would really sense, but is it possible?

Upvotes: 49

Views: 15963

Answers (11)

Nirvana
Nirvana

Reputation: 633

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.

Upvotes: 1

Everett
Everett

Reputation: 9568

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.

Upvotes: 2

RoboAlex
RoboAlex

Reputation: 5015

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

Upvotes: 7

Jon Purdy
Jon Purdy

Reputation: 54981

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

Upvotes: 3

S.Lott
S.Lott

Reputation: 391846

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?

Upvotes: 0

Robert Harvey
Robert Harvey

Reputation: 180797

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/

Upvotes: 6

Svante Svenson
Svante Svenson

Reputation: 12488

VB 6 has most of that

Upvotes: 2

Barry Wark
Barry Wark

Reputation: 107754

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.

Upvotes: 4

JSBձոգչ
JSBձոգչ

Reputation: 41378

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.

Upvotes: 1

Stephen
Stephen

Reputation: 49156

I believe Lisp fits that description.

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

Upvotes: 37

Jakob Borg
Jakob Borg

Reputation: 24435

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.

Upvotes: 6

Related Questions