struct vs. class, int vs. char.

bearophile bearophileHUGS at lycos.com
Tue Apr 28 16:12:50 PDT 2009


Andrei Alexandrescu:
> Could you please give a little more detail?

My experience with Scala is near zero, I have written and compiled only small programs, like the ones from the Shootout and the ones of the first tutorial.
Scala isn't a language as simple as they say, its type system is quite complex and powerful, so Scala isn't easy to learn if you want to use it well.
You can use monads in Scala too, in a decent enough way:
http://www.scala-lang.org/node/46

To give you a small answer, in Scala all types are used in a quite uniform way. This is useful. Even integers have a methods (as in Ruby, but Scala is statically compiled). You can add "methods" to all integers:

object extendBuiltins extends Application {
  def fact(n: Int): BigInt =
    if (n == 0) 1 else fact(n-1) * n
  class Factorizer(n: Int) {
    def ! = fact(n)
  }
  implicit def int2fact(n: Int) = new Factorizer(n)
  println(10!)
}

Removing most of the distinction from built-in types and values and user-defined ones has some advantages. alias this of D2 is a little step in that direction. Like the idea of having first-class types. Once you have first-class types, with run-time-function-evaluation you may not need templates anymore. You have just functions that act on types that run at runtime too. The space of all possible computer languages is huge.


> And speaking of Scala, after having watched a presentation, I was left 
> with the impression that traits are nothing but classes with 
> parameterized base.
[...]
> Is that correct?

I don't know, I am sorry. I may be able to answer your question after reading the Scala docs, but you may do that yourself, and probably more efficiently, because you are smarter.

Bye,
bearophile



More information about the Digitalmars-d mailing list