views:

378

answers:

8

I wondered if there is a programming language which compiles to machine code/binary (not bytecode then executed by a VM, that's something completely different when considering typing) that features dynamic and/or weak typing, e.g:

Think of a compiled language where:

  • Variables don't need to be declared
  • Variables can be created doing runtime
  • Functions can return values of different types

Questions:

  • Is there such a programming language?
  • (Why) not?

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

A: 

I'm working on a language called Prog with all of those goals in mind. It's still in the early stages, but I think it has a lot of potential. I don't know of any existing languages that take quite the same approach, but naturally I'm biased.

Jon Purdy
If you give it a more creative name I'll try it ;D
sub
Haha...well, thanks! Though you must love its simplicity: Prog is for programming. Then again, I'm also fond of brevity and Unixisms. :P
Jon Purdy
Wow, sounds really interesting... I'll keep an eye one that
Helper Method
+3  A: 

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.

calmh
+12  A: 

I believe Lisp fits that description.

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

Stephen
+4  A: 

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/

Robert Harvey
+1  A: 

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.
JSBangs
+2  A: 

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.

Barry Wark
+1  A: 

VB 6 has most of that

svinto
...but doesn't really compile to machine code (compiles to p-code).
Robert Harvey
The default setting is to compile to native code. (You can change this in the project properties.)
svinto
+1  A: 

Python to C probably needs these criteria.

  1. Write in Python.

  2. Compile Python to Executable. See http://stackoverflow.com/questions/2136837/process-to-convert-simple-python-script-into-windows-executable. Also see http://stackoverflow.com/questions/2525518/writing-code-translator-from-python-to-c

S.Lott
Aren't you talking about/aren't all these "compilers" linkers that just package the python binaries together with the script in one executable?
sub
@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.
S.Lott
@S.Lott, 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)
Manux
@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.
S.Lott