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

when I know that an object has an specific method

6 replies
Guilherme Silveira
Joined: 2011-05-29,
User offline. Last seen 42 years 45 weeks ago.

Due to a Java interface that I implement, I have a java Object in my
hands which I am sure it has a .id method. Now I want to invoke:

def check[T <: {def id:Long}](who:T)

What is the best way to invoke it?

I could extract a Java interface, change the function definition to
receive it and tell the objects to implement it, using
.asInstanceOf[X] when invoking, but I wanted to know if there is a
simpler way without the extra interface.

Suggestions?

Regards

Guilherme Silveira
Caelum | Ensino e Inovação
http://www.caelum.com.br/

Paulo Siqueira
Joined: 2011-04-13,
User offline. Last seen 42 years 45 weeks ago.
Re: when I know that an object has an specific method

What is the best way to invoke it?

You mean invoke it from Java? Or you mean you want to 'force' the objects to have the 'id' method?
 
[]s,
 
--
Paulo "JCranky" Siqueira
Visit my blog: http://www.jcranky.com/
alberto_souza
Joined: 2010-02-19,
User offline. Last seen 32 weeks 5 days ago.
Re: when I know that an object has an specific method
I'm not sure if a got your idea but, with that kind of method signature you are already guaranteeing that the object that is being passed as parameter has the id method. If you want to change the def id:Long for something more semantic, you can use the structural types:
type Identifiable = {def id} 
Now you can do def check [T <: Identifiable... ] and your objects don't need to implement this "interface". 
Alberto 



On Sat, May 28, 2011 at 9:09 PM, Guilherme Silveira <guilherme.silveira@caelum.com.br> wrote:
Due to a Java interface that I implement, I have a java Object in my
hands which I am sure it has a .id method. Now I want to invoke:

def check[T <: {def id:Long}](who:T)

What is the best way to invoke it?

I could extract a Java interface, change the function definition to
receive it and tell the objects to implement it, using
.asInstanceOf[X] when invoking, but I wanted to know if there is a
simpler way without the extra interface.

Suggestions?

Regards

Guilherme Silveira
Caelum | Ensino e Inovação
http://www.caelum.com.br/

Guilherme Silveira
Joined: 2011-05-29,
User offline. Last seen 42 years 45 weeks ago.
Re: when I know that an object has an specific method

Hi guys,

Extracting the type and adding .asInstanceOf solves the problem
without having to change the currently existing Java code. Thanks:

---
class Client { val id = 2 }

type Identifiable = {def id}
def check[T <: Identifiable](who:T) { println("yep"); }

def doSomething(guy:Object) { check(guy.asInstanceOf[Identifiable]) }

doSomething(new Guy)
---

The compatibility issue is that my old API has a method definited like
here (receiving an Object).

Regards

Guilherme Silveira
Caelum | Ensino e Inovação
http://www.caelum.com.br/

On Tue, May 31, 2011 at 11:48 AM, Alberto SOUZA wrote:
> I'm not sure if a got your idea but, with that kind of method signature you
> are already guaranteeing that the object that is being passed as parameter
> has the id method. If you want to change the def id:Long for something more
> semantic, you can use the structural types:
> type Identifiable = {def id}
> Now you can do def check [T <: Identifiable... ] and your objects don't need
> to implement this "interface".
> Alberto
>
>
>
> On Sat, May 28, 2011 at 9:09 PM, Guilherme Silveira
> wrote:
>>
>> Due to a Java interface that I implement, I have a java Object in my
>> hands which I am sure it has a .id method. Now I want to invoke:
>>
>> def check[T <: {def id:Long}](who:T)
>>
>> What is the best way to invoke it?
>>
>> I could extract a Java interface, change the function definition to
>> receive it and tell the objects to implement it, using
>> .asInstanceOf[X] when invoking, but I wanted to know if there is a
>> simpler way without the extra interface.
>>
>> Suggestions?
>>
>> Regards
>>
>> Guilherme Silveira
>> Caelum | Ensino e Inovação
>> http://www.caelum.com.br/
>
>

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: when I know that an object has an specific method

stupid question:
why doesn't the compiler know?

Am 04.06.2011 15:52, schrieb Guilherme Silveira:
> Hi guys,
>
> Extracting the type and adding .asInstanceOf solves the problem
> without having to change the currently existing Java code. Thanks:
>
> ---
> class Client { val id = 2 }
>
> type Identifiable = {def id}
> def check[T <: Identifiable](who:T) { println("yep"); }
>
> def doSomething(guy:Object) { check(guy.asInstanceOf[Identifiable]) }
>
> doSomething(new Guy)
> ---
>
> The compatibility issue is that my old API has a method definited like
> here (receiving an Object).
>
> Regards
>
> Guilherme Silveira
> Caelum | Ensino e Inovação
> http://www.caelum.com.br/
>
>
>
> On Tue, May 31, 2011 at 11:48 AM, Alberto SOUZA wrote:
>> I'm not sure if a got your idea but, with that kind of method signature you
>> are already guaranteeing that the object that is being passed as parameter
>> has the id method. If you want to change the def id:Long for something more
>> semantic, you can use the structural types:
>> type Identifiable = {def id}
>> Now you can do def check [T <: Identifiable... ] and your objects don't need
>> to implement this "interface".
>> Alberto
>>
>>
>>
>> On Sat, May 28, 2011 at 9:09 PM, Guilherme Silveira
>> wrote:
>>> Due to a Java interface that I implement, I have a java Object in my
>>> hands which I am sure it has a .id method. Now I want to invoke:
>>>
>>> def check[T <: {def id:Long}](who:T)
>>>
>>> What is the best way to invoke it?
>>>
>>> I could extract a Java interface, change the function definition to
>>> receive it and tell the objects to implement it, using
>>> .asInstanceOf[X] when invoking, but I wanted to know if there is a
>>> simpler way without the extra interface.
>>>
>>> Suggestions?
>>>
>>> Regards
>>>
>>> Guilherme Silveira
>>> Caelum | Ensino e Inovação
>>> http://www.caelum.com.br/
>>

Guilherme Silveira
Joined: 2011-05-29,
User offline. Last seen 42 years 45 weeks ago.
Re: when I know that an object has an specific method

Isn't it just because the method definition explicitly says it has an
Object? Unless I force it with instanceOf, there is no way to know if
the method is there.

Regards

Guilherme Silveira
Caelum | Ensino e Inovação
http://www.caelum.com.br/

On Sat, Jun 4, 2011 at 11:04 AM, HamsterofDeath wrote:
> stupid question:
> why doesn't the compiler know?
>
> Am 04.06.2011 15:52, schrieb Guilherme Silveira:
>> Hi guys,
>>
>> Extracting the type and adding .asInstanceOf solves the problem
>> without having to change the currently existing Java code. Thanks:
>>
>> ---
>> class Client { val id = 2 }
>>
>> type Identifiable = {def id}
>> def check[T <: Identifiable](who:T) { println("yep"); }
>>
>> def doSomething(guy:Object) {  check(guy.asInstanceOf[Identifiable]) }
>>
>> doSomething(new Guy)
>> ---
>>
>> The compatibility issue is that my old API has a method definited like
>> here (receiving an Object).
>>
>> Regards
>>
>> Guilherme Silveira
>> Caelum | Ensino e Inovação
>> http://www.caelum.com.br/
>>
>>
>>
>> On Tue, May 31, 2011 at 11:48 AM, Alberto SOUZA wrote:
>>> I'm not sure if a got your idea but, with that kind of method signature you
>>> are already guaranteeing that the object that is being passed as parameter
>>> has the id method. If you want to change the def id:Long for something more
>>> semantic, you can use the structural types:
>>> type Identifiable = {def id}
>>> Now you can do def check [T <: Identifiable... ] and your objects don't need
>>> to implement this "interface".
>>> Alberto
>>>
>>>
>>>
>>> On Sat, May 28, 2011 at 9:09 PM, Guilherme Silveira
>>> wrote:
>>>> Due to a Java interface that I implement, I have a java Object in my
>>>> hands which I am sure it has a .id method. Now I want to invoke:
>>>>
>>>> def check[T <: {def id:Long}](who:T)
>>>>
>>>> What is the best way to invoke it?
>>>>
>>>> I could extract a Java interface, change the function definition to
>>>> receive it and tell the objects to implement it, using
>>>> .asInstanceOf[X] when invoking, but I wanted to know if there is a
>>>> simpler way without the extra interface.
>>>>
>>>> Suggestions?
>>>>
>>>> Regards
>>>>
>>>> Guilherme Silveira
>>>> Caelum | Ensino e Inovação
>>>> http://www.caelum.com.br/
>>>
>
>

Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.
Re: when I know that an object has an specific method

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

When I interact with an API that does nasty things like this (return
Object, return null, etc.), I tend to write an "unsafe" wrapper right
at its border, then rely on that wrapper from thereon.

The techniques for dealing with these specific issues is worth
addressing in a technical article imo, since they are very specialised
and the most effective solution is not always obvious.

On 05/06/11 00:20, Guilherme Silveira wrote:
> Isn't it just because the method definition explicitly says it has
> an Object? Unless I force it with instanceOf, there is no way to
> know if the method is there.
>
> Regards
>
> Guilherme Silveira Caelum | Ensino e Inovação
> http://www.caelum.com.br/
>
>
>
> On Sat, Jun 4, 2011 at 11:04 AM, HamsterofDeath
> wrote:
>> stupid question: why doesn't the compiler know?
>>
>> Am 04.06.2011 15:52, schrieb Guilherme Silveira:
>>> Hi guys,
>>>
>>> Extracting the type and adding .asInstanceOf solves the
>>> problem without having to change the currently existing Java
>>> code. Thanks:
>>>
>>> --- class Client { val id = 2 }
>>>
>>> type Identifiable = {def id} def check[T <:
>>> Identifiable](who:T) { println("yep"); }
>>>
>>> def doSomething(guy:Object) {
>>> check(guy.asInstanceOf[Identifiable]) }
>>>
>>> doSomething(new Guy) ---
>>>
>>> The compatibility issue is that my old API has a method
>>> definited like here (receiving an Object).
>>>
>>> Regards
>>>
>>> Guilherme Silveira Caelum | Ensino e Inovação
>>> http://www.caelum.com.br/
>>>
>>>
>>>
>>> On Tue, May 31, 2011 at 11:48 AM, Alberto SOUZA
>>> wrote:
>>>> I'm not sure if a got your idea but, with that kind of method
>>>> signature you are already guaranteeing that the object that
>>>> is being passed as parameter has the id method. If you want
>>>> to change the def id:Long for something more semantic, you
>>>> can use the structural types: type Identifiable = {def id}
>>>> Now you can do def check [T <: Identifiable... ] and your
>>>> objects don't need to implement this "interface". Alberto
>>>>
>>>>
>>>>
>>>> On Sat, May 28, 2011 at 9:09 PM, Guilherme Silveira
>>>> wrote:
>>>>> Due to a Java interface that I implement, I have a java
>>>>> Object in my hands which I am sure it has a .id method. Now
>>>>> I want to invoke:
>>>>>
>>>>> def check[T <: {def id:Long}](who:T)
>>>>>
>>>>> What is the best way to invoke it?
>>>>>
>>>>> I could extract a Java interface, change the function
>>>>> definition to receive it and tell the objects to implement
>>>>> it, using .asInstanceOf[X] when invoking, but I wanted to
>>>>> know if there is a simpler way without the extra
>>>>> interface.
>>>>>
>>>>> Suggestions?
>>>>>
>>>>> Regards
>>>>>
>>>>> Guilherme Silveira Caelum | Ensino e Inovação
>>>>> http://www.caelum.com.br/
>>>>
>>
>>

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