Some initial suggestions (or perhaps things I don't understand)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Some initial suggestions (or perhaps things I don't understand)

nathansobo
So far Scala has been really great, and it's made me way more
productive in the construction of an XML facade that analyzes an
annotated schema and loads classes to construct fragments of the
document described therein from a database on the fly based on the
annotations. Functional programming in a Java environment is
excellent. Here are some initial thoughts:

Constructors and instance variables in Scala are confusing and sort of
frustrating. For example, if I use the default constructor, then it
seems I have to commit myself to using its arguments as values rather
than variables. I would really like to be able to specify mutable
instance variables in the default constructor.

Not being able to reference local variables with the "this" keyword is
also a little frustrating. It means that if I want to refer to
instance variables within accessor methods then I must give the formal
parameters slightly different names. In Java I have often used a
trailing underscore on instance variables but that isn't allowed in
Scala either.

Also, a convenient way of defining accessor methods for values and
variables that didn't rely on using case classes would be really nice
to have.

Maybe these issues are just me missing some larger point. I'm sure
there are good reasons behind the team's design decisions, but I just
thought I'd bring it up.

Also, a question... what happened to the Ord trait mentioned in
Programming In Scala? Has it become Ordered? I used "view bound types"
in place of the book's example that used Ord, and things seemed to
work. I guess I need to look more into views. Thanks again for
creating this awesome language.

Reply | Threaded
Open this post in threaded view
|

Re: Some initial suggestions (or perhaps things I don't understand)

Lex Spoon
Hey, Nathan, thanks for the detailed comments!

You should be sure and grab the beta release.  It seems to address
some of your concerns.

I don't know the details of Scala 1 versus 2 extremely well. I'll
answer based on Scala 2.


> Constructors and instance variables in Scala are confusing and sort of
> frustrating. For example, if I use the default constructor, then it
> seems I have to commit myself to using its arguments as values rather
> than variables. I would really like to be able to specify mutable
> instance variables in the default constructor.

All you need to do is put "var" in front of the parameter you want to
be modifiable:

  class Foo(var somestuff: String) { ... }


Also, you can perfectly well create mutable variables, and even give
them some code.  Note, by the way, that all loose code in a class is
executed whenever a class is instantiated through any constructor.

 
   class Foo {
      ...
      var myivar = some_initializer...
      ...
   }



> Not being able to reference local variables with the "this" keyword is
> also a little frustrating. It means that if I want to refer to
> instance variables within accessor methods then I must give the formal
> parameters slightly different names. In Java I have often used a
> trailing underscore on instance variables but that isn't allowed in
> Scala either.

This works fine as well, at least in Scala 2.  Here's a
tongue-twisting example:

class Stuff(var stuff: String) {
  def stuff(stuff: String) = {
    this.stuff = stuff
  }
}




> Also, a convenient way of defining accessor methods for values and
> variables that didn't rely on using case classes would be really nice
> to have.

Public vals and vars are accessible from the outside.  This is also
true for constructor parameters, if you put "val" in front of the
parameter.

  class Foo(val myPublicIvar: String) { ... }



> Also, a question... what happened to the Ord trait mentioned in
> Programming In Scala? Has it become Ordered? I used "view bound types"
> in place of the book's example that used Ord, and things seemed to
> work. I guess I need to look more into views. Thanks again for
> creating this awesome language.

You are probably looking for the "Ordered" trait.

Be aware that views are being replaced by implicit coercions.  Views
still work but are deprecated.  Be sure to grab the "changed" document
from the web site!


Overall, Scala most certainly is intended to be convenient to use.  It
would be very helpful if you continue to post whenever you run into
things like the above!


-Lex


Reply | Threaded
Open this post in threaded view
|

Re: Some initial suggestions (or perhaps things I don't understand)

nathansobo
Hey, thanks for the reply. Looks like it's about time for me to get going with
Scala 2. Is it as stable as Scala 1 you think?



Reply | Threaded
Open this post in threaded view
|

Re: Some initial suggestions (or perhaps things I don't understand)

Lex Spoon
Nathan Sobo <[hidden email]> writes:

> Hey, thanks for the reply. Looks like it's about time for me to get going with
> Scala 2. Is it as stable as Scala 1 you think?


For a new person, Scala 2 is already surely the way to go.  The
stability is probably lower, but the language is much better.  It's
stable enough that the distributed compiler is compiled with itself.


    -Lex