5

For fans of Scala's strict type system, but fans of C++:

  • Is it possible to force yourself to program C++ with the same strictness? (not using void*, not casting a lot, boxing simple value type ie struct Month {int value;}; ).
  • Or is C++ by default even stricter then Scala (if you do not try hard with casts)? Despite the "duck typing" of C++ templates -- it still won't let you compile if it doesn't fit, right?
  • Does C++0x (wrt to C++03) add something for someone who is willing to submit oneself to totally typesafe programming?
towi
  • 21,587
  • 28
  • 106
  • 187

2 Answers2

6

Short answer: yes, you can reach the same level of type safety, but it will place a burden on the programmer. It's not simply a question of providing the same safety, the type system must also be powerful and flexible enough to facilitate typesafe programming and in this regard Scalas type system is superior to C++.

C++0x will add lambda expressions which will facilitate using HOF's like map, filter, flatMap etc.

Jesper Nordenberg
  • 2,104
  • 11
  • 15
  • Scala type system is indeed more robust. I'd reformulate the first statement in something like: C++ is as typesafe as Scala but lacks Scala's flexibility requiring different programmers qualifications to use it effectively. – Basilevs Apr 05 '11 at 09:21
  • 2
    Basilevs, C++ is not type safe. Sorry. It shares a property with C that many programs that typecheck have no defined meaning. – James Iry Apr 05 '11 at 15:39
  • C++ can be used as typesafe language is some C constructs and casts are banned. – Basilevs Apr 06 '11 at 07:00
  • 1
    Saying that C++ is type safe if you don't use the C half the language makes no sense. First, there are C++ constructs that are still problematic (e.g. new, new[], delete, and delete[]) and second, saying that C++ is type safe if you don't do X, Y, and Z is just another way of saying that the C++ language is not a type safe language. – James Iry Apr 06 '11 at 15:52
  • Well you are formally right. The problem is that people tend to view C++ as C therefore reducing its typesafety even futher. – Basilevs Apr 07 '11 at 01:01
-4

C++ type system is stricter than Scala's.

  1. There is no type erasure (even in templated code).
  2. Ideally, there is no type casting in C++ (if you use direct casting like dynamic_cast, static_cast, of C style cast you would be blamed for bad design. Those are considered last resort low level tools.).
Basilevs
  • 22,440
  • 15
  • 57
  • 102
  • Good point, no type erasure. But isn't this only a compiler way to implement Generics? Isn't this hidden from the layman programmer? And I agree 100% with 2. – towi Apr 05 '11 at 06:08
  • Templates are more common approach than generics. But they don't lose any type information being recompiled each time a new type is used with them. – Basilevs Apr 05 '11 at 06:09
  • @towi: Read [this](http://stackoverflow.com/questions/31693/) to better understand the difference between templates and generics. – Björn Pollex Apr 05 '11 at 08:05
  • 3
    Type erasure is a runtime implementation detail. It's not because Scala does some typecasting under the hood that is becomes less typesafe. So based on your arguments C++ typesystem is not more typesafe than Scala. – llemieng Apr 05 '11 at 08:17
  • Erasure is different from usual abstract class of template approaches and has a great impact on programmer options. In no way it is "just implementation detail". Type erasure provides safe downcasting and therefore is more flexible than C++ without dynamic_cast. – Basilevs Apr 05 '11 at 09:15
  • As templates don't really manipulate with types in any way they are stricter in type sense than generics. – Basilevs Apr 05 '11 at 09:23
  • 3
    If we are talking about compile time type safety, type erasure is irrelevant. It's a runtime "feature" and the Scala compiler will tell you whenever you're doing something which can be unsafe at runtime due to erasure. – Jesper Nordenberg Apr 05 '11 at 14:11
  • 1
    A type system refers to what is known about the program at compile time. Type erasure produces a run time effect, so it can't decrease strictness. The limitations type erasure imposes at compile time are a reduction of what overloads are possible, and how specific type guards can be. – Daniel C. Sobral Apr 05 '11 at 14:36
  • 3
    To continue. ML and Haskell compilers tend to be implemented with type erasure and yet their type systems are (relatively) bullet proof. It's the combination of type erasure with "instanceof" style checks (including dynamic_cast in C++ and Scala's open ended pattern matching) that causes problems. So if the answer in C++ is to avoid casts then the answer in Scala is to avoid casts - and, in fact, the Scala compiler will warn you when you're using pattern matching in a way that amounts to an unsafe cast. – James Iry Apr 05 '11 at 15:44
  • @Daniel, I think type erasure is purely compile time concept. All its effects and type safety are guranteed by compiler and no additional work is done in runtime. – Basilevs Apr 06 '11 at 06:59
  • @Basilevs Type erasure is a transformation that happens from the source code to the target code: the type present in the source is erased from the target. So it is at run time that code has to deal with the fact that type has been erased -- at compile time, the type is present in the source. – Daniel C. Sobral Apr 06 '11 at 19:31