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

Default implementations in the interfaces #54

Open
wants to merge 8 commits into
base: master
from

Conversation

Projects
None yet
3 participants
@dmitryhryppa
Copy link

dmitryhryppa commented Jan 19, 2019

interface IUser {
    //Fields marked as default should be initialized here;
    default private var email:String = "";
    
    //Methods marked as default should be implemented here;
    default public function isEmailValid ():Bool {
        /* ... */ 
        return false;
    }
    
    //This method is still required to be implemented by childs
    public function doSomething ():Void;
}

class Student implements IUser {
    //This method is required by IUser
    public function doSomething ():Void {
        isValidEmail (); // this method can be used here, implementation will be injected from the interface
    }
} 

class Teacher implements IUser {
    //This method is required by IUser
    public function doSomething ():Void {  }

    //Discard default impl, and use it's own
    public function isValidEmail ():Bool {
        return false;
    }
} 

Rendered version

dmitryhryppa added some commits Jan 19, 2019

@ncannasse

This comment has been minimized.

Copy link
Member

ncannasse commented Jan 20, 2019

What you might want here instead is similar to Java abstract methods, that forces children classes to implement certain methods defined in their superclass.

@dmitryhryppa

This comment has been minimized.

Copy link
Author

dmitryhryppa commented Jan 21, 2019

@ncannasse

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.
That may improve Haxe as a language and made it more rich as for me, and reduce unnecessary boilerplate code in some situations.

@skial skial referenced this pull request Jan 23, 2019

Closed

Haxe Roundup 464 #581

1 of 1 task complete
@Aurel300

This comment has been minimized.

Copy link

Aurel300 commented Feb 16, 2019

I just wanted to add that this is already possible using macros, e.g. with tink_lang partial implementations (though implementing this manually in macros is pretty easy also).

But I think proper traits (which is essentially what this is, given that your default allows both variable and method fields) might be interesting to have without having to go into macroland.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.