This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Why this compiles?

7 replies
linjie nie
Joined: 2009-06-17,
User offline. Last seen 42 years 45 weeks ago.
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>)

Chris Twiner
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Why this compiles?

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>)

Patrick Roemer
Joined: 2009-02-18,
User offline. Last seen 42 years 45 weeks ago.
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

Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
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:

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>)


Chris Twiner
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
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()
>>>
>
>

linjie nie
Joined: 2009-06-17,
User offline. Last seen 42 years 45 weeks ago.
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:
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


Chris Twiner
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
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
>>
>
>

linjie nie
Joined: 2009-06-17,
User offline. Last seen 42 years 45 weeks ago.
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:
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
>>
>
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland