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

restrict use of fluent to specified methods #5

Open
thigg opened this issue Jul 3, 2023 · 3 comments
Open

restrict use of fluent to specified methods #5

thigg opened this issue Jul 3, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@thigg
Copy link

thigg commented Jul 3, 2023

To avoid transforming login(String,String) into an extension of String, it would be nice if this could be limited to only transform methods to an Extension, which do have a specific annotation.

This feature could be optional, although I would always enable it, I think...

@rogerkeays
Copy link
Owner

rogerkeays commented Jul 3, 2023

Fluent only transforms object method calls to static method calls, not the other way around. Your static login(String, String) method call will never be transformed. I think what you are saying is you want to prevent developers from using String.login(String) ? Note also, only method calls are transformed, not method declarations.

@thigg
Copy link
Author

thigg commented Jul 3, 2023

Yes sorry for my unclear wording here.
Lets say I have a

public static void log(String msg)

in my codebase.
The plugin does now transform a call on String::log to this method if it is in scope.
I would prefer to be able to opt-in only here. Especially if ide support becomes a thing.
I can also see, that this implies, that I can not simply do a .assertEquals("abc") anymore easily. But maybe there would be ways to work around that.

I currently see 3 benefits in making it explicit with annotations:

  • the annotation is also documentation about the intended use of the method
  • the annotation prevents accidental use
  • the annotation keeps the scope clean to make method suggestions in IDEs more useful

@thigg thigg changed the title Optionally only transform Methods with annotation Optionally only transform calls for Methods with annotation Jul 3, 2023
@rogerkeays rogerkeays changed the title Optionally only transform calls for Methods with annotation restrict use of fluent to specified methods Jul 4, 2023
@rogerkeays
Copy link
Owner

rogerkeays commented Jul 4, 2023

Okay, I broadened the scope of this issue a bit so we aren't constrained to annotations. My prototype for Fluent actually only transformed calls to methods ending in "EX", (e.g. "hello".duplicateEX()"). This was very simple, but didn't quite satisfy me.

Lombok and Manifold use annotations, Kotlin has a different syntax for declaring extensions methods (fun String.duplicate() {...}), and C# requires a this modifier on the first parameter of the method declaration. D, on the other hand, uses uniform function call syntax, which allows any function to be called in a fluent style.

I think if Fluent were to add annotations, it would lose it's point of difference from Lombok and Manifold. Mostly, those annotations are for the IDE, anyway. Annotations would also mean you can't use anything from commons StringUtils (for example) as an extension method.

If we were to add new syntax, another option would be import extension org.apache.commons.StringUtils.* instead of import static. As it is, we basically assume anything you import static is an extension method. So maybe we need a way to disable extensions rather than enable them, as you suggested.

Personally, I'm okay with uniform function call syntax. It makes an IDE plugin more problematic, but I don't think we should be coding for our IDEs. For now, we also want to make minimal changes to the compiler so patching future versions is easier.

@rogerkeays rogerkeays added the enhancement New feature or request label Jul 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants