java companion objects

69 views
Skip to first unread message

Paul Phillips

unread,
Apr 30, 2012, 3:17:10 PM4/30/12
to scala-i...@googlegroups.com
I was hoping this would work already, but I guess that was asking a lot.

package java.lang;

public class String$ {
public static String$ MODULE$ = new String$();
public String Bippy = "abc";
}

All the bippies are in the right place, but scala wants to be all cool
about it and pretend String's statics are all there are.

scala> java.lang.String$.MODULE$.Bippy
res1: String = abc

scala> java.lang.String.Bippy
<console>:8: error: value Bippy is not a member of object String
java.lang.String.Bippy
^

We could make it work if we were so inclined, I think.

My immediate motivation is that there's no "companion object scope"
for String, so its implicits have to be in Predef to work. I think
it'd be nicer if we could position implicits with the relevant type,
even if the relevant type is a java class.

Josh Suereth

unread,
Apr 30, 2012, 4:40:29 PM4/30/12
to scala-i...@googlegroups.com

I've wanted a @sneakyCompanion annotation for a while to do this...

Stefan Zeiger

unread,
May 1, 2012, 4:13:39 AM5/1/12
to scala-i...@googlegroups.com
On 2012-04-30 21:17, Paul Phillips wrote:
> I was hoping this would work already, but I guess that was asking a lot.
>
> package java.lang;
>
> public class String$ {
> public static String$ MODULE$ = new String$();
> public String Bippy = "abc";
> }

If the use case for this is pimping standard Java objects with new
methods, you'll run into another problem. You can't access a class in a
java.* package through a normal classloader. Only system extensions in
java.ext.dirs are allowed to use those packages.

-sz
Reply all
Reply to author
Forward
0 new messages