-
Notifications
You must be signed in to change notification settings - Fork 126
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
Comments
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
}
} |
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. |
I see, this alternative does make sense. Thank you for the quick feedback! |
Cast support for extensions
Essentially bringing to extensions support for custom casting.
Main class would implement the static method:
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:
To convert an integer to an expression with ease.
The text was updated successfully, but these errors were encountered: