-
Notifications
You must be signed in to change notification settings - Fork 215
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
so that if .new gets overridden, it gets properly called
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
131ad1d
to
d36cdb0
Compare
allows declaring and typing variables, inject inherited methods OTTB
d36cdb0
to
f1425c8
Compare
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