-
Notifications
You must be signed in to change notification settings - Fork 59
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
Default implementations in the interfaces #54
Conversation
What you might want here instead is similar to Java abstract methods, that forces children classes to implement certain methods defined in their superclass. |
Well, that's a half of problem :) To be fair, I picked up default implementations from Java too. Both things are useful and flexible. Maybe it is possible to support both features? Abstract methods in classes and default implementations in interfaces. |
I just wanted to add that this is already possible using macros, e.g. with But I think proper traits (which is essentially what this is, given that your |
I don't want code in interfaces. I would be open to abstract methods Java-style though, though I didn't really think the implications through yet. |
I agree with @Simn. Interfaces should stay interfaces. |
Well, this idea actually comes from Java too: // A simple program to Test Interface default
// methods in java
interface TestInterface
{
// abstract method
public void square(int a);
// default method
default void show()
{
System.out.println("Default Method Executed");
}
} Example from here: https://www.geeksforgeeks.org/default-methods-java/ But, would be nice to see "classic" abstract classes (as @RealyUniqueName says) in Haxe instead of "default" things in interfaces. Why not :) |
I think static functions/variables would be convenient in enums and interfaces, otherwise you have to make pairs of classes, which can be annoying . |
Just chiming in to say, I like interface default implementations, they're very handy. I have built a macro implementation, and it's a little slow and a little hairy - so in that respect, it'd be nice if it was a built-in feature. In my implementation, I've (ab)used metadata, which happens to keep the implementation separated (in the metadata) out of the pure interface definition: package;
@:autoBuild(util.DefaultImplementations.apply_defaults({
some_var:"hello world",
get_is_bar:
{
return some_var == "bar";
},
set_is_bar:
{
// Note: variable name 'input_value' is defined in the interface below
if (input_value) some_var = "bar";
return input_value;
}
}))
interface IFoo
{
public var some_var:String;
public var is_bar(get,set):Bool;
public function get_is_bar():Bool;
public function set_is_bar(input_value:Bool):Bool;
} The most hairy part is that, while it's building fields for the given class, it doesn't know which interface triggered the macro. So it scans all the interfaces of the class (and parent classes) to find the one matching the fields provided in the metadata. It needs the interface, because while the metadata provides the
One downside of separating the interface definition from the implementation is that the method parameter names, e.g. ETA: Oh, and it's sometimes handy to some_method: @:final {
...
} |
I think that's perfectly valid:
|
This has been rejected by nay votes of @ncannasse, @nadako, @RealyUniqueName and @Simn. |
Rendered version