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

implementing java interfaces

6 replies
dmoshal
Joined: 2008-11-06,
User offline. Last seen 2 years 24 weeks ago.

Hi, me <- scala newbie, java 12 years

am trying to implement a simple java interface in scala:

interface Listener
{
public void message (Map map, String msg);
}

in Scala, I tried the following:

class Foo extends Listener
{
def message (map :Map[Any,Any], msg :String) :Unit = println ("test")
}

The eclipse plugin context help says:
"class Foo needs to be abstract, since method message in Trait Listener of type
(java.util.Map,java.util.String) Unit is undefined.

Any ideas???

David

Tony Morris
Joined: 2008-12-19,
User offline. Last seen 30 weeks 4 days ago.
Re: implementing java interfaces

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

In Scala (or Java either), Map[Any, Any] is not equivalent to Map.
Since Map takes two type variables, Map[_, _] is equivalent to just Map.

David Moshal wrote:
> Hi, me <- scala newbie, java 12 years
>
> am trying to implement a simple java interface in scala:
>
> interface Listener { public void message (Map map, String msg); }
>
>
> in Scala, I tried the following:
>
> class Foo extends Listener { def message (map :Map[Any,Any], msg
> :String) :Unit = println ("test") }
>
>
> The eclipse plugin context help says: "class Foo needs to be
> abstract, since method message in Trait Listener of type
> (java.util.Map,java.util.String) Unit is undefined.
>
> Any ideas???
>
> David
>
>
>

- --
Tony Morris
http://tmorris.net/

************************************
* Anteromedial Osseous Impingement *
************************************

http://www.ajronline.org/cgi/content/full/178/3/601
"can result in chronic ankle pain, especially in athletes and the
younger population (15-40 years old)"

http://radiographics.rsnajnls.org/cgi/content/figsonly/22/6/1457
"Soft-tissue and osseous impingement syndromes of the ankle can be an
important cause of chronic pain, particularly in the professional
athlete."

1. Take any person with soft tissue and osseous joint impingement from
trauma
2. Surgically tighten the joint ligaments particularly those in the
area of impingement.
3. When the patient complains of incredible and permanent pain, shrug
your shoulders.

Outcome
You'll find the patient at the mental hospital bordering psychosis.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFJj4MDmnpgrYe6r60RAk4dAJ9W75C/ObY8LtBTOGgmgMeByBC2SwCffdCu
HrarcDJQxaYFclFiTM+oA+k=
=gzRy
-----END PGP SIGNATURE-----

dmoshal
Joined: 2008-11-06,
User offline. Last seen 2 years 24 weeks ago.
Re: implementing java interfaces

This worked:

class SListener extends Listener
{
def message (header :java.util.Map[_,_], message :java.lang.String) :Unit =
{
println (message)
}
}

NOTE: I had to type the Map and String too.

Many thanks

Dave

dmoshal
Joined: 2008-11-06,
User offline. Last seen 2 years 24 weeks ago.
Re: implementing java interfaces

This worked:

class SListener extends Listener
{
def message (header :java.util.Map[_,_], message :java.lang.String) :Unit =
{
println (message)
}
}

NOTE: I had to type the Map and String too.

Many thanks

Dave

dmoshal
Joined: 2008-11-06,
User offline. Last seen 2 years 24 weeks ago.
Re: implementing java interfaces

Out of interest:

is java: Map <?, ?>

similar to Scala:

Map [_ ,_]

Tony Morris
Joined: 2008-12-19,
User offline. Last seen 30 weeks 4 days ago.
Re: Re: implementing java interfaces

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Yes, the topic you want to read up on is called Existential Types.
It's also similar to Map[T forSome { type T }, U forSome { type U }]

David Moshal wrote:
> Out of interest:
>
> is java: Map <?, ?>
>
> similar to Scala:
>
> Map [_ ,_]
>
>
>
>
>

- --
Tony Morris
http://tmorris.net/

************************************
* Anteromedial Osseous Impingement *
************************************

http://www.ajronline.org/cgi/content/full/178/3/601
"can result in chronic ankle pain, especially in athletes and the
younger population (15-40 years old)"

http://radiographics.rsnajnls.org/cgi/content/figsonly/22/6/1457
"Soft-tissue and osseous impingement syndromes of the ankle can be an
important cause of chronic pain, particularly in the professional
athlete."

1. Take any person with soft tissue and osseous joint impingement from
trauma
2. Surgically tighten the joint ligaments particularly those in the
area of impingement.
3. When the patient complains of incredible and permanent pain, shrug
your shoulders.

Outcome
You'll find the patient at the mental hospital bordering psychosis.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFJj5eSmnpgrYe6r60RAviQAJ4yQ2ANJPDGWYWq2U8TsENDfrudyACgtsly
PKr8Bgmx9tqYV2dpr/6t6wY=
=KIMY
-----END PGP SIGNATURE-----

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Re: implementing java interfaces
Better to remove the raw type from the original code.

2009/2/9 Tony Morris <tmorris@tmorris.net>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Yes, the topic you want to read up on is called Existential Types.
It's also similar to Map[T forSome { type T }, U forSome { type U }]

David Moshal wrote:
> Out of interest:
>
> is java: Map <?, ?>
>
> similar to Scala:
>
> Map [_ ,_]
>
>
>
>
>

- --
Tony Morris
http://tmorris.net/

************************************
* Anteromedial Osseous Impingement *
************************************

http://www.ajronline.org/cgi/content/full/178/3/601
"can result in chronic ankle pain, especially in athletes and the
younger population (15-40 years old)"

http://radiographics.rsnajnls.org/cgi/content/figsonly/22/6/1457
"Soft-tissue and osseous impingement syndromes of the ankle can be an
important cause of chronic pain, particularly in the professional
athlete."

1. Take any person with soft tissue and osseous joint impingement from
trauma
2. Surgically tighten the joint ligaments particularly those in the
area of impingement.
3. When the patient complains of incredible and permanent pain, shrug
your shoulders.

Outcome
You'll find the patient at the mental hospital bordering psychosis.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFJj5eSmnpgrYe6r60RAviQAJ4yQ2ANJPDGWYWq2U8TsENDfrudyACgtsly
PKr8Bgmx9tqYV2dpr/6t6wY=
=KIMY
-----END PGP SIGNATURE-----


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