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

Computed attribute value in the xml literal requires explicit call to the toString() method. #1787

Closed
scabug opened this issue Mar 11, 2009 · 3 comments

Comments

@scabug
Copy link

scabug commented Mar 11, 2009

It looks inconsistent when values inside the xml elements are implicitly converted to string but values in the xml attributes are not.

Reproducing:

scala> <x>{2+2}</x>
 
res2: scala.xml.Elem = <x>4</x>

scala> <x y={2+2} />

<console>:5: error: overloaded method constructor UnprefixedAttribute
with alternatives 
(String,Option[Seq[scala.xml.Node]],scala.xml.MetaData)scala.xml.UnprefixedAttribute 
<and> 
(String,String,scala.xml.MetaData)scala.xml.UnprefixedAttribute 
<and> (String,Seq[scala.xml.Node],scala.xml.MetaData)scala.xml.UnprefixedAttribute cannot be applied to (java.lang.String,Int,scala.xml.MetaData)
       <x y={2+2} />
        ^

scala> <x y={(2+2).toString} /> 
 
res4: scala.xml.Elem = <x y="4"></x>
@scabug
Copy link
Author

scabug commented Mar 11, 2009

Imported From: https://issues.scala-lang.org/browse/SI-1787?orig=1
Reporter: Dan (lifecoder)

@scabug
Copy link
Author

scabug commented May 20, 2012

@retronym said (edited on May 20, 2012 8:12:06 AM UTC):
Notes in passing:

https://github.com/scala/scala/blob/8c95273b/src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala#L216

Elaborating on that comment, simply adding toString here would not preserve these:

scala> <a href={None}/>
res6: scala.xml.Elem = <a/>

scala> <a href={Some(new xml.Atom("xxx"))}/>
res7: scala.xml.Elem = <a href="xxx"/>

The desugaring:

$ scala -Xprint:parser -e "<a b={1+1}>{1+1}</a>"

              var $md: _root_.scala.xml.MetaData = _root_.scala.xml.Null;
              $md = new _root_.scala.xml.UnprefixedAttribute("b", 1.$plus(1), $md);
              new _root_.scala.xml.Elem(null, "a", $md, $scope, ({
                val $buf = new _root_.scala.xml.NodeBuffer();
                $buf.$amp$plus(1.$plus(1));
                $buf
              }: _*))

For elements, the implicit toString comes (eventually) because the added expressions are routed through NodeBuffer#&+.(https://github.com/scala/scala/blob/8c95273b/src/library/scala/xml/NodeBuffer.scala)

But it's really hard to retrofit this for attributes without (theoretically) breaking existing code (in particular becase the constructor for UnprefixedAttribute is overloaded).

@scabug
Copy link
Author

scabug commented Jul 17, 2015

@SethTisue said:
The scala-xml library is now community-maintained. Issues with it are now tracked at https://github.com/scala/scala-xml/issues instead of here in the Scala JIRA.

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.

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

No branches or pull requests

1 participant