-
Notifications
You must be signed in to change notification settings - Fork 217
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
Proposal: Data (and struct) syntax #2061
base: master
Are you sure you want to change the base?
Proposal: Data (and struct) syntax #2061
Conversation
Thank you for the kickstart. Howëver, this design needs more innovation to be suitable for the anonymous superclass-less design that I and many prefer. Measure = Data.define(:amount, :unit) do
self::TAU = 6.28r
end If RBS class Measure = Data(amount: Integer, unit: String)
TAU: Rational
end This idea of RBS DSLs is great 👍. I suggest making this system extensible for OOP-based DSLs (e.g., Delegate, FFI/Fiddle) to tap in. The RBS plugin (whatever form it is) provides handling code that generates the corresponding raw signatures (in the same spirit as soutaro/rbs-inline#76). class SimpleStruct = FFI::Struct(value: Float)
end Note that type args (AKA. generics) are in a tuple list ( class A < Data[a: Integer, b: String]
end This can even open the doors to “heterogenous type args” by combining the thoughts above. # 0: Integer, 1: String
def []: (0) -> Integer
| (1) -> String
# a: Integer, b: String
def []: (:a) -> Integer
| (:b) -> String |
@HoneyryderChuck Thank you for this PR. I think there are several things to consider:
|
Thx for the feedback sofar!
@ParadoxV5 Indeed, this syntax does not address that. I think it puts us a step closer by providing a "middle ground" with an anonymous class with dynamic method injection, but the thing you're asking should be its own separate contribution; namely, RBS needs a syntax for anonymous class which supports patterns such as
I find that suggestion interesting. I don't know enough of RBS internals much to propose such a plugin system (I don't think there is one now, right? 🤔 ), but I could see this being useful for cases such as dynamic method injection potentially, i.e. the case you stated with Delegate, where RBS internally wouldn't need to fiddle with the concept of what a "delegated" class is, and that would be left for the library to provide. Not planning yet to solve the delegate issue, and data/struct are core classes (so should be solved in RBS core), but that could be done, as long as there is an agreement on which RBS syntax can be "bailed out" to external plugins (and how).
It could, I just thought it'd be clearer to distinguish it from the generic declarations. Another idea would be to use brackets, which would map well to how records are typed (but could be confusing for Data/Struct? maybe?): # generic example
class A < B[C]
# current proposal
class A < Data(a: Integer)
# proposal with brackets
class A < Data{a: Integer}
Perhaps, didn't want to go that far 😅 but that could be an avenue for tuples/records.
@soutaro rbs-inline does not (yet) support other injected methods beyond |
bd70d45
to
131ad1d
Compare
d36cdb0
to
f1425c8
Compare
d6621ab
to
c0b010e
Compare
so that if .new gets overridden, it gets properly called
allows declaring and typing variables, inject inherited methods OTTB
c0b010e
to
e1af69f
Compare
@soutaro I think the PR is at a point where I need some feedback on the direction, i.e. whether it's worth pursuing it or not. I've coalesced into using brackets, i.e. Spent more time than I would have wished fixing the cli "runtime" prototype generation. The main reason was that, because I'm using The |
Curly braces looks strange as an “operator”, maybe because no text-based languages’ve ever used it.
Could square brackets
[P.S.] Well, this system also look like generics to me – type kwargs, if you will 😁.
Isn’t
I partially retract my review by specifing my opinion: RBS
|
I'd say no. Consider that the parsing code reuses record parsing primitives. The same limitations I found using the initial proposal of
That's a question for @soutaro . I just tried to integrate my change with the existing system, which had already some support for Data/Struct value objects. The goal of the PR is not to propose an alternative system.
I believe the RBS notation for generics uses
I agree. Current |
This is a proposal on how to solve the
Data
andStruct
usage, which currently has to be hand-stitched and is often times incomplete (lack of#members
, for example). Essentially it boils down to this:The generated type will have the corresponding attribute readers, correct initiallize signatures,
#members
, and all that, as well as correct class innheritance hierarchy (anonymous dynamic class in betweenA
andData
with all the aforementioned methods).Implementation-wise, this patch reuses code from record attibutes to parse the class members, and provides an exceptional path for classes inheriting from
Data
(a similar solution could be built forStruct
).I've left this purposedly raw and incomplete to get some early feedback, before I go too far on an unwanted path.
cc @soutaro