Skip to content

Added generic functions for context management#540

Merged
AllenDang merged 2 commits intoAllenDang:masterfrom
sgosiaco:generics
Aug 8, 2022
Merged

Added generic functions for context management#540
AllenDang merged 2 commits intoAllenDang:masterfrom
sgosiaco:generics

Conversation

@sgosiaco
Copy link
Contributor

@sgosiaco sgosiaco commented Aug 6, 2022

Hi @AllenDang @gucio321 I saw #517 and since I've been using generics a lot recently, I figured I'd try my hand at making the changes.

The GetState and SetState functions still exist on the context struct for backwards compatibility, but there are now generic versions of them.
I've also updated the "internal" code to use the generic version instead.
I've kept the assert still for GetState to catch/prevent any mixing up of types.

The "reason" behind [T any, genericDisposable[T]] is explained here
Although I'm using it to directly call Dispose, it helps enforce the Disposable interface and it's also needed to have the return type as a pointer of the generic type.

Feel free to make any changes that you see fit.

@sgosiaco
Copy link
Contributor Author

sgosiaco commented Aug 6, 2022

Also to explain a bit why adding the generic type directly to func (c *context) GetState[T Disposable] doesn't work.
It's because the type parameter needs to be defined directly on the struct in order for it to be available for use in a function tied to a struct, where something like this would "work" func (c *context[T Disposable]) GetState.
But in the usecase of context, it can't be tied directly to a type since it can hold many different states, this is why I opted to make it a generic function where you pass in the context.

@codecov-commenter
Copy link

Codecov Report

Merging #540 (a267bf4) into master (1b68148) will increase coverage by 0.22%.
The diff coverage is 24.00%.

@@            Coverage Diff            @@
##           master    #540      +/-   ##
=========================================
+ Coverage    2.86%   3.09%   +0.22%     
=========================================
  Files          32      32              
  Lines        3179    3138      -41     
=========================================
+ Hits           91      97       +6     
+ Misses       3087    3040      -47     
  Partials        1       1              
Impacted Files Coverage Δ
ClickableWidgets.go 0.00% <0.00%> (ø)
CodeEditor.go 0.00% <0.00%> (ø)
EventHandler.go 0.00% <0.00%> (ø)
ExtraWidgets.go 0.00% <0.00%> (ø)
ImageWidgets.go 0.00% <0.00%> (ø)
MemoryEditor.go 0.00% <0.00%> (ø)
ProgressIndicator.go 0.00% <0.00%> (ø)
SplitLayout.go 0.00% <0.00%> (ø)
TextWidgets.go 0.00% <0.00%> (ø)
Window.go 0.00% <0.00%> (ø)
... and 3 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@AllenDang AllenDang merged commit a816620 into AllenDang:master Aug 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants