-
Notifications
You must be signed in to change notification settings - Fork 288
Document Metamodel::TypePretense and Metamodel::MethodDelegation #3352
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
Open
Kaiepi
wants to merge
5
commits into
Raku:main
Choose a base branch
from
Kaiepi:metamodel-coercionhow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
aecd526
Document Metamodel::TypePretense and Metamodel::MethodDelegation
Kaiepi b1716d4
Document role type pretense and method delegation behaviour
Kaiepi d660828
Correct Metamodel::TypePretense and Metamodel::MethodDelegation subkinds
Kaiepi 7caaa30
Clarify Metamodel::MethodDelegation.delegate_methods_to's documentation
Kaiepi 7f6f2c7
Clarify how roles typecheck in Metamodel::TypePretense
Kaiepi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
=begin pod :kind<Type> :subkind<role> :category<metamodel> | ||
|
||
JJ marked this conversation as resolved.
Show resolved
Hide resolved
|
||
=TITLE role Metamodel::MethodDelegation | ||
|
||
=SUBTITLE Metarole for delegating method dispatch | ||
|
||
role Metamodel::MethodDelegation { } | ||
|
||
I<Warning>: this role is part of the Rakudo implementation, and is not | ||
a part of the language specification. | ||
|
||
Methods of C<Any> and C<Mu> can be invoked on roles, despite them not | ||
actually having these types as parents: | ||
|
||
=begin code | ||
role Role { } | ||
|
||
say Role.raku; # OUTPUT: «Role» | ||
say Role.^pun.^parents(:all).map(*.^name); # OUTPUT: «()» | ||
=end code | ||
|
||
C<Metamodel::MethodDelegation> is the metarole responsible for this | ||
behavior. Using the metamethods this provides, metaobjects can delegate | ||
method dispatch to another type object. This can be useful when | ||
implementing types that shouldn't store methods of their own through | ||
L<C<Metamodel::MethodContainer>|/type/Metamodel::MethodContainer>, but | ||
should still support method dispatch somehow. | ||
|
||
All this metarole does is provide an interface for storing a type object | ||
to delegate methods to and provide a default C<find_method> method for | ||
delegating method lookups to that type object if no other method lookup | ||
behavior for it exists; any other behavior related to methods is left up | ||
to the metaclasses that do this metarole to implement themselves. | ||
|
||
Because method delegation is a property of the metaclass for a HOW, not | ||
HOWs themselves, the C<delegate_methods_to> and | ||
C<delegating_methods_to> metamethods this metarole provides must be | ||
invoked directly through a metaclass or HOW, not with C<.^> syntax: | ||
|
||
=for code :preamble<role Role { }> | ||
say Role.HOW.delegating_methods_to.^name; # OUTPUT: «Any» | ||
|
||
This metarole is commonly used in combination with | ||
L<C<Metamodel::TypePretense>|/type/Metamodel::TypePretense>. | ||
|
||
=head1 Methods | ||
|
||
=head2 method delegate_methods_to | ||
|
||
method delegate_methods_to($type) | ||
|
||
Delegates methods to C<$type>. This should be a type object, but may be | ||
any object with a C<find_method> metamethod technically. | ||
|
||
=head2 method delegating_methods_to | ||
|
||
method delegating_methods_to() | ||
|
||
Returns the type object a metaobject is delegating methods to. | ||
|
||
=head2 method find_method | ||
|
||
method find_method($obj, $name) | ||
|
||
Looks up a method on the type object this metaobject is delegating | ||
method dispatch to. | ||
|
||
=end pod |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
=begin pod :kind<Type> :subkind<role> :category<metamodel> | ||
|
||
JJ marked this conversation as resolved.
Show resolved
Hide resolved
|
||
=TITLE role Metamodel::TypePretense | ||
|
||
=SUBTITLE Metarole for type pretenses | ||
|
||
role Metamodel::TypePretense { } | ||
|
||
I<Warning>: this role is part of the Rakudo implementation, and is not | ||
a part of the language specification. | ||
|
||
Any role will type-check as C<Mu>, C<Any>, and C<Cool>, but don't | ||
actually have these classes as parents: | ||
|
||
=begin code | ||
class Class { } | ||
role Role { } | ||
|
||
say Role ~~ Mu; # OUTPUT: «True» | ||
say Role ~~ Any; # OUTPUT: «True» | ||
say Role ~~ Cool; # OUTPUT: «True» | ||
|
||
say Class.^parents(:all).map(*.^name); # OUTPUT: «(Any Mu)» | ||
say Role.^pun.^parents(:all).map(*.^name); # OUTPUT: «()» | ||
=end code | ||
|
||
C<Metamodel::TypePretense> is the metarole that's responsible for this | ||
behavior. Using the metamethods this provides, types can C<pretend to | ||
be> other types, i.e. types can type-check as other types. This can be | ||
useful when implementing types that should not store parent types | ||
through | ||
L<C<Metamodel::MultipleInheritance>|/type/Metamodel::MultipleInheritance>, | ||
but should still type-check like other types somehow. | ||
|
||
All this metarole does is provide an interface for storing type objects | ||
in a HOW and provide a default C<type_check> method that allows types to | ||
type-check as the types they're pretending to be if no other | ||
type-checking behavior for it exists; any other behavior related to type | ||
pretenses are left up to the metaclasses that do this metarole to | ||
implement themselves. | ||
|
||
Because type pretenses are a property of the metaclass for a HOW, not | ||
HOWs themselves, the C<pretend_to_be> and C<pretending_to_be> | ||
metamethods this metarole provides must be invoked directly through a | ||
metaclass or HOW, not with C<.^> syntax: | ||
|
||
=for code :preamble<role Role { }> | ||
say Role.HOW.pretending_to_be.map(*.^name); # OUTPUT: «(Cool Any Mu)» | ||
|
||
This metarole is commonly used in combination with | ||
L<C<Metamodel::MethodDelegation>|/type/Metamodel::MethodDelegation>. | ||
|
||
=head1 Methods | ||
|
||
=head2 method pretend_to_be | ||
|
||
method pretend_to_be(@types) | ||
|
||
Makes all types for a type of HOW pretend to be any of the type objects | ||
in C<@types>. | ||
|
||
=head2 method pretending_to_be | ||
|
||
method pretending_to_be() | ||
|
||
Returns the type objects this type of HOW is pretending to be. | ||
|
||
=head2 method type_check | ||
|
||
method type_check($obj, $checkee) | ||
|
||
If C<$checkee> is the same object as C<$obj> or is of any of the types | ||
C<$obj> is pretending to be, returns C<1>, otherwise returns C<0>. | ||
|
||
=end pod |
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.
Uh oh!
There was an error while loading. Please reload this page.