GithubHelp home page GithubHelp logo

Comments (6)

nrinaudo avatar nrinaudo commented on June 24, 2024 1

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.

nrinaudo avatar nrinaudo commented on June 24, 2024

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.

revprez avatar revprez commented on June 24, 2024

I'm sure you're right. I need to get up to speed on implicits before I can digest this.

from kantan.csv.

revprez avatar revprez commented on June 24, 2024

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.

nrinaudo avatar nrinaudo commented on June 24, 2024

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 Ts, 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.

revprez avatar revprez commented on June 24, 2024

That works, though don't know enough about context to understand why yet. I'll keep studying.

from kantan.csv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.