Skip to content
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

Add methods from List/Map to Listing/Mapping #683

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

holzensp
Copy link
Contributor

Methods that "project out" of the type can be added to Listing and Mapping to avoid having to use toList()/toMap(). Methods that result in a Listing, resp. Mapping, are not included for performance and (lazy) semantics reasons, but methods like contains etc. can be defined straightforwardly.

var keys = getAllKeys();
var builder = new VmObjectBuilder(keys.getLength());
for (var key : keys) {
var member = VmUtils.readMember(this, key);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var member = VmUtils.readMember(this, key);
var value = VmUtils.readMember(this, key);

public VmDynamic toDynamic() {
var keys = getAllKeys();
var builder = new VmObjectBuilder(keys.getLength());
for (var key : keys) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using self.forceAndIterateMemberValues() here? This should be much more efficient than calling readMember() repeatedly because it iterates the amend hierarchy only once.

Zooming out, how about returning a Dynamic whose members delegate to the mapping? This would avoid the need to force the mapping, which seems desirable.

Same comment applies to other similar methods.


/// Converts this mapping to a [Map].
external function toMap(): Map<Key, Value>

/// Converts this mapping to a [Dynamic] object.
external function toDynamic(): Dynamic
Copy link
Contributor

@translatenix translatenix Oct 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about documenting that late binding and Mapping.default are not preserved?
"Returns a Dynamic that delegates to this mapping" would be a clear explanation of a good implementation.

@@ -220,6 +220,11 @@ public VmCollection.Builder<VmList> builder() {
return new Builder();
}

@TruffleBoundary
public static VmCollection.Builder<VmList> newBuilder() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Existing code uses VmList.EMPTY.builder() instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants