It allows you to add value to existing code—to build on what you already have—because it was designed for seamless interoperability with Java.
Their run-time performance is usually on par with Java programs. Scala code can call Java methods, access Java fields, inherit from Java classes, and implement Java interfaces. None of this requires special syntax, explicit interface descriptions, or glue code. In fact, almost all Scala code makes heavy use of Java libraries, often without programmers being aware of this fact.
Another aspect of full interoperability is that Scala heavily re-uses Java types. Scala arrays are mapped to Java arrays. Scala also re-uses many of the standard Java library types. For instance, the type of a string literal "abc" in Scala is java. String , and a thrown exception must be a subclass of java. So you can write str. How can this be achieved without breaking interoperability?
In fact, Scala has a very general solution to solve this tension between advanced library design and interoperability. Scala lets you define implicit conversions , which are always applied when types would not normally match up, or when non-existing members are selected. In the case above, when looking for a toInt method on a string, the Scala compiler will find no such member of class String , but it will find an implicit conversion that converts a Java String to an instance of the Scala class RichString , which does define such a member.
The conversion will then be applied implicitly before performing the toInt operation. Scala code can also be invoked from Java code. Scala programs tend to be short.
Scala programmers have reported reductions in number of lines of up to a factor of ten compared to Java. These might be extreme cases. A more conservative estimate would be that a typical Scala program should have about half the number of lines of the same program written in Java. Fewer lines of code mean not only less typing, but also less effort at reading and understanding programs and fewer possibilities of defects. There are several factors that contribute to this reduction in lines of code.
For instance, semicolons are optional in Scala and are usually left out. As an example, compare how you write classes and constructors in Java and Scala.
In Java, a class with a constructor often looks like this:. Repetitive type information can be left out, so programs become less cluttered and more readable. Scala gives you many tools to define powerful libraries that let you capture and factor out common behavior. For instance, different aspects of library classes can be separated out into traits, which can then be mixed together in flexible ways. Or, library methods can be parameterized with operations, which lets you define constructs that are, in effect, your own control structures.
Together, these constructs allow the definition of libraries that are both high-level and flexible to use. Programmers are constantly grappling with complexity. To program productively, you must understand the code on which you are working. Overly complex code has been the downfall of many a software project. Unfortunately, important software usually has complex requirements.
Scala helps you manage complexity by letting you raise the level of abstraction in the interfaces you design and use. As an example, imagine you have a String variable name , and you want to find out whether or not that String contains an upper case character. In Java, you might write this:. In principle, such control abstractions are possible in Java as well. For instance, if you wanted to support querying over strings, you might invent an interface CharacterProperty with a single method hasProperty :.
With that interface you can formulate a method exists in Java: It takes a string and a CharacterProperty and returns true if there is a character in the string that satisfies the property.
You could then invoke exists as follows:. A static type system classifies variables and expressions according to the kinds of values they hold and compute. Scala stands out as a language with a very advanced static type system. After all, the absence of a static type system has been cited by some as a major advantage of dynamic languages.
The most common arguments against static types are that they make programs too verbose, prevent programmers from expressing themselves as they wish, and make impossible certain patterns of dynamic modifications of software systems.
However, often these arguments do not go against the idea of static types in general, but against specific type systems, which are perceived to be too verbose or too inflexible. With these impediments out of the way, the classical benefits of static type systems can be better appreciated.
Among the most important of these benefits are verifiable properties of program abstractions, safe refactorings, and better documentation. Verifiable properties. Static type systems can prove the absence of certain run-time errors. For instance, they can prove properties like: booleans are never added to integers, private variables are not accessed from outside their class, functions are applied to the right number of arguments, only strings are ever added to a set of strings.
For instance, they will usually not detect array bounds violations, non-terminating functions, or divisions by zero. They will also not detect that your program does not conform to its specification assuming there is a spec, that is!
Static type systems have therefore been dismissed by some as not being very useful. The argument goes that since such type systems can only detect simple errors, whereas unit tests provide more extensive coverage, why bother with static types at all?
We believe that these arguments miss the point. Although a static type system certainly cannot replace unit testing, it can reduce the number of unit tests needed by taking care of some properties that would otherwise need to be tested. Likewise, unit testing can not replace static typing. After all, as Edsger Dijkstra said, testing can only prove the presence of errors, never their absence.
So the guarantees that static typing gives may be simple, but they are real guarantees of a form no amount of testing can deliver. Safe refactorings. A static type system provides a safety net that lets you make changes to a codebase with a high degree of confidence.
Consider for instance a refactoring that adds an additional parameter to a method. In a statically typed language you can do the change, re-compile your system and simply fix all lines that cause a type error. Once you have finished with this, you are sure to have found all places that needed to be changed. The same holds for many other simple refactorings like changing a method name, or moving methods from one class to another. In all cases a static type check will provide enough assurance that the new system works just like the old one.
Static types are program documentation that is checked by the compiler for correctness. Unlike a normal comment, a type annotation can never be out of date at least not if the source file that contains it has recently passed a compiler. Furthermore, compilers and integrated development environments can make use of type annotations to provide better context help. For instance, an integrated development environment can display all the members available for a selection by determining the static type of the expression on which the selection is made and looking up all members of that type.
Typically, useful documentation is what readers of a program cannot easily derive themselves. In a method definition like. In the example above, the following two less annoying alternatives would work as well. In fact, only a few features of Scala are genuinely new; most have been already applied in some form in other languages. The list cannot be exhaustive—there are simply too many smart ideas around in programming language design to enumerate them all here.
Expressions, statements and blocks are mostly as in Java, as is the syntax of classes, packages and imports. Scala also owes much to other languages.
Its uniform object model was pioneered by Smalltalk and taken up subsequently by Ruby. Its idea of universal nesting almost every construct in Scala can be nested inside any other construct is also present in Algol, Simula, and, more recently in Beta and gbeta.
Thanks a ton for such a detailed reply. It's also scalable in the sense that you can add what looks like new syntax in library code that in other languages would have to be built into the language itself. Jasper-M isn't that what's meant by a domain specific language? It's what enables the creation of expressive DSLs. Sign up or log in Sign up using Google.
Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Does ES6 make JavaScript frameworks obsolete? Podcast Do polyglots have an edge when it comes to mastering programming Featured on Meta. Now live: A fully responsive profile. Related In general, the new watchers most likely from the Dotty announcement and Hacker News posts here and here are perhaps the most interesting things about these time series.
Additionally, 2. Now that 2. The 2. Finally, it is interesting to note that with Dotty getting closer to being able to compile large portions of the main Scala ecosystem, the prospect of significant savings in development cycle times has caused a number of companies to come forward and express interest in moving to it as soon as possible. It will be interesting to see what happens when Dotty achieves functional parity with scalac. Scala is a multi-paradigm language, and its community is certainly a reflection of that.
This design thesis has led to significant adoption by converts from pure functional programming FP languages e. Haskell in addition to the large numbers of Java converts drawn by the projects listed above. There is and has always been a sizable pool of expat Haskell developers working on Scalaz; since its inception, the community has been somewhat polarizing in its full-throated advocacy of porting Haskell-like FP patterns, syntax, etc.
In late , after the failed introduction of a new Code of Conduct CoC , the community began to fracture, as seen in this Tweet from Edward Kmett. The Typelevel folks did, in fact, leave to create a new FP library Cats , which, although not a direct fork of Scalaz, uses many of the same concepts and is more or less a direct competitor.
The overall magnitude of pull request activity metrics are larger for Cats than Scalaz, though some of this may be due to the age of the library. However, in terms of commits per day and new watchers per day, there appears to be no significant decrease over time for Scalaz. So, it would seem that the fears of some in the community have come to pass.
Indeed, the existence of both Scalaz and Cats has caused numerous headaches for downstream libraries, though most seem to have found workable solutions, either through including all necessary FP dependencies as FS2 and scodec have done , or abstracting them through shims or source-code-level preprocessing. On the other hand, perhaps Tony Morris was correct in saying that the Scalaz project is quite different from Cats in terms of motivation, and that ultimately there is room for both libraries to continue to evolve and serve different needs within the Scala community.
Cats has striven to remain lightweight and modular see here and here as well as eschew some of the more inscrutable syntax of Scalaz. However, the larger reason might have been that the CoC was imposed on a seven-year-old community rather than introduced as an initial condition. Changing the modes of group discourse in this fashion is arguably tantamount to asking participants to join an entirely separate movement. The two most likely outcomes are flat-out rejection of the CoC, or this is what happened with Scala-internals the CoC is just ignored and the constant negativity drags forum activity to a near halt.
The main fault line that the Scalaz drama exposed the same issue just came up again recently on the Scala-debate forum is a philosophical disagreement over codes of conduct in open source software development. One side feels that ideas are tantamount, and how those ideas may be communicated is of lesser importance this debate came to a head at LambdaConf this year. This side feels that it is not acceptable to ignore criticism simply because it was conveyed in a rude or insulting fashion.
The other side feels that violent communication drags the entire community down, and therefore notions of acceptable communication should be circumscribed by a CoC.
I can say from personal experience that my interactions with the Typelevel crew have been incredibly positive—the same goes for everyone on my team. This concern seems overblown to me. Lightbend CEO Mark Brewer made it clear in a followup blog post that Lightbend has a continued commitment to the language and the community around Scala. When I mentioned the lingering concerns to Brewer, he replied:.
We are fortunate to have a very engaging and vocal community around Scala, which frequently means that opinions on what Lightbend is doing get a significant amount of coverage. This concern is somewhat well founded. Scala has a steeper learning curve than many other languages, and the lack of centralized onboarding materials and community forums like Rust has, for example is an ongoing issue.
Hopefully, the introduction of the Scala Center will also include more documentation efforts. I spoke with Heather Miller , executive director of the Scala Center, about documentation and this is what she said:.
However, due to how the Scala Center is governed, it is not my personal decision alone to make whether or not Scala Center funds should be allocated to hire a professional writer to improve documentation. This is something that needs to be proposed, discussed, and voted on at the upcoming advisory board meeting before I can take action hiring.
The Scala Center has 3. The center listens to suggestions from individuals, companies, and advisory board members every week on unofficial channels.
0コメント