janpfeifer / gonb

GoNB, a Go Notebook Kernel for Jupyter
https://github.com/janpfeifer/gonb
MIT License
631 stars 35 forks source link

Generics on structs not working? #23

Closed AnotherCodeArtist closed 1 year ago

AnotherCodeArtist commented 1 year ago

Hi Jan!

Just came across another problem connected with generic structs. The following code ...

type Model[T any] struct {
    Data T
}

results in

 # gonb_0224f8ff 
Cell[8]: Line 2 ./main.go:9:10: undefined: T 

Any workaround or quick fix available?

janpfeifer commented 1 year ago

hey @AnotherCodeArtist , sorry I hadn't noticed this issue before. My bad.

It does support generics in functions, as in:

func identity[T any](x T) T {
    return x
}

%%
fmt.Println(identity("Hello"))
fmt.Println(identity(math.Pi))

Let me fix it. ETA tomorrow.

janpfeifer commented 1 year ago

I think I found the issue and fixed it -- again apologies I missed that, now I added the case to the parsing test.

I went ahead and created a new release (v0.6.2), but pls test it out if it works for you. If not, pls let me know.

cheers

AnotherCodeArtist commented 1 year ago

Hi Jan!

First tests show that it works like a charm! Thanx for the brilliant support.

BTW: I'm also longing for improved code completion.

janpfeifer commented 1 year ago

Btw, the code completion improved some recently (v0.6.1), but there are case where it still doesn't work.

Would you remember specific code-completion cases that don't work for you ? I'm looking for examples of cases that need work.

AnotherCodeArtist commented 1 year ago

Of course, we are now somehow in the wrong issue, but here's a simple case where code-completion does not work at all:

import "fmt"

type Person struct {
    Firstname, Lastname string
}

%%
john := Person{"John", "Doe"}
fmt.Println(john.Fi...

When I type jo nothing gets completed. The same with john.Fi. I gues none of the local variables is recognized.

Would we really cool if you could do something about it.

janpfeifer commented 1 year ago

Thanks for the example.

Interesting this is another instance of the cell not being parseable ... notice that is you close the parenthesis the auto-complete starts working. But ideally I have to find a way to deal with this ...

Btw, if you don't mind I'll close this issue, since it's about the generics on structs. There is #22 , I'll follow up there.

cheers

janpfeifer commented 1 year ago

Btw, I managed to figure out a relatively simple way to handle unparseable cell content. So code-completion in the example you provided now works (in the v0.6.3 release)! I think this will help in many cases. If you notice any other cases, pls let me know :)