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

@varargs annotation on object method

9 replies
Renato Cavalcanti
Joined: 2011-10-27,
User offline. Last seen 42 years 45 weeks ago.
Hi,
I'm writing a small lib in Scala that will be used from Java code. One of my methods has varargs and I want to make it Java "friendly" by annotating it with @varargs.
However, it seems only to work for methods in classes, not in objects. Is there a reason for that? Am I missing something?  
See code below:
// scala class C
class C {    @scala.annotation.varargs    def f(values:String*) = println(values) }
// scala object  Oobject O {    @scala.annotation.varargs    def f(values:String*) = println(values) }
// calling code from Java A classpublic class A {    public static void main(String[] args) {        new C().f("a", "b", "c");        O.f("a", "b", "c");    }}

Line calling C compiles, but call to O gives:
../p/A.java:5: cannot find symbol[error] symbol  : method f(java.lang.String,java.lang.String,java.lang.String)[error] location: class p.O[error]         O.f("a", "b", "c");[error]          ^[error] 1 error

I was plaining to fill a bug ticket for that, but I would like to first check with you guys if this is really the expected behavior. 
edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.
Re: @varargs annotation on object method
Dear Renato,
I had similar problems and my understanding is that when you call from java a Scala object method, not all the features of scala are available.

The workaround I found is the following:

// scala object  O object O {
    def instance=this
    @scala.annotation.varargs    def f(values:String*) = println(values) }
Now accessing O.instance().f() should work

Let me know

Best Regards
Edmondo

2011/10/27 Renato Cavalcanti <renato [at] xavena [dot] eu>
Hi,
I'm writing a small lib in Scala that will be used from Java code. One of my methods has varargs and I want to make it Java "friendly" by annotating it with @varargs.
However, it seems only to work for methods in classes, not in objects. Is there a reason for that? Am I missing something?  
See code below:
// scala class C
class C {    @scala.annotation.varargs    def f(values:String*) = println(values) }
// scala object  Oobject O {    @scala.annotation.varargs     def f(values:String*) = println(values) }
// calling code from Java A classpublic class A {    public static void main(String[] args) {         new C().f("a", "b", "c");        O.f("a", "b", "c");    }}

Line calling C compiles, but call to O gives:
../p/A.java:5: cannot find symbol[error] symbol  : method f(java.lang.String,java.lang.String,java.lang.String)[error] location: class p.O[error]         O.f("a", "b", "c"); [error]          ^[error] 1 error

I was plaining to fill a bug ticket for that, but I would like to first check with you guys if this is really the expected behavior. 

Renato Cavalcanti
Joined: 2011-10-27,
User offline. Last seen 42 years 45 weeks ago.
Re: @varargs annotation on object method

Hi Edmondo,

Thanks for the tip. And yes, it works. However, it works for object O of my example, but not for the code from my library.

It seems that it breaks when the object has other methods returning functions.

In any case, it looks like a bug to me.
I don't see a reason why the it should work for classes and not for objects. Neither why it stops working for objects (using your workaround) in some special situations.

Thanks anyway,

Regards,

Renato

On 27 Oct 2011, at 09:38, Edmondo Porcu wrote:

> Dear Renato,
> I had similar problems and my understanding is that when you call from java a Scala object method, not all the features of scala are available.
>
> The workaround I found is the following:
>
> // scala object O
> object O {
> def instance=this
> @scala.annotation.varargs
> def f(values:String*) = println(values)
> }
>
> Now accessing O.instance().f() should work
>
> Let me know
>
> Best Regards
> Edmondo

Renato Cavalcanti
Joined: 2011-10-27,
User offline. Last seen 42 years 45 weeks ago.
Re: @varargs annotation on object method

Hi Edmondo,

Thanks for the tip. And yes, it works. However, it works for object O from my example, but not for the code from my library.

It seems that it breaks when the object has other methods returning functions.

In any case, it looks like a bug to me.
I don't see a reason why the it should work for classes and not for objects. Neither why it stops working for objects (using your workaround) if you add a method returning a function.

Thanks anyway,

Regards,

Renato

On 27 Oct 2011, at 09:38, Edmondo Porcu wrote:

> Dear Renato,
> I had similar problems and my understanding is that when you call from java a Scala object method, not all the features of scala are available.
>
> The workaround I found is the following:
>
> // scala object O
> object O {
> def instance=this
> @scala.annotation.varargs
> def f(values:String*) = println(values)
> }
>
> Now accessing O.instance().f() should work
>
> Let me know
>
> Best Regards
> Edmondo

Razvan Cojocaru 3
Joined: 2010-07-28,
User offline. Last seen 42 years 45 weeks ago.
Re: @varargs annotation on object method
I think O.MODULE$.f() should work as well - that's the name for the instance...?

Thanks,Razvan
On 2011-10-27, at 3:38 AM, Edmondo Porcu <edmondo [dot] porcu [at] gmail [dot] com> wrote:

Dear Renato,
I had similar problems and my understanding is that when you call from java a Scala object method, not all the features of scala are available.

The workaround I found is the following:

// scala object  O object O {
    def instance=this
    @scala.annotation.varargs    def f(values:String*) = println(values) }
Now accessing O.instance().f() should work

Let me know

Best Regards
Edmondo

2011/10/27 Renato Cavalcanti <renato [at] xavena [dot] eu>
Hi,
I'm writing a small lib in Scala that will be used from Java code. One of my methods has varargs and I want to make it Java "friendly" by annotating it with @varargs.
However, it seems only to work for methods in classes, not in objects. Is there a reason for that? Am I missing something?  
See code below:
// scala class C
class C {    @scala.annotation.varargs    def f(values:String*) = println(values) }
// scala object  Oobject O {    @scala.annotation.varargs     def f(values:String*) = println(values) }
// calling code from Java A classpublic class A {    public static void main(String[] args) {         new C().f("a", "b", "c");        O.f("a", "b", "c");    }}

Line calling C compiles, but call to O gives:
../p/A.java:5: cannot find symbol[error] symbol  : method f(java.lang.String,java.lang.String,java.lang.String)[error] location: class p.O[error]         O.f("a", "b", "c"); [error]          ^[error] 1 error

I was plaining to fill a bug ticket for that, but I would like to first check with you guys if this is really the expected behavior. 

Mirco Dotta
Joined: 2009-02-25,
User offline. Last seen 42 years 45 weeks ago.
Re: @varargs annotation on object method
I think O.MODULE$.f() should work as well - that's the name for the instance...?

I believe that `MODULE$` should "never" be used, as static forwarders to the module classare injected by the compiler (i.e., using `O.f()` should - in theory - work and it should be semantically equivalent to `O.MODULE$.f()`).
I'd tend to agree that it is a bug (or a known limitation).
-- Mirco

Thanks,Razvan
On 2011-10-27, at 3:38 AM, Edmondo Porcu <edmondo [dot] porcu [at] gmail [dot] com> wrote:

Dear Renato,
I had similar problems and my understanding is that when you call from java a Scala object method, not all the features of scala are available.

The workaround I found is the following:

// scala object  O object O {
    def instance=this
    @scala.annotation.varargs    def f(values:String*) = println(values) }
Now accessing O.instance().f() should work

Let me know

Best Regards
Edmondo

2011/10/27 Renato Cavalcanti <renato [at] xavena [dot] eu>
Hi,
I'm writing a small lib in Scala that will be used from Java code. One of my methods has varargs and I want to make it Java "friendly" by annotating it with @varargs.
However, it seems only to work for methods in classes, not in objects. Is there a reason for that? Am I missing something?  
See code below:
// scala class C
class C {    @scala.annotation.varargs    def f(values:String*) = println(values) }
// scala object  Oobject O {    @scala.annotation.varargs     def f(values:String*) = println(values) }
// calling code from Java A classpublic class A {    public static void main(String[] args) {         new C().f("a", "b", "c");        O.f("a", "b", "c");    }}

Line calling C compiles, but call to O gives:
../p/A.java:5: cannot find symbol[error] symbol  : method f(java.lang.String,java.lang.String,java.lang.String)[error] location: class p.O[error]         O.f("a", "b", "c"); [error]          ^[error] 1 error

I was plaining to fill a bug ticket for that, but I would like to first check with you guys if this is really the expected behavior. 


Renato Cavalcanti
Joined: 2011-10-27,
User offline. Last seen 42 years 45 weeks ago.
Re: @varargs annotation on object method

I have done some tests with different scenarios and I have the impression it is a bug.
Depending on what you have in your object you may get a varargs method for java interop or not. Basically, the varargs annotation is not be picked in all the cases.

Using MODULE$ may work, but that's exactly the kind of thing I want to avoid. The library is intent to be used from java code.

Thanks,

Renato

Razvan Cojocaru 3
Joined: 2010-07-28,
User offline. Last seen 42 years 45 weeks ago.
RE: @varargs annotation on object method

Are you saying that doing it like

 

def instance = this

 

Is much safer since it goes through whatever lazy initialization is there?

 

That’s fair. Why doesn’t the compiler inject that then – it obviously must have something equivalent…

 

From: Mirco Dotta [mailto:mirco [dot] dotta [at] gmail [dot] com]
Sent: October-27-11 9:33 AM
To: Razvan Cojocaru
Cc: Edmondo Porcu; scala-user [at] googlegroups [dot] com
Subject: Re: [scala-user] @varargs annotation on object method

 

I think O.MODULE$.f() should work as well - that's the name for the instance...?

 

I believe that `MODULE$` should "never" be used, as static forwarders to the module class

are injected by the compiler (i.e., using `O.f()` should - in theory - work and it should be 

semantically equivalent to `O.MODULE$.f()`).

 

I'd tend to agree that it is a bug (or a known limitation).

 

-- Mirco




Thanks,

Razvan


On 2011-10-27, at 3:38 AM, Edmondo Porcu <edmondo [dot] porcu [at] gmail [dot] com> wrote:

Dear Renato,
I had similar problems and my understanding is that when you call from java a Scala object method, not all the features of scala are available.

The workaround I found is the following:

// scala object  O

object O {
    def instance=this

    @scala.annotation.varargs

    def f(values:String*) = println(values) 

}


Now accessing O.instance().f() should work

Let me know

Best Regards
Edmondo

2011/10/27 Renato Cavalcanti <renato [at] xavena [dot] eu>

Hi,

 

I'm writing a small lib in Scala that will be used from Java code. One of my methods has varargs and I want to make it Java "friendly" by annotating it with @varargs.

 

However, it seems only to work for methods in classes, not in objects. Is there a reason for that? Am I missing something?  

 

See code below:

 

// scala class C

class C {

    @scala.annotation.varargs

    def f(values:String*) = println(values) 

}

 

// scala object  O

object O {

    @scala.annotation.varargs

    def f(values:String*) = println(values) 

}

 

// calling code from Java A class

public class A {

    public static void main(String[] args) {

        new C().f("a", "b", "c");

        O.f("a", "b", "c");

    }

}

 

 

Line calling C compiles, but call to O gives:

 

../p/A.java:5: cannot find symbol

[error] symbol  : method f(java.lang.String,java.lang.String,java.lang.String)

[error] location: class p.O

[error]         O.f("a", "b", "c");

[error]          ^

[error] 1 error

 

 

I was plaining to fill a bug ticket for that, but I would like to first check with you guys if this is really the expected behavior. 

 

 

 

Mirco Dotta
Joined: 2009-02-25,
User offline. Last seen 42 years 45 weeks ago.
Re: @varargs annotation on object method

Hi Renato,

Paul Philips did his magic and already fixed the issue in trunk.

Check out this commit

https://lampsvn.epfl.ch/trac/scala/changeset/25900/

The workaround at the moment is to use O.MODULE$.f(), as the
@varargs annotation was not propagated correctly in the forwarder
methods injected by the compiler.

Cheers,
Mirco

On Oct 27, 2011, at 9:23 AM, Renato Cavalcanti wrote:

> Hi,
>
> I'm writing a small lib in Scala that will be used from Java code. One of my methods has varargs and I want to make it Java "friendly" by annotating it with @varargs.
>
> However, it seems only to work for methods in classes, not in objects. Is there a reason for that? Am I missing something?
>
> See code below:
>
> // scala class C
> class C {
> @scala.annotation.varargs
> def f(values:String*) = println(values)
> }
>
> // scala object O
> object O {
> @scala.annotation.varargs
> def f(values:String*) = println(values)
> }
>
> // calling code from Java A class
> public class A {
> public static void main(String[] args) {
> new C().f("a", "b", "c");
> O.f("a", "b", "c");
> }
> }
>
>
> Line calling C compiles, but call to O gives:
>
> ../p/A.java:5: cannot find symbol
> [error] symbol : method f(java.lang.String,java.lang.String,java.lang.String)
> [error] location: class p.O
> [error] O.f("a", "b", "c");
> [error] ^
> [error] 1 error
>
>
> I was plaining to fill a bug ticket for that, but I would like to first check with you guys if this is really the expected behavior.
>

Renato Cavalcanti
Joined: 2011-10-27,
User offline. Last seen 42 years 45 weeks ago.
Re: @varargs annotation on object method
Thanks very much for the assistance and fast reaction.
Unfortunately, I've found other small bugs related with @varargs annotation.https://issues.scala-lang.org/browse/SI-5125
Regards,
Renato
On 28 Oct 2011, at 09:30, Mirco Dotta wrote:
Hi Renato,

Paul Philips did his magic and already fixed the issue in trunk.

Check out this commit

https://lampsvn.epfl.ch/trac/scala/changeset/25900/

The workaround at the moment is to use O.MODULE$.f(), as the
@varargs annotation was not propagated correctly in the forwarder
methods injected by the compiler.

Cheers,
 Mirco

On Oct 27, 2011, at 9:23 AM, Renato Cavalcanti wrote:

Hi,

I'm writing a small lib in Scala that will be used from Java code. One of my methods has varargs and I want to make it Java "friendly" by annotating it with @varargs.

However, it seems only to work for methods in classes, not in objects. Is there a reason for that? Am I missing something?  

See code below:

// scala class C
class C {
   @scala.annotation.varargs
   def f(values:String*) = println(values)
}

// scala object  O
object O {
   @scala.annotation.varargs
   def f(values:String*) = println(values)
}

// calling code from Java A class
public class A {
   public static void main(String[] args) {
       new C().f("a", "b", "c");
       O.f("a", "b", "c");
   }
}


Line calling C compiles, but call to O gives:

../p/A.java:5: cannot find symbol
[error] symbol  : method f(java.lang.String,java.lang.String,java.lang.String)
[error] location: class p.O
[error]         O.f("a", "b", "c");
[error]          ^
[error] 1 error


I was plaining to fill a bug ticket for that, but I would like to first check with you guys if this is really the expected behavior.



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