- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Creating Java class instance from Scala.
Tue, 2011-05-24, 07:28
Hi.
I have a difficulty to convert a sample Java program into Scala. (Sample application in htmlparser)
The problem is in the java code:
NodeFilter filter;
...
filter = new NodeClassFilter(LinkTag.class); // how to make this line works in Scala?
In the NodeClassfilter.java:
public class NodeClassFilter implements NodeFilter {
...
public NodeClassFilter(Class cls) { ... }
}
I have no experience in Java, and read barely "Programming in Scala", so I don't know
how to create "filter" variable in Scalr. I tried with similar syntax, but failed:
scala> var filter = new NodeClassFilter(LinkTag.class)
<console>:1: error: identifier expected but 'class' found.
var filter = new NodeClassFilter(LinkTag.class)
^
scala> var filter = new NodeClassFilter(new LinkTag)
<console>:12: error: type mismatch;
found : org.htmlparser.tags.LinkTag
required: java.lang.Class[_]
var filter = new NodeClassFilter(new LinkTag)
^
I suspect that ".class" member in Java connected to something like run-time class information or so...
Could you guide me how to code in Scalr for the line noted above?
Thank you in advance.
Regards,
I have a difficulty to convert a sample Java program into Scala. (Sample application in htmlparser)
The problem is in the java code:
NodeFilter filter;
...
filter = new NodeClassFilter(LinkTag.class); // how to make this line works in Scala?
In the NodeClassfilter.java:
public class NodeClassFilter implements NodeFilter {
...
public NodeClassFilter(Class cls) { ... }
}
I have no experience in Java, and read barely "Programming in Scala", so I don't know
how to create "filter" variable in Scalr. I tried with similar syntax, but failed:
scala> var filter = new NodeClassFilter(LinkTag.class)
<console>:1: error: identifier expected but 'class' found.
var filter = new NodeClassFilter(LinkTag.class)
^
scala> var filter = new NodeClassFilter(new LinkTag)
<console>:12: error: type mismatch;
found : org.htmlparser.tags.LinkTag
required: java.lang.Class[_]
var filter = new NodeClassFilter(new LinkTag)
^
I suspect that ".class" member in Java connected to something like run-time class information or so...
Could you guide me how to code in Scalr for the line noted above?
Thank you in advance.
Regards,
Tue, 2011-05-24, 08:17
#2
Re: Creating Java class instance from Scala.
Thank you very much!
And I do apologize to post into wrong mailing-list. I'll use scala-user instead.
Regards,
And I do apologize to post into wrong mailing-list. I'll use scala-user instead.
Regards,
On 5/23/11 11:28 PM, cinsk wrote:
> Hi.
>
> I have a difficulty to convert a sample Java program into Scala.
> (Sample application in htmlparser)
Hi cinsk! Welcome to scala. :-)
First though, while I will be happy to answer your question, the mailing
list you really want is scala-user which is focused around answering
basic questions like yours:
http://www.scala-lang.org/node/199#scala-user
> The problem is in the java code:
>
> NodeFilter filter;
> ...
> filter = new NodeClassFilter(LinkTag.class); // how to make this
> line works in Scala?
>
Short answer: translate LinkTag.class to classOf[LinkTag]. (In my
experience this is actually something that is non-trivial to figure out
unless you know what you are looking for.)
> I suspect that ".class" member in Java connected to something like
> run-time class information or so...
>
Yes. Associated with every Java class is a Java *object* which is an
instance of java.lang.Class which contains a runtime representation of
that class. ".class" is not really a member per se as I understand it
so much as a special form that instructs Java that you are referring to
the runtime class object rather than the class itself, if that makes
sense. In Scala, the corresponding special form is classOf[...].
> Could you guide me how to code in Scalr for the line noted above?
filter = new NodeClassFilter(classOf[LinkTag])
> Thank you in advance,
You are welcome in advance. :-)
Cheers,
Greg