tags:

views:

205

answers:

5

There are 2 reasons for me to ask:
1. I'd like a better code fragmentation to facilitate version control on per-function level
2. I struggle from some attention deficit disorder and it is hard for me to work with long pieces of code such as big class files

To address these problems I used to use include directives in C++ and partial class definitions and manually-definable foldable regions in C#. Are there any such things available in Scala 2.8?

I've tried to use editor-fold tag in NetBeans IDE, but it does not work in Scala editor unfortunately :-(

UPDATE: As far as I understand, there are no such facilities in Scala. So I'd like to ask: someone who has any connection to Scala authors, or an account on their Bugzilla (or whatever they use), please, suggest them an idea - they should probably think of introducing something of such (I was fascinated by C# regions and partial classes for example, and plain old includes also look like a convenient tool to have) to make Scala even more beautiful through laconicity, IMHO.

+9  A: 

How about doing it with traits? You define it like this:

trait Similarity 
{
  def isSimilar(x: Any): Boolean
  def isNotSimilar(x: Any): Boolean = !isSimilar(x)
}

...and then you use it like so:

class Point(xc: Int, yc: Int) extends Similarity 
{
 var x: Int = xc
 var y: Int = yc
 def isSimilar(obj: Any) =
    obj.isInstanceOf[Point] &&
    obj.asInstanceOf[Point].x == x
}

If the class Point were bigger, you could split it further into traits, resulting in the division that you want. Please note, however, that I don't think this is advisable, as it will make it very difficult to get a good overview of your code, unless you already know it by heart. If you can break it in a nice way, however, you might be able to get some nice, reusable blocks out of it, so in the end it might still be worth doing.

Best of luck to you!

Banang
+1  A: 

Scala code folding works properly in IDEA.

Randall Schulz
I mean not just language blocks (like functions) folding, but manually defined regions to be named and folded, like with #region in C#. Is such a feature available in Idea?
Ivan
Yes, arbitrary regions may be folded (replaced by an ellipsis). You can also define which portions of a file's structure should be pre-folded when the file is initially opened.
Randall Schulz
@randall-schulz, thanks. I'll try it.
Ivan
+1  A: 

The version control tools I work with (bzr or git, mostly) have no trouble isolating changes line-by-line. What use case do you have--that's common enough to worry about--where line-level isolation (which allows changes to independent methods to be merged without user intervention) is not enough?

Also, if you can't focus on something as large as one class with many methods, use more classes. A method generally requires you to know what the other methods are, what the fields are, and so on. Having that split across separate files is just asking for trouble. Instead, encapsulate your problem in a different way so you can work with smaller self-contained chunks at a time.

Rex Kerr
I mean not by-line, but by-function (while functions do grow, changing numbers of lines where they end and pushing down lines where next functions begin).
Ivan
As for focusing - I was pretty comfortable with C++ having one file for a class declaration (listing all it's methods) and a separate file for each method implementation. This was also pretty convenient for classes documenting, as verbose descriptive comments could be placed in a declaration file without cluttering code files.
Ivan
@Ivan - I've not had trouble with git getting confused when some lines push down over where others used to be. It seems to match the code better than that. And with folding braces in NetBeans, it seems like you're back to having one file listing all of the methods, except you can expand them out. So you should be in good shape if you get it working at least to fold at braces.
Rex Kerr
@rex-kerr, I'd say in NetBeans Scala editor braces folding works "on Sundays" and other days when it has a mood to work occasionally :-) Maybe this is caused by errors (red wavy underlines) Net Beans Scala editor uses to finds even in absolutely correct, compilable and working code, sometimes even generated by itself. Eclipse Scala editor does not work at all on my system, throwing many-screens-long exceptions instead. I have everything up-to-date, plugins for both NetBeans and Eclipse are installed from their official update repositories. I hope they'll get debugged some day.
Ivan
Following Randall's advice, I am going to try Idea this evening.
Ivan
@Ivan - If you want something that is too stupid to get confused, try the syntax highlighting for Scala in Kate (if you use an OS that can run it). I am pretty sure TextMate does a good job also, though I haven't used it.
Rex Kerr
+3  A: 

I suggest to read white paper by Martin at this link. In this white paper 'Case Sudy: The Scala Compiler' chapter will give you idea about how you can achieve component based design having code in several separate files.

Jaydeep
Thanks. Will read this. I've always supposed that complex and long code is a result of bad architecture. Designing good one with Scala is still a field to explore and think a lot...
Ivan
+1  A: 
//file A.scala
trait A { self: B =>
....
}

//file B.scala
trait B { self: A =>
....
}

//file C.scala
class C extends A with B
Mushtaq Ahmed