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

Transform an array into a buffer via Array.buffer. #387

Merged
merged 4 commits into from
Jun 13, 2022

Conversation

matthewhammer
Copy link
Contributor

Creates a buffer from an array.

q-uint
q-uint previously approved these changes Jun 13, 2022
Copy link
Contributor

@q-uint q-uint left a comment

Choose a reason for hiding this comment

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

Yes. 💯

@matthewhammer matthewhammer marked this pull request as ready for review June 13, 2022 17:07
),
Suite.test(
"buffer",
Array.buffer<Nat>([0, 1, 2, 3]).toArray(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Are the type parameters needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, I'm not sure. I copied the code (including the type parameters) from the prior test and adapted it. Will check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like the type arguments are indeed required:

arrayTest.mo:222.5-222.31: type error [M0098], cannot implicitly instantiate function of type
  <A>[A] -> Buffer<A>
to argument of type
  [Nat]
because implicit instantiation of type parameter A is under-constrained with
  Nat  <:  A  <:  Any
where
  Nat  =/=  Any
so that explicit type instantiation is required

https://github.com/dfinity/motoko-base/runs/6870423809?check_suite_focus=true#step:8:373

Copy link
Contributor

Choose a reason for hiding this comment

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

The type instantiation is necessary because buffer is invariant (right?) so there is no principal choice.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd add a doc comment and maybe consider adding a more general 'fromIter' method too, especially if we already have 'toIter'.

ggreif
ggreif previously approved these changes Jun 13, 2022
Copy link
Contributor

@ggreif ggreif left a comment

Choose a reason for hiding this comment

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

Looks fine to me!

Maybe change the PR title (type signature?)

@matthewhammer matthewhammer dismissed stale reviews from ggreif and q-uint via 7e86242 June 13, 2022 22:05
@matthewhammer matthewhammer changed the title Array.buffer Transform an array into a buffer via Array.buffer. Jun 13, 2022
@matthewhammer matthewhammer enabled auto-merge (squash) June 13, 2022 22:14
@matthewhammer matthewhammer merged commit d3b1081 into master Jun 13, 2022
@matthewhammer matthewhammer deleted the array-to-buffer branch June 13, 2022 23:17
@crusso
Copy link
Contributor

crusso commented Jun 14, 2022

Hmm, shouldn't that be called toBuffer (or maybe even preferably Buffer.fromArray to avoid coupling Array.mo to Buffer.mo) for consistency with other conversions?

And a (trivial) doc comment would be useful.

It's not too late to change since we haven't released this yet.

@crusso
Copy link
Contributor

crusso commented Jun 14, 2022

This PR is also related to #368.

@rossberg
Copy link
Contributor

Drive-by: agreeing with Claudio, this should be Buffer.fromArray.

@matthewhammer
Copy link
Contributor Author

matthewhammer commented Jun 15, 2022

Drive-by: agreeing with Claudio, this should be Buffer.fromArray.

To be clear, we are still talking about a module function within module Buffer, not a class method within class Buffer, right?

If so, fine. Otherwise, this suggestion doesn't make sense to me.

Update:

Nevermind. I am looking at the other PR, and I think it's clear we are all talking about a module member, not a class member.

#389 is ready for review.

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.

5 participants