scastie | An interactive playground for Scala | Build Tool library
kandi X-RAY | scastie Summary
kandi X-RAY | scastie Summary
An interactive playground for Scala
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 scastie
scastie Key Features
scastie Examples and Code Snippets
Community Discussions
Trending Discussions on scastie
QUESTION
I'm trying to list null fields recursively with Shapeless. But it doesn't seem to show all the fields:
...ANSWER
Answered 2022-Mar-24 at 15:00I am not sure this is exactly what you want, but I hope you can tweak it to make it work for your need. I have considered the following as null
s:
- All
None
s - Any other types which are
null
- Recursive
null
s orNone
s found in case classes and/orOption
s
You can add further implicits to handle other cases:
QUESTION
Experimenting with Scala 3's metaprogramming capabilities, I found this issue with inline matching that I'm not able to satisfy myself with an explanation for.
Given a transparent inline method eitherTest
which takes an inlined Either[String, Int]
and then returns either the String
or the Int
directly using an inline match expression, things work perfectly fine if the input to eitherTest
is explicitly typed to a Left
or a Right
. However, the compiler appears to be unable to reduce the match expression if the input is explicitly typed to the supertype of Either
. What makes this more curious is that the argument to eitherTest
is itself typed as Either
.
Is this a compiler bug? Is this expected behavior? I'm struggling to understand why the compiler would not be able to resolve this.
Scastie link: https://scastie.scala-lang.org/CThqajauRVeJvxvW0uYy4w
Code:
...ANSWER
Answered 2022-Mar-20 at 16:36 val l: Either[String, Int] = Left("hello")
val r: Either[String, Int] = Right(22)
QUESTION
I'm new to Scala and would like to do the DateTime using joda.
In Scastie (an online Scala IDE) I wrote this:
...ANSWER
Answered 2022-Jan-25 at 11:54You're trying to use something from this library: https://search.maven.org/artifact/joda-time/joda-time/2.10.13/jar
You can include this library in Scastie using the following line:
QUESTION
We would like to base some of our JSON validations to the result of the validation of a previous validation.
...ANSWER
Answered 2021-Dec-29 at 19:52You could chain the Reads using orElse.
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 am struggling a little with scala 2.12:
I have the following hierarchy:
...ANSWER
Answered 2021-Dec-16 at 12:35It doesn't work because Writer
declares that its write
method will accept an arbitrary A
. What if someone decides to pass an A
that is not a B
to writer.write
? Then it wouldn't work, so the compiler stops you from doing that.
QUESTION
Is there a better way to rewrite these overloaded methods to avoid the double definition
issue?
ANSWER
Answered 2021-Dec-09 at 15:57QUESTION
In the example below, I'm wondering why funPoly
can't accept the existentially quantified type value outersFromInnersEx
, even though funEx
can.
ANSWER
Answered 2021-Dec-02 at 02:36It's about type variances, you can make funPoly
work by changing
QUESTION
I am wondering how to fix the type erasure warning even though I added ClassTag
? is there an easy fix without adding an additional library like TypeTag?
ANSWER
Answered 2021-Nov-30 at 22:04Type erasure is a consequence of how generics are implemented on the JVM. It means that a runtime, you won't be able to tell the difference between e.g. a List[Int]
and a List[String]
; all the runtime knows is that you have a List
. The knowledge of whether you're expecting [Int]
or [String]
is compile-time only.
In your case, you have T_SEQUENCE[T_ElemType]
- so given some object of type Any
, you can use isInstanceOf
to check if you have an instance of some T_SEQUENCE
, but there's no knowledge of what the type parameter on it is.
You could make an assumption that your method will never receive a T_SEQUENCE
with the wrong type parameter, and use that to do
QUESTION
In Dean Wampler's book, Programming Scala, 3rd Edition, there is an example with:
...ANSWER
Answered 2021-Sep-16 at 07:50As suggested in the comments, Nil
extends List[Nothing]
, thus your case IterableOnce[t] => t
applies and Elem[Nil.type] =:= Nothing
.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install scastie
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