-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Method to share fields between types #7442
Comments
The discussion started here: https://groups.google.com/forum/?fromgroups=#!topic/julia-users/c17XiHNzyHc |
And @pbazant comment in #4935 is spot on: The following is worth reading http://ptgmedia.pearsoncmg.com/images/020163371x/items/item33.html |
I read the link you provided (admittedly, I haven't had time to read the entire piece), but I'm not sure I understand the starting premise. I don't see the point of creating an assignment operator for the sub-types. It seems more like an argument for not going with that design rather than not having fields in abstract types. |
Could you please make a concrete proposal of what you would like to see in Julia? #4935 and #1974 are very different and there have been various concerns that have been raised in these issues. In your proposal you say that "writing the interface is cumbersome". But even with abstract types with fields you would have to write the interface. Or is your idea to use fields as interface? #3292 is a proposal to delegate methods from fields to composite types that would make this part a little less type intensive. |
I liked the proposal of having abstract classes with fields. The biggest issue people seemed to have with it (e.g. in the thread and by @StefanKarpinski later) was how to deal with constructors. Other languages (e.g. Scala) have solved this problem by disallowing the abstract type to have constructors. I know this was shot down before because the child class will be affected by changes to the abstract class. However, if you think about the abstract types as interfaces, this should be the case. Additionally, you already have that problem with the grouping method. Thus, abstract types with fields doesn't add any new problems, but it does solve a few. I'd also love to being able to subclass from multiple abstract types. I know this is being called multiple inheritance, but I think it's closer to Scala's mixins than c++ or Python's MI. I know there are some unresolved issues to the multiple parents, so I'm not arguing as strongly for this, though I would really love to see this as a feature. The code could look like:
Seems like a much cleaner design to me than having to define all the interface methods for the separate state type. Having a The proposal of overloading the '.' operator might also be able to fix the problem, although it's not as clear to me if that will actually cut down on the code written. However, it might lend itself well to letting macros generating the messy code. |
Ok lets look at your example how this could be written with current julia
So there is no need to define any interface methods. The key thing here is that the So comparing your method with mine, the only negative thing is that the fields are repeated. And regarding this I have to agree that abstract types with fields are nice. But IMHO this is not a major thing that is solved here. Its just field repetitions. All the methods which are the more important thing can already be written as if the fields would be there. This is a major difference compared to Java and I think repeating methods or introducing unnecessary boilerplate code for internal accessors is what makes this annoying. Regarding the super constructor: You can also already have this. consider the following:
But again, I am not really against abstract types with field. Its just that there are more pressing issues in my opinion: abstract multiple inheritance #5 and formal interface definitions #6975. |
Closing as dup of various existing issues. |
@tknopp In this example it doesn't look like too much work to simply copy code, but any code base that gets much larger you will run into maintainability issues. If you have to change a field you might have a lot of subclasses to update (which may be strewn across many classes). As for the priority, I guess that's a subjective and dependent on what you're doing. I also think multiple inheritance is important. It's just that this issue is affecting my code right now. Also, I suspect both issues will affect each other. As for |
@JeffBezanson I agree that this issue is covered in other places, but I think those issues provide a specific solution. The thought was to keep an issue that was non-implementation specific (i.e. the point is some method to deal with this design decision whether its by abstract types with fields or overload the If it's decided that this isn't something Julia should do, then it makes sense to close it. I won't argue the point further -- I really like Julia and am trying to use it for my various projects. Whether it's something you guys decide is appropriate for the language is another matter. But I thought it was worth voicing my concern as well as others I know who are contemplating using Julia. |
@abeschneider Yes of course priority is subjective (as this whole issue). The point I wanted to make is that for MI there is no real workaround and projects like Gtk.jl really need this. Java interfaces do support this and without it one is really limited in various situations. Abstract types with fields can be relatively easily emulated and your last concern about the non-locality of the fields can be worked around by using a macro that contains the field declarations. But again, I am not against it. My vote is for disallowing field overloads and allowing abstract types with fields. Please note that closing this issue does not mean that it is not a good idea. It just that #4935 is the "official" issue for the thing you want to see and it is easier for all if the discussions are not spread across various issues. |
@tknopp That's fair. I've been reading 'you can already do that' as not being important and probably not making it into the language. I am mostly trying to determine if there feature will eventually be included in the future and am fine repeating code for now. |
In a real-life example that has been simplified, I have the design:
Unfortunately this code violates DRY and becomes difficult to maintain as I add more fields (and there are many more). One way to fix this is by creating a type to hold states:
However, this creates a problem that I have a tight-coupling with Player and PlayerState. Specifically, I need to know that every Player has a field called 'state' and mandate that other people do the same thing. If this is violated, the error will not be obvious as to why.
I can get around this problem by doing:
So I now have a well-defined interface that solves the previous problem, but also introduces another issue: writing this interface is cumbersome. With a decent number of fields (>> 2), the code quickly gets ugly.
I know this may be considered a repeat of what other people have written for other issues and that there exists multiple proposals for fixing these type of problems (e.g. including fields in abstract types or overloading the '.' operator). However, I thought it might be more productive to put the general problem down without suggesting a specific solution (since those exist already).
The text was updated successfully, but these errors were encountered: