Comments (6)
It's really not as complicated as you might fear, and it's certainly worth learning (obviously not just for kantan.csv). If you're struggling, ping me on gitter and I'll try to make the time for a useful introduction on the matter.
from kantan.csv.
There are a few issues with this code.
The first one, the one that causes compilation to fail, is that readCsv
and asCsvReader
expect an implicit RowDecoder[T]
in scope (and asCsvWriter
expects a RowEncoder[T]
). You need to provide one.
You have various ways of doing that - have it as an implicit parameters of the methods that call asCsvReader
, readCsv
and asCsvWriter
, for example. It's probably better to have the instances as part of the trait's abstract fields, though.
Sort that out, and you code compiles.
You could also replace filter(_.isSuccess).map(_.get)
with collect { Success(a) => a }
, it's a little more idiomatic (and calling get
, however safe it may seem, is a code smell).
I think you could also replace most of toCsvString
with a call to asCsv
, which enriches all TraversableOnce[T]
instances such that there is an implicit RowEncoder[T]
in scope.
Unless I'm mistaken, there is nothing that needs to be fixed in kantan.csv, what you complain about is expected and normal. I'll close this issue - issues are meant for bugs and improvements, and your problem is neither, it's a request for help. If you feel I didn't sufficiently address your problem, you're quite welcome on the gitter channel where we can go through this a bit interactively.
from kantan.csv.
I'm sure you're right. I need to get up to speed on implicits before I can digest this.
from kantan.csv.
Appreciate it. I'm plowing through some stuff right now, avoiding the temptation to try and link it directly to my immediate need. Besides, my work around (simply implement within the case classes I need to [de]serialize) deals with all the scoping issues--I suspect they're scoping issues--I was having, so I can learn at a somewhat leisurely pace.
If I run into trouble, I'll take you up on your kind offer.
from kantan.csv.
Fair enough.
I just get a bit bothered by the fact that you're using kantan.csv in a way that forces you to write encoding / decoding code - the whole purpose of it is that you mostly should not, I hate writing such code.
So, quickly then. Kantan.csv relies heavily on a pattern known as type classes. It boils down to implicits and knowing how they're resolved: if you have a type T
, and an instance of RowDecoder[T]
, then given a CSV row (that is, a Seq[String]
) you know how to turn it into a T
.
This is how, say, asCsvReader
works: it expects an implicit RowDecoder[T]
in scope, parses the CSV stream and delegates decoding to T
to the decoder.
There are various ways to provide an implicit RowDecoder[T]
. If T
is a concrete type (say, List[Int]
), you can implement one and mark is as implicit
. In your case though, T
is a variable and we know nothing about it - there is no way the compiler will be able to find a RowDecoder[T]
for all possible T
s, that is, for all possible types ever. So you need to help it out by providing this instance yourself. Most often, it's done through context bounds, but in your case everything is in a trait (we might want to talk about that to, I think you might be making this much more complex than it needs to be), so that's not a possibility. The simplest way to get things to work would be to rewrite parse
's signature as follows:
def parse(line : String, sep : Char = ',', header : Boolean = false)(implicit dt: RowDecoder[T]): Option[T]
T
will be know by the time parse
is called, so the compiler can work out whether it has a RowDecoder[T]
instance in scope (most likely, since you're using the generic
module).
from kantan.csv.
That works, though don't know enough about context to understand why yet. I'll keep studying.
from kantan.csv.
Related Issues (20)
- [feat request] Publishing for Scala.js 1.0.0 HOT 4
- Separator detection
- Help with a case
- could not find implicit value for evidence parameter of type kantan.csv.HeaderDecoder HOT 8
- RowEncoder for more than 22 fields
- Overriding codecs HOT 2
- Enabling `quoteAll` breaks double quote escaping
- Configurable line separator (write CSV with \n line separator)
- Consuming a pipe-delimited file becomes erratic when is set to CsvConfiguration('|', '"', QuotePolicy.Always, Header.None)
- Intellij red flag Ambiguous implicit complain still sbt compilation works fine HOT 2
- Can we use this to read csv from S3 bucket and how? HOT 1
- Scala 3 support HOT 3
- my readCsv fails on corrupted file without means to inspect it
- Help with identifying separator HOT 1
- Case insensitive headers HOT 2
- Parsing a cell as a NonEmptyList
- Single columns optional with CSV containing headers
- Why case class has implicits issue ? HOT 1
- How to read a wide CSV from S3 in streaming fashion? HOT 1
- StackOverflowError when parsing csv with different then expected cell separator and quoted cells
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 kantan.csv.