-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Traits for java! #1
Comments
Thank you for posting your ideas. I had been working on PHP for many years before moving to Java 2 decades ago. It would be great to leave a sample code snippet further explaining the traits for Java. Also, do you mean those 3rd party libraries will be transpiled at source code level to fulfill the traits? Jaspiler is designed to do such work. It's just there is a long way to go. |
@caoccao Sorry for the wait, on the code sample of a trait on php here is a sample: Traits in PHP are a mechanism for code reuse in single inheritance languages such as PHP. They allow you to define methods that can be reused across multiple classes. Here's a simple example demonstrating how to define and use a trait: Defining a TraitFirst, you define a trait. A trait is defined similarly to a class, but instead of extending another class, you use the trait Reader {
public function add($var1, $var2) {
return $var1 + $var2;
}
}
trait Writer {
public function multiply($var1, $var2) {
return $var1 * $var2;
}
} Using Traits in a ClassTo use these traits in a class, you simply include them using the class File {
use Reader;
use Writer;
public function calculate($var1, $var2) {
echo "Result of addition: ". $this->add($var1, $var2). "<br>";
echo "Result of multiplication: ". $this->multiply($var1, $var2);
}
} Instantiating and Using the ClassFinally, you can instantiate the class and call its methods, which now include those defined in the traits. $o = new File();
$o->calculate(5, 3); This example demonstrates how traits can be used to encapsulate behavior that can be shared among multiple unrelated classes, enhancing code reuse and reducing duplication. It is like I suppose macros, it almost simply copies your code in trait into your class, and also checks for conflicts and ignores duplicates, so you can use a trait twice or further down as long as you maintain the method signature. It is almost like an java's default method in interfaces, but consider you have ability to declare and access members. Issue with it It can be messy if misused, and i suppose that's the reason why it is not included in java with its strict nature, but however for an experienced dev, It can be a huge neat feature to use to structure your code. |
Another feature I would direly need for java type system, is Union in Generics. check it out in php and in typescript, It enables you to specify multiple gentypes out of a select. In java the trick to do this is you will have to have those types(classes or interfaces) have to use a common parent, then specify the parent, however at times this may not be the case. For example the types are within a library, or something. Consider you want to write a single concise method that accepts Line or Point but not Area -Primitives from a graphics library, you either have to overload your methods have 2 duplicate methods or with an internal one that fleshes out the common logic..... etc., I mean its a mess. It would be simple to be able to just filter the 2 types or any abstract combination of types. How I envision the library implementing this, is to create those interfaces that represent the combination of types internally or overload and just duplicate your methods for you. Just something. Anyway this is a good idea we have here of a jaspiler I really hope for it to grow big like other projects such as Babel that can add features to a language without affecting the base definition of the langauge. Especially syntactic sugars. |
Those ideas make sense. Thank you. The problems on my desk are:
I expect it to be a community driven project which implies who needs that who implements that. Please feel free to share your feedbacks. Thank you. |
Have you ever used php? especialy Laravel. do you understand how good traits can help especially in initial stages where you have not really defined your model to normalize to actual inheritance structure, or you have constraints over different models that extend from different ancestors and you dont have ability to edit them as they are part of a library or api, yet you want to add a common spec on all of them.
Yes, this is a huge feature i have ever desired in java. I wish to see this project grow to enable us to add our own features to java of any level or version.
The text was updated successfully, but these errors were encountered: