-
Notifications
You must be signed in to change notification settings - Fork 21
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
XML mode Scala escapes should consider implicit conversion to NodeSeq #3573
Comments
Imported From: https://issues.scala-lang.org/browse/SI-3573?orig=1
|
@harrah said: The first translates to new UnprefixedAttribute("attr", x, xml.Null) while the second translates to val buffer = new xml.NodeBuffer
buffer &+ x
new xml.Elem(null, "sometag", xml.Null, scope, buffer) !UnprefixedAttribute has overloaded constructors: new UnprefixedAttribute(key: String, value: String, next: MetaData)
new UnprefixedAttribute(key: String, value: Seq[Node], next1: MetaData)
new UnprefixedAttribute(key: String, value: Option[Seq[Node]], next: MetaData) The signature of &+(o: Any): NodeBuffer So, an attribute value must conform to This is also why <sometag attr={1}/> doesn't work, but this does: <sometag>{1}</sometag> In any case, this is essentially the same issue as #1787 and should probably be closed as a duplicate. |
@harrah said: scala> <sometag>{3}</sometag>
res1: scala.xml.Elem = <sometag>3</sometag>
scala> <sometag>{new Xmlable(3)}</sometag>
res2: scala.xml.Elem = <sometag>i = 3</sometag> In theory it should be source compatible (test suite passes), but I'm sure it breaks someone's code somewhere. #1787 requires a more invasive approach since type parameters aren't allowed on auxiliary constructors. |
Willis Blackburn (willisblackburn) said: I'd be surprised if there are too much existing code where (a) some class X is defined, (b) there's an implicit conversion from X to NodeSeq in scope, and (c) an expression evaluating to X is used within {}, but (d) the developer expects the X to be converted to NodeSeq via toString. One suggestion: If the method is called with an Iterable[T] and there is an implicit conversion from T to NodeSeq in scope, then IMHO the method should map the Iterable with the conversion function before passing it to addAny. |
@harrah said: Rather, the caller could define an implicit like: implicit def views[T](x: Seq[T])(implicit view: T => Seq[Node]): Seq[Node] =
x flatMap view With that you can nest arbitrarily: scala> val xs = Seq(3,4) map (new Xmlable(_))
xs: Seq[Xmlable] = List(Xmlable@6d172f8f, Xmlable@d338d3d)
scala <x>{Seq(xs,xs)}</x>
res4: scala.xml.Elem = <x>i = 3i = 4i = 3i = 4</x> |
@SethTisue said: Interested community members: if you consider this issue significant, feel free to open a new issue for it on GitHub, with links in both directions. |
I have a class that I can implicitly convert to NodeSeq. It looks something like this:
I'm surprised that when I use an instance of Xmltable this class in a XML-mode Scala escape expression (not sure exactly what the name for it is), Scala does not invoke the implicit conversion. It seems like it would make sense for Scala to attempt to convert the escape expression to a NodeSeq and only fall back on a default conversion (to Atom or Text via toString) if necessary.
Strangely Scala does use the implicit conversion if I use an Xmlable as an attribute.
The text was updated successfully, but these errors were encountered: