Skip to content

implement String(::Base.Generator) constructor#61475

Open
xingzihai wants to merge 1 commit intoJuliaLang:masterfrom
xingzihai:implement-string-generator
Open

implement String(::Base.Generator) constructor#61475
xingzihai wants to merge 1 commit intoJuliaLang:masterfrom
xingzihai:implement-string-generator

Conversation

@xingzihai
Copy link
Copy Markdown

Summary

This PR implements a String(::Base.Generator) constructor, resolving the method error when attempting to call String on a Generator (e.g., from Iterators.map).

Problem

Currently, calling String(Iterators.map(c->c+1, "Hello, world")) results in a method error because there is no String method that accepts a Generator argument.

Solution

Add a new constructor:

String(g::Generator) = join(g)

This uses join to convert the Generator elements to a String, where each element is converted via print.

Example Usage

# Before: MethodError
String(x for x in ["a", "b", "c"])  # Now works: "abc"

String(Iterators.map(identity, "hello"))  # Now works: "hello"

Notes

  • The implementation uses join(g) which is a well-established method for converting iterables to strings.
  • Each element is converted to string via print, providing flexibility for different element types.
  • A docstring with examples has been added.

Closes #57072

Add a String constructor that accepts a Generator argument, resolving
the method error when calling String on Iterators.map results.

The implementation uses join(g) to convert Generator elements to a
String, where each element is converted via print.

Closes JuliaLang#57072
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.

implement String(::Base.Generator)

1 participant