- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Why this compiles?
Fri, 2011-11-11, 18:04
Hi list -
I do not understand why this compiles.
I think (((xml2\"a") headOption) toList) return a List, why List have method "\"?
pls help. thanks alot.
scala> val xml2= <xml><a><b></b></a></xml>
xml2: scala.xml.Elem = <xml><a><b></b></a></xml>
scala> (((xml2\"a") headOption) toList) \ "b"
res10: scala.xml.NodeSeq = NodeSeq(<b></b>)
I do not understand why this compiles.
I think (((xml2\"a") headOption) toList) return a List, why List have method "\"?
pls help. thanks alot.
scala> val xml2= <xml><a><b></b></a></xml>
xml2: scala.xml.Elem = <xml><a><b></b></a></xml>
scala> (((xml2\"a") headOption) toList) \ "b"
res10: scala.xml.NodeSeq = NodeSeq(<b></b>)
Fri, 2011-11-11, 18:57
#2
Re: Why this compiles?
Responding to linjie nie:
> I do not understand why this compiles.
> I think (((xml2\"a") headOption) toList) return a List, why List have
> method "\"?
> pls help. thanks alot.
>
> scala> val xml2=
> xml2: scala.xml.Elem =
>
> scala> (((xml2\"a") headOption) toList) \ "b"
> res10: scala.xml.NodeSeq = NodeSeq()
The list is automatically converted to a NodeSeq due to an implicit
conversion defined in NodeSeq's companion object as #seqToNodeSeq().
See:
http://daily-scala.blogspot.com/2010/04/companion-object-implicits.html
For smaller examples like this, the scalac '-X:print:typers' option can
help to spot the implicit.
Best regards,
Patrick
Fri, 2011-11-11, 19:17
#3
Re: Why this compiles?
It is a List -- he wrote toList.
On Fri, Nov 11, 2011 at 12:39 PM, Chris Twiner <chris.twiner@gmail.com> wrote:
On Fri, Nov 11, 2011 at 12:39 PM, Chris Twiner <chris.twiner@gmail.com> wrote:
It's a NodeSeq and part of the xml library not a List. The \ is a selector and in this case looks for children called a.
You can think of \ as a poor man's xpath
On Nov 11, 2011 6:04 PM, "linjie nie" <nielinjie@gmail.com> wrote:Hi list -
I do not understand why this compiles.
I think (((xml2\"a") headOption) toList) return a List, why List have method "\"?
pls help. thanks alot.
scala> val xml2= <xml><a><b></b></a></xml>
xml2: scala.xml.Elem = <xml><a><b></b></a></xml>
scala> (((xml2\"a") headOption) toList) \ "b"
res10: scala.xml.NodeSeq = NodeSeq(<b></b>)
Fri, 2011-11-11, 19:37
#4
Re: Why this compiles?
yeah apologies. Save and send right next together on the android
gmail, that and hills don't mix.
sent from my laptop...
On Fri, Nov 11, 2011 at 7:12 PM, Naftoli Gugenheim wrote:
> It is a List -- he wrote toList.
>
> On Fri, Nov 11, 2011 at 12:39 PM, Chris Twiner
> wrote:
>>
>> It's a NodeSeq and part of the xml library not a List. The \ is a
>> selector and in this case looks for children called a.
>>
>> You can think of \ as a poor man's xpath
>>
>> On Nov 11, 2011 6:04 PM, "linjie nie" wrote:
>>>
>>> Hi list -
>>>
>>> I do not understand why this compiles.
>>> I think (((xml2\"a") headOption) toList) return a List, why List have
>>> method "\"?
>>> pls help. thanks alot.
>>>
>>> scala> val xml2=
>>> xml2: scala.xml.Elem =
>>>
>>> scala> (((xml2\"a") headOption) toList) \ "b"
>>> res10: scala.xml.NodeSeq = NodeSeq()
>>>
>
>
Sat, 2011-11-12, 05:07
#5
Re: Re: Why this compiles?
I know implicit conversion, but I have not imported from NodeSeq.
I thought importting is required if you want use implicit conversion, did I miss anything here?
pls advise.
On Sat, Nov 12, 2011 at 1:37 AM, Patrick Roemer <sangamon@netcologne.de> wrote:
I thought importting is required if you want use implicit conversion, did I miss anything here?
pls advise.
On Sat, Nov 12, 2011 at 1:37 AM, Patrick Roemer <sangamon@netcologne.de> wrote:
Responding to linjie nie:
I do not understand why this compiles.
I think (((xml2\"a") headOption) toList) return a List, why List have
method "\"?
pls help. thanks alot.
scala> val xml2= <xml><a><b></b></a></xml>
xml2: scala.xml.Elem = <xml><a><b></b></a></xml>
scala> (((xml2\"a") headOption) toList) \ "b"
res10: scala.xml.NodeSeq = NodeSeq(<b></b>)
The list is automatically converted to a NodeSeq due to an implicit conversion defined in NodeSeq's companion object as #seqToNodeSeq().
See:
http://daily-scala.blogspot.com/2010/04/companion-object-implicits.html
For smaller examples like this, the scalac '-X:print:typers' option can help to spot the implicit.
Best regards,
Patrick
Sat, 2011-11-12, 13:17
#6
Re: Re: Why this compiles?
This answer sent from the laptop :-)
http://stackoverflow.com/questions/5598085/where-does-scala-look-for-imp...
From Patricks answer : "conversion defined in NodeSeq's companion object"
As its defined in the companion object its automatically searched there.
On Sat, Nov 12, 2011 at 5:01 AM, linjie nie wrote:
> I know implicit conversion, but I have not imported from NodeSeq.
> I thought importting is required if you want use implicit conversion, did I
> miss anything here?
> pls advise.
>
> On Sat, Nov 12, 2011 at 1:37 AM, Patrick Roemer
> wrote:
>>
>> Responding to linjie nie:
>>
>>> I do not understand why this compiles.
>>> I think (((xml2\"a") headOption) toList) return a List, why List have
>>> method "\"?
>>> pls help. thanks alot.
>>>
>>> scala> val xml2=
>>> xml2: scala.xml.Elem =
>>>
>>> scala> (((xml2\"a") headOption) toList) \ "b"
>>> res10: scala.xml.NodeSeq = NodeSeq()
>>
>> The list is automatically converted to a NodeSeq due to an implicit
>> conversion defined in NodeSeq's companion object as #seqToNodeSeq().
>>
>> See:
>> http://daily-scala.blogspot.com/2010/04/companion-object-implicits.html
>>
>> For smaller examples like this, the scalac '-X:print:typers' option can
>> help to spot the implicit.
>>
>> Best regards,
>> Patrick
>>
>
>
Mon, 2011-11-14, 01:57
#7
Re: Re: Why this compiles?
I see, really cool, thank you all very much.
On Sat, Nov 12, 2011 at 8:08 PM, Chris Twiner <chris.twiner@gmail.com> wrote:
On Sat, Nov 12, 2011 at 8:08 PM, Chris Twiner <chris.twiner@gmail.com> wrote:
This answer sent from the laptop :-)
http://stackoverflow.com/questions/5598085/where-does-scala-look-for-implicits
From Patricks answer : "conversion defined in NodeSeq's companion object"
As its defined in the companion object its automatically searched there.
On Sat, Nov 12, 2011 at 5:01 AM, linjie nie <nielinjie@gmail.com> wrote:
> I know implicit conversion, but I have not imported from NodeSeq.
> I thought importting is required if you want use implicit conversion, did I
> miss anything here?
> pls advise.
>
> On Sat, Nov 12, 2011 at 1:37 AM, Patrick Roemer <sangamon@netcologne.de>
> wrote:
>>
>> Responding to linjie nie:
>>
>>> I do not understand why this compiles.
>>> I think (((xml2\"a") headOption) toList) return a List, why List have
>>> method "\"?
>>> pls help. thanks alot.
>>>
>>> scala> val xml2= <xml><a><b></b></a></xml>
>>> xml2: scala.xml.Elem = <xml><a><b></b></a></xml>
>>>
>>> scala> (((xml2\"a") headOption) toList) \ "b"
>>> res10: scala.xml.NodeSeq = NodeSeq(<b></b>)
>>
>> The list is automatically converted to a NodeSeq due to an implicit
>> conversion defined in NodeSeq's companion object as #seqToNodeSeq().
>>
>> See:
>> http://daily-scala.blogspot.com/2010/04/companion-object-implicits.html
>>
>> For smaller examples like this, the scalac '-X:print:typers' option can
>> help to spot the implicit.
>>
>> Best regards,
>> Patrick
>>
>
>
It's a NodeSeq and part of the xml library not a List. The \ is a selector and in this case looks for children called a.
You can think of \ as a poor man's xpath
On Nov 11, 2011 6:04 PM, "linjie nie" <nielinjie@gmail.com> wrote: