GithubHelp home page GithubHelp logo

Traits for java! about jaspiler HOT 4 OPEN

Whizboy-Arnold avatar Whizboy-Arnold commented on July 29, 2024
Traits for java!

from jaspiler.

Comments (4)

caoccao avatar caoccao commented on July 29, 2024

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.

from jaspiler.

Whizboy-Arnold avatar Whizboy-Arnold commented on July 29, 2024

@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 Trait

First, you define a trait. A trait is defined similarly to a class, but instead of extending another class, you use the use keyword within a class to include the trait's methods.

trait Reader {
    public function add($var1, $var2) {
        return $var1 + $var2;
    }
}

trait Writer {
    public function multiply($var1, $var2) {
        return $var1 * $var2;
    }
}

Using Traits in a Class

To use these traits in a class, you simply include them using the use keyword within the class definition.

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 Class

Finally, 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.

from jaspiler.

Whizboy-Arnold avatar Whizboy-Arnold commented on July 29, 2024

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.

from jaspiler.

caoccao avatar caoccao commented on July 29, 2024

Those ideas make sense. Thank you.

The problems on my desk are:

  1. It's half way done.
  • Code -> AST
  • AST Transformation
  • AST -> Code (Tedious work, requires weeks/months)
  1. The trait feature lacks IDE support.
  • Idea Extension
  • VS Code Extension

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.

from jaspiler.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.