Comments (3)
::
is a (non-empty) List
. It is not an extractor for arbitrary Seq
.
scala> List(42, 27) match { case x :: xs => x }
^
warning: match may not be exhaustive.
It would fail on the following input: Nil
val res0: Int = 42
scala> Vector(42, 27) match { case x :: xs => x }
^
error: constructor cannot be instantiated to expected type;
found : scala.collection.immutable.::[A]
required: scala.collection.immutable.Vector[Int]
scala> (Vector(42, 27): Seq[Int]) match { case x :: xs => x }
scala.MatchError: Vector(42, 27) (of class scala.collection.immutable.Vector1)
... 30 elided
It's usually recommended to ask questions on chat before creating a ticket.
from scala3.
Whatever you decide its okay for me. Most probably its just a minor issue but it took me a while to figure it out.
For me it just seems to break this promise of functional programming to easily reason about functions by just inspecting their code. The headOfSeqOrZero function in my example looks not too suspect for me. Just this parameter type of Seq which provides the :: operator. Either the input contains at least one element then the head of the seq is returned otherwise it just yields 0. But for some sequences head is returned for others not, just dependent on the creation of the seq.
It looks somehow effectful to me. Give it a seq 1, 2, 3 it returns 1. Give it another seq 1, 2, 3 it returns 0. Hmm.
"seq from unsafe wrapped array fails to deconstruct in head and tail" in:
def headOfSeqOrZero(seq: Seq[Int]): Int =
seq match
case head :: tail =>
head
case _ =>
0
//success
headOfSeqOrZero(Seq(1, 2, 3)) should be (1)
//failure
import scala.collection.immutable.ArraySeq
headOfSeqOrZero(ArraySeq.unsafeWrapArray(Array(1, 2, 3))) should be (1)
from scala3.
There is an extractor for Seq
.
scala> Vector(42, 27) match { case x +: xs => x }
^
warning: match may not be exhaustive.
It would fail on the following inputs: Vector0, Vector1(), Vector2(), Vector3(), Vector4(), Vector5(), Vector6()
val res0: Int = 42
Just this parameter type of Seq which provides the :: operator.
This is your misunderstanding.
There are different flavors of patterns when pattern matching. It happens that ::
is "just a case class", but +:
is an "extractor".
Your match is really seq match case _: List =>
or more precisely case _: :: =>
. One case is for list, the other for other seqs.
This would be a fruitful discussion on the discord chat or the discourse forum. They have people who are great at explaining things. https://scala-lang.org/community/ has links.
from scala3.
Related Issues (20)
- Inline methods of opaque types leave redundant proxies in byte code
- Wrong unimplemented members reported
- Multi-Source Extension Overloads would be shadowed by local extensions with the same name HOT 4
- Name clash with Ordering HOT 2
- SIP-47 Clause Interleaving + Java = `bad class file` error HOT 1
- Polymorphic capture functions can be exploited to hide captures HOT 1
- Scala 3 generated @main function is generated in package scope HOT 8
- 3.5.0-RC7 Release procedure HOT 1
- "method apply must be called with () argument" must be generated for scala.Function0 HOT 5
- Regression in `softwaremill/tapir` - StaleSymbolException when compiling macros
- type variable resolution contradicts language spec in a case of an inner class extending an outer class HOT 3
- static final null character crashes parser HOT 1
- quoted Symbol.spliceOwner should not be the current class in extends clauses
- Unexpected type representation reached the compiler backend HOT 1
- a type defined in a trait is "not found" in an object that extends the trait (under certain circumstances) HOT 1
- Anonymous givens for two different types with same name create "Duplicate definition" HOT 1
- Unexpected warning when `@main` is used twice within a single object HOT 2
- Match type with opaque type among cases does not resolve HOT 1
- Cannot do a map on a Map with Scala 3, with Scala 2.13 it works ok HOT 6
- 3.5.0 & 3.5.1-RC2 Release procedure HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scala3.