IntelliJ Go type inference failing

Answered

I'm having an issue where both IntelliJ and Goland type inference fails.

 



The code compiles, runs through various go linters and go formatters fine, but IntelliJ claims it is not valid type inference. Have commented on the row where type inference fails.


package main

type UserCreated struct {
    UserID   string `json:"event_id"`
    Username  string `json:"username"`
}

type UserProfilePictureUpdated struct {
    UserID string `json:"event_id"`
    PictureURL string `json:"picture_url"`
}

// Marker interface
type ProjectionHandlers[TState any] interface {
    IsEventHandlers()
}

type UserProjectionHandlers[TState any] interface {
    ProjectionHandlers[TState]

    UserCreated(state TState, event UserCreated) (TState, error)
    UserProfilePictureUpdated(state TState, event UserProfilePictureUpdated) (TState, error)
}



type Projector[TEventHandlers ProjectionHandlers[TState], TState any] struct {
    initialState TState
    handlers     TEventHandlers
}

func NewEventProcessor[TEventHandlers ProjectionHandlers[TState], TState any](
    initialState TState,
    handlers TEventHandlers,
) *Projector[TEventHandlers, TState] {
    return &Projector[TEventHandlers, TState]{
       initialState: initialState,
       handlers:     handlers,
    }
}

type UserProjector[THandlers UserProjectionHandlers[TState], TState any] = Projector[THandlers, TState]

func NewUserProjector[THandlers UserProjectionHandlers[TState], TState any](
    initialState TState,
    handlers THandlers,
) *UserProjector[THandlers, TState] {
    // This fails type conversion in IntelliJ
    // Cannot use 'NewEventProcessor(initialState, handlers)' (type *Projector[TEventHandlers, TState]) as the type *UserProjector[THandlers, TState]
    return NewEventProcessor(initialState, handlers)
    
    // THe below works 
    //return (*UserProjector[THandlers, TState])(NewEventProcessor(initialState, handlers))
}


type UserNameProjectionState struct {
    Username string `json:"username"`
}

type UserNameProjectorHandlers struct{}
// IsCategoryHandler implements the CategoryHandlers marker interface
func (h UserNameProjectorHandlers) IsEventHandlers() {}

func NewUserNameProjectorHandlers() UserNameProjectorHandlers {
    return UserNameProjectorHandlers{}
}


func (h UserNameProjectorHandlers) UserCreated(
    state UserNameProjectionState,
    event UserCreated,
) (UserNameProjectionState, error) {
    // Initialize the user in the map
    state.Username = event.Username
    return state, nil
}

func (h UserNameProjectorHandlers) UserProfilePictureUpdated(
    state UserNameProjectionState,
    _ UserProfilePictureUpdated,
) (UserNameProjectionState, error) {
    return state, nil
}

type UserNameProjector = UserProjector[UserNameProjectorHandlers, UserNameProjectionState]

func NewUserNameProjector() *UserNameProjector {
    initialState := UserNameProjectionState{
       Username: "",
    }
    handlers := NewUserNameProjectorHandlers()

    return NewUserProjector(initialState, handlers)
}


func main() {
    // Example usage
    projector := NewUserNameProjector()

    // create a UserCreated event
    event := UserCreated{
       UserID:   "123",
       Username: "john_doe",
    }
    state, err := projector.handlers.UserCreated(projector.initialState, event)

    if err != nil {
       panic(err)
    }
    println("Name:", state.Username)

}
 

0
2 comments

For me this is a deal-breaker for using IntelliJ or Goland for coding Go, which is a pity given I'm using it for all other coding.

VSCode with to official Go plugin from Google has no issue with the type inference.

0

Dear Bagge Robert, Thank you for contacting JetBrains Support!
I was able to reproduce this issue both in IntelliJ IDEA with the Go plugin and in GoLand. This problem is directly related to Go language inspections and is similar to the issue GO-17580.
I have created a new ticket in YouTrack for the developers regarding this issue - GO-19149. You can subscribe to updates on this ticket by clicking the star icon in the top right corner or by leaving a comment at the following link: GO-19149

0

Please sign in to leave a comment.