Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept functions as interfaces #26

Closed
bsideup opened this issue Feb 6, 2016 · 5 comments
Closed

Accept functions as interfaces #26

bsideup opened this issue Feb 6, 2016 · 5 comments
Labels

Comments

@bsideup
Copy link

bsideup commented Feb 6, 2016

Hi!

First of all, thanks for go-linq, it's awesome :)

Since there are no generics in Golang, we have to deal with interface{}-s.

I just spotted nice hack in Glow project to allow users to specify types on parameters themself:
https://github.com/chrislusf/glow/blob/master/flow/dataset_map.go#L19-L47

Seems like a really great idea to steal :)

@ahmetb
Copy link
Owner

ahmetb commented Feb 6, 2016

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

@bsideup
Copy link
Author

bsideup commented Feb 7, 2016

@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 :)

@ahmetb
Copy link
Owner

ahmetb commented Feb 11, 2016

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

@ahmetb
Copy link
Owner

ahmetb commented Feb 19, 2016

@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.

@bsideup
Copy link
Author

bsideup commented Feb 19, 2016

@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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants