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

Feature Request: (or How-To) about call a method on Foldable when "T", "U" inherit from same interface #48

Open
kcmvp opened this issue Jul 8, 2024 · 3 comments

Comments

@kcmvp
Copy link

kcmvp commented Jul 8, 2024

type Foldable[T any, U any] interface { leftValue() T rightValue() U hasLeftValue() bool }

here is the my case:
1: I build a mo.Either base on a if/else, saying Either[A,B], A and B implement same interface C
2: I build a Foldable from the either. but I want to execute same function on the foldable.

right now the API must pass in two method "successFunc" and "failureFunc", for the case i describe seems redundant

by the way, of course, I can use case assert cast A or B to C. and then call the method.

@kcmvp kcmvp changed the title Feature Request: (Or How) about call a method on Foldable when "T", "U" inherit from same interface Feature Request: (or How-To) about call a method on Foldable when "T", "U" inherit from same interface Jul 8, 2024
@samber
Copy link
Owner

samber commented Jul 8, 2024

hi @kcmvp

I'm not sure to understand your requirements. Can you make a quick demo?

@kcmvp
Copy link
Author

kcmvp commented Jul 8, 2024

either := lo.IfF(typ.Interface(), func() mo.Either[*types.Interface, *types.Named] { return mo.Left[*types.Interface, *types.Named](typ.Type().Underlying().(*types.Interface)) }).ElseF(func() mo.Either[*types.Interface, *types.Named] { return mo.Right[*types.Interface, *types.Named](typ.Type().(*types.Named)) }) mo.Fold[*types.Interface, *types.Named, string](either, func(named *types.Named) string { panic("1:should be the same" + "2: the method may return nothing") }, func(t *types.Interface) string { panic("1:should be the same" + "2: the method may return nothing") })

@kcmvp
Copy link
Author

kcmvp commented Jul 8, 2024

image

1: in either case(case1, case2) the logic should be the same, as *types.Interface and *types.Named may inherit from same interface type.TypeSet (explict or implict)
So In my case I hope there is just one function as parameter as below

Fold[T, U, R any](f Foldable[T, U], process func(ts type.TypeSet) string) string
T -> *types.Interface
U -> *types.Named

image

just as I high-light in red, some times this method return nothing just do some side effect.

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

No branches or pull requests

2 participants