tliron / glsp

Language Server Protocol SDK for Go
Apache License 2.0
146 stars 20 forks source link

Why members of CompletionItem are pointers? #29

Open pherrymason opened 2 months ago

pherrymason commented 2 months ago

What's the reasoning behind some CompletionItem members like Kind, Details, Deprecated, Preselect, etc... are defined as pointers?

tliron commented 1 month ago

Because they are optional. The nil value should be interpreted as "doesn't exist".

This should definitely be documented better...

pherrymason commented 1 month ago

Have you considered using an optional type? Like:

type Option[T any] struct {
   value  T
   isSetted bool
}

func Some[T any](value T) Option[T] {
   return Option[T]{value: value, isSetted: true}
}

func None[T any]() Option[T] {
   return Option[T]{isSetted : false}
}
tliron commented 1 month ago

The project started before generics were available. It is definitely possible to switch, but ... for what it's worth, Go has nils. (Terrible design decision.) And nils are very often used for exactly this semantic intent. I don't love it, but it's common practice in Go (and other null-enabled languages) and I think changing this now may annoy current users of this library.

pherrymason commented 1 month ago

I'm ok with it!

tliron commented 1 month ago

Thank you for the discussion, it's useful. I'm not opposed to changing in the future. I will re-open this as an issue to document the use of nils, at the very least.

pherrymason commented 1 month ago

Thank you too. I'm not an expert with Go, explaining me the context allowed me to understand better the design.