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
Using prototype in records #1316
Comments
Also to this suggestion, Derived classes inherit from input arguments: type Production = string list
type ProductionsCrew(mainProductions:Set<Production>, augmentedProductions:Set<Production>) =
member _.mainProductions = mainProductions
member _.augmentedProductions = augmentedProductions
type NullableCrew(crew:ProductionsCrew,symbols:Set<string>,nonterminals:Set<string>,terminals:Set<string>,nullables:Set<string>) =
inherit &crew //*
member _.symbols = symbols
member _.nonterminals = nonterminals
member _.terminals = terminals
member _.nullables = nullables
type GrammarCrew(crew:NullableCrew,firsts:Map<string,Set<string>>,lasts:Map<string,Set<string>>,follows:Map<string,Set<string>>,precedes:Map<string,Set<string>>) =
inherit &crew //*
member _.firsts = firsts
member _.lasts = lasts
member _.follows = follows
member _.precedes = precedes |
I'm confused. Your title says that this suggestion is for records, but your "existing way of approaching this problem" section uses classes. Which do you mean? Should this: type ProductionsCrew(mainProductions:Set<Production>, augmentedProductions:Set<Production>) =
member _.mainProductions = mainProductions
member _.augmentedProductions = augmentedProductions instead be written as this? type ProductionsCrew =
{ mainProductions: Set<Production>
augmentedProductions: Set<Production> } |
Prototypes are similar to inheritance, currently only classes can inherit in fsharp. Therefore, the current method is to use classes instead of records to implement prototypes. |
Record inheritance has already been suggested and rejected in #70. A syntax change would not overrule Don Syme's reasons against it. |
Do you have a specific use case you could describe? We may be able to find a better solution. |
I just went and looked at that and thought, cool - syntactic sugaring by the compiler to insert the 'inherited' fields to save typing them out again and then having 2 copies to maintain later. I didn't think of it involving the CLR type system at all, it would be erased by the compiler into a new flat record. Maybe if it were described that way the objection would change? |
In general, make separate suggestions in separate issues. I would not recommend suggesting this, though. If the inheriting class takes two instances of the inherited class as parameters, which is inherited? I couldn't guess by looking at the code. If you already have an instance of the inherited object, it sounds to me as though you might have a "has-a" relationship rather than an "is-a" relationship, so you might consider using a property rather than inheriting. |
@realparadyne I don't know, but that almost sounds more similar in intention to extracting a record with the common fields to use in both. Or might the proposed spread operator (#1253) help? |
@LyndonGingerich I thought of spread but that lets you make a new record instance at a point when you have instances of the other records you want to incorporate. It doesn't make a new, named type before you create instances of it. I'm not saying I have any use cases for this feature, but then again it's never been an option. |
It might. Don Syme suggested the use of the spread operator in type definitions in the linked issue, though he says he's "a bit ambivalent" on that. |
Records with prototypes can indeed be separated from normal records. Maybe He should be marked with The prototype and the prototype of JavaScript are the same, so the use cases of the prototype can refer to the prototype of JavaScript. One of the use cases is to have the function return all immutable calculation results. When you first calculate the return and many results, then another calculate has other results based on the results of the first step, and so on, the members of these results do not need to be nested, resulting in too many long name references. The definition of a type can concisely indicate the calculation process and dependency relationships. This is the desired effect: https://github.com/xp44mm/FslexFsyacc/blob/master/FslexFsyacc.Prototypes/ItemCoreCrews.fs This is the equivalent type definition by code generation https://github.com/xp44mm/FslexFsyacc/blob/master/FslexFsyacc/Yacc/ItemCoreCrews.fs These two are initialization codes: https://github.com/xp44mm/FslexFsyacc/blob/master/FslexFsyacc/Yacc/ProductionCrewUtils.fs https://github.com/xp44mm/FslexFsyacc/blob/master/FslexFsyacc/Yacc/ItemCoreCrewUtils.fs |
I'm confused. What are you responding to here? Unfortunately, I'm not familiar with JavaScript. If I understand correctly, you are creating records using helper functions, but you are frustrated that certain fields are nested too deeply. Do I understand correctly? Just to be sure, have you first tried moving fields to parent records to avoid nesting?
@realparadyne I honestly don't know. If you think your suggestion has not yet been addressed and would be useful, maybe you should open an issue and suggest it. |
correctly, certain fields are nested too deeply. I cannot provide the name of the intermediate path because as long as the target field name is sufficient to locate it.
Yes, but apart from No moving, the data is still in place, i hope fsharp can omit the intermediate special suggest: let prods = g.augmentedProductions instead of: let prods = g.prototype.prototype.augmentedProductions // any number of prototype |
Keep the traditional recording system unchanged and add a new data type that can be prototype. |
I propose we Use prototype in records
A prototype chain is a type inheritance, Referencing fields in prototype records can omit prototype
All function parameters of the base class can be applied to the data of the derived class.
The existing way of approaching this problem in F# is ...
Pros and Cons
The advantages of making this adjustment to F# are ...
Simpler code
Reduced one base class construction when initializing derived classes
The disadvantages of making this adjustment to F# are ...
The prototype class must be reference transparent, with the same input parameters, and the class must be equivalent.
I don't know how to solve the problem of duplicate names on the prototype chain yet. I suggest not allowing duplicate names for now.
Extra information
Estimated cost (XS, S, M, L, XL, XXL):
Related suggestions: (put links to related suggestions here)
Affidavit (please submit!)
Please tick these items by placing a cross in the box:
Please tick all that apply:
For Readers
If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.
The text was updated successfully, but these errors were encountered: