docs.scala-lang | The Scala Documentation website | Build Tool library
kandi X-RAY | docs.scala-lang Summary
kandi X-RAY | docs.scala-lang Summary
This repository contains the source for the Scala documentation website, as well as the source for "Scala Improvement Process" (SIP) documents.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of docs.scala-lang
docs.scala-lang Key Features
docs.scala-lang Examples and Code Snippets
Community Discussions
Trending Discussions on docs.scala-lang
QUESTION
I'm trying to write some abstractions in some Spark Scala code, but running into some issues when using objects. I'm using Spark's Encoder
which is used to convert case classes to database schema's here as an example, but I think this question goes for any context bound.
Here is a minimal code example of what I'm trying to do:
...ANSWER
Answered 2022-Feb-10 at 14:17Your first error almost gives you the solution, you have to import spark.implicits._
for Product types.
You could do this:
QUESTION
I am learning scala
from docs.scala-lang.org. There is an example
ANSWER
Answered 2022-Feb-07 at 21:40Accessing the command-line arguments like that is no longer supported in Scala 3:
https://docs.scala-lang.org/scala3/reference/dropped-features/delayed-init.html
QUESTION
There is an aspect of the pattern matching I don't understand.
In the documentation of the pattern matching they show an example such as :
...ANSWER
Answered 2022-Feb-02 at 18:37Under the hood, Scala pattern matching often boils down to code that's exactly like the if (notification instanceof Email) { ... } else if (notification instanceof SMS)
Java code.
The particular example you give, of an abstract class Notification
which isn't sealed
is one where the Scala code is no better (except perhaps expressing overall intent more clearly) than the if
/instanceof
tree.
This is because the main benefit of pattern matching is the possibility of exhaustivity checking. With the if
/instanceof
approach and the example of pattern matching you present, you aren't going to be alerted that you haven't handled every case (e.g. you left off the VoiceRecording
case).
By making Notification
sealed
(e.g. sealed abstract class Notification
), the Scala compiler will ensure that no Scala code in other files (technically, compilation units, which are for all intents and purposes files) can extend Notification
; since it now knows all the possible Notification
s, it can then raise a compiler error if you miss a case. There's no reliable way to do this in the if
/instanceof
case because that's a lower level of abstraction.
QUESTION
I have a couple of solutions to a "double definition" problem, but I can't figure what they're really doing to work around the type erasure issue.
I'll give some general context as well, since I'm probably approaching the problem wrong in the first place, but ultimately help understanding DummyImplicits & by-name params in this context is enough.
Context
I'm replacing parsers for deeply nested JSON where pretty much every value is optional, and nearly all data (including Int, Double, etc.) is stored as Strings. The classes that catch the parsed values take this general form (for now).
...ANSWER
Answered 2022-Jan-12 at 01:56The relevant section of compiled code for option-1
looks like
QUESTION
I copied the following code form the Auxiliary Class Constructors article, pasted into Scastie, changed class
to trait
and set the Scala version to 3.1.0:
ANSWER
Answered 2021-Dec-29 at 14:23The documentation on trait
parameters only mentions trait
parameters, not trait
constructors:
Scala 3 allows traits to have parameters, just like classes have parameters.
It also links to the original SIP document for reference:
For more information, see Scala SIP 25.
In the SIP-25 – trait
parameters, it says [bold emphasis mine]:
In the
ClassDef
of traits, we still do not allow secondary constructors.
However, this restriction, is not reflected in the Syntax Summary, which does not actually distinguish between class
es and trait
s syntactically. So, the restriction is purely one of documentation, not syntax specification.
So, to answer your question:
Why?
Because your code is syntactically valid but semantically invalid, and ScalaMeta seems to not expect this specific kind of semantic invalidity.
And why is it after line 1, which seems perfectly fine and gets accepted when I remove the rest? Does "invariant failed" mean that it's a bug in the compiler? (Many other compilers in such cases add to the message an encouragement to report it.)
It's clearly not a bug in the compiler since the exception is not thrown in the compiler, it is thrown in ScalaMeta.
Main questions:
- Are secondary (or auxiliary – IIUC the two terms I've seen used apparently interchangeably mean the same) constructors allowed in traits?
No. SIP-25 clearly disallows them.
QUESTION
I'm migrating a project from Scala 2.12.1 to 2.13.6, and find that SeqView#flatMap
now returns a View
, which doesn't have a distinct
method. I thus have one bit of code that does not compile anymore:
ANSWER
Answered 2021-Nov-17 at 02:09There are actually 3 basic possibilities for lazy sequences in Scala 2.13: View, Iterator and LazyList.
View is the simplest lazy sequence with very little additional costs. It's good to use by default in general case to avoid allocations for intermediate results when working with large sequences.
It's possible to traverse the View several times (using foreach, foldLeft, toMap, etc.). Transformations (map, flatMap, filter, etc.) will be executed separately for every traversal. So care has to be applied either to avoid time-consuming transformations, or to traverse the View only once.
Iterator can be traversed only once. It's similar to Java Streams or Python generators. Most transformation methods on Iterator require that you only use the returned Iterator and discard the original object.
It's also fast like a View and supports more operations, including distinct.
LazyList is basically a real strict structure, which can be expanded automatically on the fly. LazyList memoizes all the generated elements. If you have a val
with a LazyList, the memory will be allocated for all the generated elements. But if you traverse it on the fly and don't store in a val
, the garbage collector can clean up the traversed elements.
Stream in Scala 2.12 was considerably slower than Views or Iterators. I'm not sure if this applies to LazyList in Scala 2.13.
So every lazy sequence has some caveat:
- View can execute transformations multiple times.
- Iterator can be consumed only once.
- LazyList can allocate the memory for all the sequence elements.
In your use case I believe, it's Iterator that's the most appropriate:
QUESTION
The most common scenario I encounter this with is with Maps. Here is the full anonymous notation:
...ANSWER
Answered 2021-Oct-06 at 21:34The options have for accessing tuples are Kernel.elem/2
, Access.elem/2
and pattern matching. However, the Enum.sort_by/3
function does the job less verbosely than Enum.sort/2
.
QUESTION
I'm migrating from Scala 2.13 to Scala 3 and I'm trying to rewrite small utility functions. In 2.13 it was possible to write one more generic implicit and another one more specific but in Scala 3 it seems no longer possible.
...ANSWER
Answered 2021-Oct-14 at 16:11As user pointed out, problem can be solved by using annotation @targetName("...")
QUESTION
In the Scala documentation at https://docs.scala-lang.org/tour/classes.html, the following example shows a minimal class definition that includes parameters:
...ANSWER
Answered 2021-Sep-30 at 03:09From the documentation you linked:
Primary constructor parameters with
val
andvar
are public.
E.g. (as in the example):
QUESTION
I am currently doing a lot of compiler research. This not only entails to writing compiler plugins but also modifying the dotty compiler, from the parser to the typer. Therefore I need to constantly look at the raw ASTs to sketch the necessary transformations.
In Scala 2, the reflection library provided the following functionality:
...ANSWER
Answered 2021-Oct-03 at 11:08You can define your own reify
in your project
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install docs.scala-lang
To build and view site with Docker:. It will incrementally build and serve site at http://localhost:4000. For more details on the Docker option, see this issue.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page