Repository navigation
CLR interop gaps: super into CLR constructors, CLR constants, strings for Object parameters - #31
Merged
Conversation
… Object
Three things that did not work when calling .NET from Ruby:
- A Ruby class derived from a CLR class could not pass arguments to the
base class's constructor: super in initialize reached Object#initialize
("wrong number of arguments (given 1, expected 0)"), and a base class
without a parameterless constructor could not be allocated at all
("define Doctor#new singleton method instead of Doctor#initialize").
super in initialize now runs the matching CLR base constructor on the new
instance, as base(...) does in a C# constructor - with keywords, splats,
protected constructors of abstract classes, through several Ruby levels.
A base class with a parameterless constructor is still constructed with
it on allocation (so an initialize without super works as before; super()
then does nothing more). Without one, new allocates the instance
unconstructed - its Ruby class set, so virtual calls from the base
constructor reach Ruby overrides - and raises if initialize never calls
super, which C# would not have compiled.
- CLR const and static readonly fields are constants of their class:
System::Math::PI, String::Empty, an enum's values, as Java's static final
fields are in JRuby. Properties and other static members stay methods -
a constant is cached where it is used, which DateTime.Now must not be -
and naming one as a constant (Task::Factory) says how to call it
(Task.Factory). Builtin classes such as Integer keep MRI's constants.
`::` on a type group (System::Nullable::GetUnderlyingType) qualifies as
its non-generic member, as a type group does where a module is expected.
- A Ruby String passed for a CLR method's Object parameter arrives as a
System.String instead of a MutableString, which CLR code that looks at
what it gets did not understand: ADO.NET's AddWithValue raised "No
mapping exists from object type MutableString", System.Text.Json
serialized its internals. JRuby passes a java.lang.String likewise. Ruby
library methods are unaffected. A string stored through an Object
parameter (ArrayList#Add) comes back as a System::String, which compares,
hashes and matches in case/when like the Ruby string it was and answers
Ruby String methods; only String === and #inspect's quotes tell them
apart.
ClrConstructor3 expected the old allocation error; it now gets the new one,
after the module's initialize has run.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
A Ruby subclass of Proc (or Array, Hash, String) whose initialize calls super reached the new CLR-constructor path, because those builtins are CLR types too, and Proc's constructors do not take a block's arguments: spec/core/proc/new_spec "called on a subclass of Proc" raised ArgumentError. super into a CLR constructor, and allocating unconstructed, are now only for plain CLR classes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #26 (and stacked on it): it closes the gaps #26 left in calling .NET from Ruby.
superininitializeruns the CLR base constructorA Ruby class derived from a CLR class could not pass arguments to the base constructor.
super(name)ininitializereachedObject#initializeand failed with "wrong number of arguments (given 1, expected 0)".Now
superininitializeruns the matching CLR base constructor on the new instance, asbase(...)does in a C# constructor. This works with:super(**opts)→ object initializer)Allocation:
initializewithoutsuperworks exactly as before (class MyForm < Form). A latersuper()does nothing more.newallocates the instance unconstructed: its Ruby class is set, so virtual calls from the base constructor reach Ruby overrides. Ifinitializenever callssuper, which C# would not compile,newraises a TypeError saying so.CLR constants
CLR
constandstatic readonlyfields are now constants of their class, as Java's static final fields are in JRuby. Examples:System::Math::PI,String::Empty, enum values (Color::Green).Properties and other static members stay methods, as they always were. A Ruby constant is cached where it is used, which
DateTime.Nowmust not be. Naming one as a constant now says how to call it:Other details:
Integer::MaxValueis still a NameError, andSystem::Int32.MaxValuestill works.::on a type group qualifies as its non-generic member:System::Nullable::GetUnderlyingType(...).Ruby strings for
ObjectparametersA Ruby String passed for a CLR method's
Objectparameter now arrives as aSystem.Stringinstead of IronRuby'sMutableString. CLR code that inspects what it gets didn't understandMutableString:AddWithValueraised "No mapping exists from object type MutableString".JRuby passes a
java.lang.Stringthe same way. Ruby library methods are unaffected.Behaviour change to be aware of: a string stored through an
Objectparameter, e.g.ArrayList#AddorDictionary[Object, Object]#[]=, comes back as aSystem::String.case/whenlike the Ruby string it was, and it answers Ruby String methods.String ===and#inspect's quotes tell the two apart.Tests/Interop/net/bcl/idictionary) pinned the oldinspectoutput ({:foo=>"bar"}, now{:foo=>'bar'}).Testing
ClrSuperConstructor1/2in the newClrSubclassingTests.cs, plusClrConstantFields1andClrStringObjectArguments1.ClrConstructor3expected the old allocation error and now gets the new one, after the module'sinitializehas run.ObjectSpace.each_objectones). spec/library: 6,398 examples, 0 failures, 0 errors.Tests/Interop/net):idictionaryinspect expectation described above.🤖 Generated with Claude Code