Re: object.getClass is still an error in annotation

Rodrigo Cano <[email protected]>
Newsgroups gmane.comp.lang.scala
Message-ID <CAM5b94ismPuKyXZgs+2RATQ7QzDFm9JrvsZ-tcHQjHzvcD9HhA@mail.gmail.com>
Its something like this, in scala when you want the class of a type, you do
classOf[MyType], classOf doesn't work for objects though:

scala> object O
defined object O

scala> classOf[O]
<console>:8: error: not found: type O
              classOf[O]

scala> classOf[O.type]
<console>:9: error: class type required but O.type found
              classOf[O.type]

Notice in the last error message that O.type exists, but its not a class
type. Now the only way you are left with to get the class of an object is
getClass. The problem is that getClass is a runtime invocation, not static
information (it depends on an instance), so there is no way to pass the
class of an object to an annotation, because the annotation expects static
class information (so getClass is not valid) and classOf cannot be used
with objects.

Hope that clears it out.

On Wed, Mar 25, 2015 at 5:14 PM, Oliver Ruebenacker <[email protected]>
wrote:

> How is the use of O.type related to the message subject and the issue
> referenced?
>
> On Wed, Mar 25, 2015 at 4:11 PM, Rodrigo Cano <[email protected]> wrote:
>
>> Thats exactly why he is using .type and not getClass, annotations exists
>> during compile time, not runtime (the information can be retained, but it
>> all must exists during compile time, its completely static).
>>
>> On Wed, Mar 25, 2015 at 5:08 PM, Oliver Ruebenacker <[email protected]>
>> wrote:
>>
>>>   Right!
>>>
>>>   Still, O.getClass (a method called at run time) and O.type (a constant
>>> evaluated at compile time) are two different things.
>>>
>>> On Wed, Mar 25, 2015 at 4:02 PM, Rodrigo Cano <[email protected]>
>>> wrote:
>>>
>>>> @Oliver you are writing that statement in the wrong position, you can
>>>> only use <Singleton>.type in a place were a type is expected:
>>>>
>>>> scala> object O
>>>> defined object O
>>>>
>>>> scala> val theO: O.type = O
>>>> theO: O.type = O$@39ed3c8d
>>>>
>>>>
>>>> On Wed, Mar 25, 2015 at 4:47 PM, Oliver Ruebenacker <[email protected]>
>>>> wrote:
>>>>
>>>>>
>>>>>      Hello,
>>>>>
>>>>>   Isn't the problem with O.type, rather than with object.getClass?
>>>>>
>>>>> scala> object O
>>>>> defined object O
>>>>>
>>>>> scala> O.type
>>>>> <console>:1: error: identifier expected but 'type' found.
>>>>>        O.type
>>>>>          ^
>>>>>
>>>>>      Best, Oliver
>>>>>
>>>>> On Wed, Mar 25, 2015 at 3:26 PM, Simon Schäfer <[email protected]>
>>>>> wrote:
>>>>>
>>>>>>  scala> object O
>>>>>> defined object O
>>>>>>
>>>>>> scala> classOf[O.type]
>>>>>> <console>:9: error: class type required but O.type found
>>>>>>               classOf[O.type]
>>>>>>                        ^
>>>>>>
>>>>>> It is broken. Therefore
>>>>>>
>>>>>> scala> :paste
>>>>>> // Entering paste mode (ctrl-D to finish)
>>>>>>
>>>>>> @pickling.directSubclasses(Array(classOf[O1.type]))
>>>>>> sealed abstract class C(val a: Int)
>>>>>> object O1 extends C(1)
>>>>>>
>>>>>> // Exiting paste mode, now interpreting.
>>>>>>
>>>>>> <console>:7: error: class type required but O1.type found
>>>>>>        @pickling.directSubclasses(Array(classOf[O1.type]))
>>>>>>                                                   ^
>>>>>>
>>>>>> is also broken. There is another thing broken:
>>>>>>
>>>>>> scala> :paste
>>>>>> // Entering paste mode (ctrl-D to finish)
>>>>>>
>>>>>> @pickling.directSubclasses(Array(O1T))
>>>>>> sealed abstract class C(val a: Int)
>>>>>> object O1 extends C(1)
>>>>>> final val O1T = O1.getClass
>>>>>>
>>>>>> // Exiting paste mode, now interpreting.
>>>>>>
>>>>>> <console>:7: error: annotation argument needs to be a constant;
>>>>>> found: this.O1T
>>>>>>        @pickling.directSubclasses(Array(O1T))
>>>>>>                                         ^
>>>>>>
>>>>>> One workaround is:
>>>>>>
>>>>>> scala> :paste
>>>>>> // Entering paste mode (ctrl-D to finish)
>>>>>>
>>>>>> @pickling.directSubclasses(Array(classOf[O1]))
>>>>>> sealed abstract class C(val a: Int)
>>>>>> case class O1() extends C(1)
>>>>>>
>>>>>> // Exiting paste mode, now interpreting.
>>>>>>
>>>>>> defined class C
>>>>>> defined class O1
>>>>>>
>>>>>> Not that awesome. Does anyone know another workaround? I don't even
>>>>>> ask if anyone has the time to fix these - I don't know - 10 year old bugs
>>>>>> (one ticket is https://issues.scala-lang.org/browse/SI-2453).
>>>>>>
>>>>>> --
>>>>>> You received this message because you are subscribed to the Google
>>>>>> Groups "scala-language" group.
>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>> send an email to [email protected].
>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Oliver Ruebenacker
>>>>> Solutions Architect at Altisource Labs
>>>>> <http://www.altisourcelabs.com/>
>>>>> Be always grateful, but never satisfied.
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "scala-language" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to [email protected].
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>>
>>>>  --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "scala-language" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to [email protected].
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>>>
>>>
>>> --
>>> Oliver Ruebenacker
>>> Solutions Architect at Altisource Labs <http://www.altisourcelabs.com/>
>>> Be always grateful, but never satisfied.
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "scala-language" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "scala-language" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> --
> Oliver Ruebenacker
> Solutions Architect at Altisource Labs <http://www.altisourcelabs.com/>
> Be always grateful, but never satisfied.
>
> --
> You received this message because you are subscribed to the Google Groups
> "scala-language" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.