23

Although C++0x is quite an improvement to C++ (type inference, anonymous functions, and so on), I have to say that Scala seems even better. The thing is that Scala only runs on the JVM, although it seems like it can also run on top of C#.

Ideally, I would like a language as nice as Scala, but running "on top of" C++ -- using the standard libraries, easily linking against C/C++ object files, the whole deal. I do a lot of numerical programming built of top of well established C/C++ libraries (fast and reliable), and that is not something I can walk away from.

Is anyone of aware of such a language?

Update:

The features I am looking for are:

  • Seamless integration with C/C++ libraries, just like Scala can access Java libraries without the need for bindings to be generated/maintained

  • A strong type system, with a well designed type inference system that keeps me from having to write verbose and redundant type annotations

  • Functional and OO features built into the language, with the support of its own libraries instead of only relying on the standard C/C++ libraries.

It seems like a lot of the clang/LLVM work being done right now may facilitate work along these lines, but it would be nice to find that something like this is already being worked on.

Marcus P S
  • 871
  • 10
  • 16
  • 5
    What features of Scala do you find appealing? – James McNellis Dec 29 '10 at 02:19
  • 3
    Virtually all languages can link with C code, e.g. C#, Python, Delphi, Ruby, etc., and you rarely have libraries that only expose a C++ API anyway (because that would basically restrict the library to only be used from C++, and nothing else). I'm not entirely sure what you're asking for beyond that. – Michael Madsen Dec 29 '10 at 02:24
  • 1
    How is this programming related? I mean, where is the code? Voted to move it to programmers.stackexchange.com – OscarRyz Dec 29 '10 at 04:16
  • 1
    You haven't really specified anything that isn't already in C++. Too vague to be answered. – jalf Dec 29 '10 at 09:01
  • 4
    For what it's worth though, "As Java is to Scala, C++ is to a skateboard". That's about as meaningful an answer as you're going to get. – jalf Dec 29 '10 at 11:17
  • I'm guessing that a better standard library for C++ would suit your purposes (say something where the functional programming style isn't burdened by the need to pass external iterators everywhere), and that a better language isn't actually going to accomplish a whole lot. – Ken Bloom Dec 29 '10 at 16:48
  • @jalf: C++ isn't a strong type system the way Scala is. In C++, templates are basically duck typing (i.e. dynamic typing). Though Scala allows that to an extent (through structural types), Scala does a much better job with things like checking the requirements proactively at compile time, and allowing covariant and contravariant generics. – Ken Bloom Dec 29 '10 at 16:52
  • @Ken: yes, there are many differences between all three languages. Which is why this question is impossibly vague. Anything built on top of Java (such as Scala) is going to attempt to solve a very different set of problems than something built on top of C++. – jalf Dec 31 '10 at 16:07
  • @KenBloom: template arguments are duck-typed *statically*, which is what makes templates acceptable in C++. – Fred Foo Apr 06 '12 at 10:21
  • @larsmans: that's true, but how difficult should template errors be to interpret. – Ken Bloom Apr 06 '12 at 14:57
  • 1
    @KenBloom: difficult template errors are not inherent to C++. See [Clang](http://clang.llvm.org/) – Mooing Duck Apr 12 '12 at 22:03
  • From what I have seen, Scala is another attempt to resurrect LISP semantics. Much like JavaScript. Nothing against any of them. – brian beuning Dec 11 '12 at 23:06

12 Answers12

21

I wonder why no one has yet mentioned the D programming language. It is a perfect fit for your requirements.

Madoc
  • 5,841
  • 4
  • 25
  • 38
  • @Marcus: no more than Scala is. It doesn't have an ISO standard or anything, and they occasionally the write new versions of the language (D 1.0 and D 2.0), but there is a GPL'ed GCC frontend for D. – Ken Bloom Dec 29 '10 at 16:39
  • @Ken: Regarding ISO standards - neither does Java for that matter. – Clifford Dec 31 '10 at 13:46
  • @kirill_igum That's like saying that Scala doesn't have as many libraries as Java... – Madoc Apr 12 '12 at 17:10
  • 1
    @Madoc D has as many libraries as C. It does work with some c++. But D and C++ templates don't work together http://dlang.org/cpp_interface.html I couldn't find a way to use, for example, boost.odeint in D with custom states. If D had a mechanism to support c++ templates, i'd jump today. scientific libraries is the only thing that stops me from D. – kirill_igum Apr 12 '12 at 18:11
  • @kirill_igum: templates are a compile-time macro kind of thing. You won't find other languages supporting C++ templates, because to use them, you NEED a C++ compiler implementation. Your only options are: 1) C++; 2) a language which is backwards-compatible with C++; 3) a language that does what C++ templates do, but in its own way; or 4) a language that can link to some C++ code that wraps C++ templates, but doesn't expose them. D provides 3 and 4. AFAIK, the only way to get 2 is with 1. –  Jun 30 '12 at 15:34
11

You might want to think about Haskell. It has as much niceness as any language out there (in its own way, of course), but it is ardent in its adherence to functional programming so the barrier to learning is substantial. Still, it can be used to call C/C++, and it's surprisingly high-performance on its own.

You also should decide whether you need the language itself to be capable of fairly high-performance computing. If not, it's pretty easy to create Python bindings to C/C++, and Python has quite a few nice features. Or you could use something like Matlab, which is designed for numeric computing as I'm sure you well know, and can integrate with C code pretty well via mex files. If you do need the language to be one in which you can currently write high-performance code, you might even consider taking on the nontrivial task of creating C++ bindings for Scala (it's not that bad if you use JNI or JNA for Java, and then call that Java from Scala), and then only use that for the most important numeric work while Scala handles the less time-critical (but still somewhat important) parts.

Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
  • Haskell looks interesting in its own right, and I have heard great things about its performance. However, the interoperability with C/C++ is not all that seamless. The other alternatives you mentioned suffer from similar problems -- nothing major for general use, but not as nice as the Scala/Java integration. – Marcus P S Dec 29 '10 at 16:22
  • 2
    Very little is as nice as Scala/Java integration, I'm afraid. – Rex Kerr Dec 30 '10 at 01:35
6

It's hard to write a language that's compatible with C++, becuase there are so many features, and the ABI is generally considered to be specific to a particular version of the C++ compiler. If you're really looking for easy integration with C++ I'd venture that your best bet is to use Scala with GCJ. You can use the GCJ CNI to integrate with your C++ objects (though suggesting the GCJ CNI is grounds for a downvote around here.)

Ken Bloom
  • 57,498
  • 14
  • 111
  • 168
5

How about Scala itself, with access to your C++ libraries through SWIG-generated Java bindings?

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • SWIG sounds a lot better than dealing with JNI directly, that is for sure. Something to look into, even if it is not the perfect solution. – Marcus P S Jan 03 '11 at 05:38
  • Java Foreign Function and Memory API is a good alternative to JNI. – exaucae Jul 21 '23 at 10:36
5

I'm surprised nobody mentioned D. It is close to the C++-syntax but much "cleaner", can use C and C++ libs without problems and adds a lot of advanced features. Sure, the step is not as big as from Java to Scala, but I think given C++'s complexity it is much harder to come up with a really innovative design that doesn't throw most parts of C++ away.

Landei
  • 54,104
  • 13
  • 100
  • 195
4

Well, Scala is an object-oriented and functional programming language. If you are looking for a modern object-oriented and functional programming language that can also call C code through a foreign function interface, then you might be interested in OCaml.

Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
3

Rust is looking like a better and better candidate.

Marcus P S
  • 871
  • 10
  • 16
1

Have a look :

D Programming Language

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
1

I'm looking for the same thing. Lua and D are two options. lua has a command line interpreter, luajit is fast, and for math I use gsl shell. D is more sophisticated but still simple to use. D is also as fast as C++.

kirill_igum
  • 3,953
  • 5
  • 47
  • 73
1

It's been a few years since this question was posted, but C++14 is probably the right answer now. C++11/14 add type inference, lambda functions, and various other features akin to those Scala added over Java. Other realistic contenders for a C++98/03 replacement are Go and Rust.

# Makefile
CXX = clang++
CXXFLAGS = -std=c++1y -stdlib=libc++
LDFLAGS = -lc++

main: main.cpp
    $(CXX) -o $@ $(CPPFLAGS) $(CXXFLAGS) $< $(LDFLAGS)
Jeff Schwab
  • 613
  • 6
  • 16
1

What about Python with C++ bindings?

See here.

Otávio Décio
  • 73,752
  • 17
  • 161
  • 228
1

How about Scala + JNA? Basically, JNA is a library that allows you to declare interfaces (or traits in Scala) that match DLL's.

Anonymous
  • 821
  • 1
  • 5
  • 14
  • JNA seems nice, but performance is quite bad, from what I gather (a lot of overhead). JNI, although fast, looks hellish to use. – Marcus P S Jan 03 '11 at 05:34
  • If this line of reasoning is useful to your use case look at https://code.google.com/p/bridj/ – Langley May 24 '13 at 13:25