- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
when I know that an object has an specific method
Sun, 2011-05-29, 05:09
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/
Tue, 2011-05-31, 16:07
#2
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:
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/
Sat, 2011-06-04, 15:07
#3
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/
>
>
Sat, 2011-06-04, 15:07
#4
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/
>>
Sat, 2011-06-04, 15:27
#5
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/
>>>
>
>
Sat, 2011-06-04, 15:37
#6
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/
>>>>
>>
>>
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/