Coder Social home page Coder Social logo

Accept functions as interfaces about go-linq HOT 5 CLOSED

ahmetb avatar ahmetb commented on May 27, 2024
Accept functions as interfaces

from go-linq.

Comments (5)

ahmetb avatar ahmetb commented on May 27, 2024

@bsideup hmm, I am not sure I understood what's going on from the code. Can you please elaborate?

from go-linq.

bsideup avatar bsideup commented on May 27, 2024

@ahmetalpbalkan yes, I will try :)

Currently what we have in Go-linq:

We receive func with T (aka interface{}) signature, then users almost always perform casting:

over18Names, err := From(students)
    .Where(func (s T) (bool,error){
        return s.(*Student).age >= 18, nil
    })
    .Select(func (s T) (T,error){
        return s.(*Student).name, nil
    }).Results()

But, since casting is a must, we can move it into the go-linq and allow users to specify type on func's signature, so this code will look like this:

over18Names, err := From(students)
    .Where(func (s Student) (bool,error){
        return s.age >= 18, nil
    })
    .Select(func (s Student) (T,error){
        return s.name, nil
    }).Results()

Technically it's possible with reflection. I you want I can implement it and send as a pull request :)

from go-linq.

ahmetb avatar ahmetb commented on May 27, 2024

@bsideup ooh interesting. What would be the method signatures for Where/Select in this case?

from go-linq.

ahmetb avatar ahmetb commented on May 27, 2024

@bsideup If you're still interested, I'd love to see a prototype. It appears like this would make these methods accept interface{} rather than []linq.T. I have a feeling like this would make the caller code cleaner although it'll probably make it a bit harder to use for the first time, but then we have errors etc to handle that. Feel free to send something over to discuss if you like.

from go-linq.

bsideup avatar bsideup commented on May 27, 2024

@ahmetalpbalkan sorry for no response for a long time, was busy a little bit

actually, I did some research, and it looks like reflection will affect performance :(

Also, I found this great post about it:
http://blog.burntsushi.net/type-parametric-functions-golang/

from go-linq.

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.