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] Cast support #648

Open
terminalsin opened this issue Dec 8, 2024 · 3 comments
Open

[Feature] Cast support #648

terminalsin opened this issue Dec 8, 2024 · 3 comments
Labels

Comments

@terminalsin
Copy link

terminalsin commented Dec 8, 2024

Cast support for extensions

Essentially bringing to extensions support for custom casting.

Main class would implement the static method:

public class MyObject {


    public static MyObject cast(Object object) {

   }
}

This would override the behaviour of checkcast and would instead call this method when casting. This allows for me to be able to do this:

(Expr) 1

To convert an integer to an expression with ease.

@terminalsin terminalsin changed the title [Feature [Feature] Cast support Dec 8, 2024
@terminalsin
Copy link
Author

Additional uses: Non related object conversion:

public class ConversionParty {
    public static void main(String[] args) {
        // Color conversions
        ColorX color1 = (ColorX) "#FF5733";  // From hex
        ColorX color2 = (ColorX) 16777215;   // From RGB int
        ColorX color3 = (ColorX) Arrays.asList(255, 87, 51);  // From RGB list

        // JSON conversions
        class User {
            public String name = "John";
            public int age = 25;
        }
        JsonObject json1 = (JsonObject) new User();  // POJO to JSON
        JsonObject json2 = (JsonObject) Map.of("key", "value");  // Map to JSON

        // Time conversions
        TimeX time1 = (TimeX) "1h30m";  // From string format
        TimeX time2 = (TimeX) 3600000;  // From milliseconds
        TimeX time3 = (TimeX) "45s";    // From string format
    }
}

@rsmckinney
Copy link
Member

This is a bit controversial, many would argue against it because it overloads the cast expression as a coercion expression, which makes code harder to understand. This is one of many "mistakes" for which C++ is notorious. Having been exposed to a lot of C++ magic, I mostly agree with the dissenters here, HOWEVER. . .

Manifold offers a perhaps better alternative to cast overloading with unit expressions. Instead of casting you invent your own abbreviated syntax to coerce or transform adjacent expressions, which results in concise, readable code.

You could use unit expressions to make your examples above look like this.

        // Color conversions
        ColorX color = rgb "#FF5733"; 
        ColorX color = rgb 255 87 51;
        ColorX color = 255r 87g 51b; 

        // Time conversions
        TimeX time = 1h 30m;
        TimeX time  = 3600000 msec;
        TimeX time = 45s; 

It's simple and you can do pretty much anything with it; it's fun :)

Check out the time/date example in the link above, also check out the manifold-science module and the Range API for more examples.

@terminalsin
Copy link
Author

I see, this alternative does make sense. Thank you for the quick feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants