quick pimping using Compilets

28 views
Skip to first unread message

Eric Christiansen

unread,
Feb 17, 2013, 1:57:17 PM2/17/13
to nativel...@googlegroups.com
Hi Olivier,

It it possible to write a Compilet that will enable adding a member to an existing type with minimal syntax? For example, now I might write:

implicit class AddFooToInt(self: Int) {
  def foo = ...
}

There's redundancy in the above statement. A better syntax would be something like:

Int addMembers {
  def foo = ... // Here "self" is automatically defined

Olivier Chafik

unread,
Feb 17, 2013, 3:03:46 PM2/17/13
to nativel...@googlegroups.com
Hi Eric,

Compilets (and compiler plugins in general) can't really cheat the typer, since they run after it (well, you *can* run a plugin before the typer, but it's hard to do anything useful without type information...).

Modifying types should be made easy by type macros if they ever come out, although I haven't tested them yet (see branch paradise/macros in github.com/scala/scala)

Cheers

--
You received this message because you are subscribed to the Google Groups "NativeLibs4Java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nativelibs4ja...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Eric Christiansen

unread,
Feb 17, 2013, 4:09:53 PM2/17/13
to nativel...@googlegroups.com
So... disappointing... 

I'm a bit confused, though. My reading of this extremely old doc is that a plugin should allow you to define your own syntax. It seems you can specify at what phase(s) in compilation you want your plugin to be invoked, so why not invoke it after the parser but before the typer?

I've noticed this weird situation in the Scala world where most practitioners prefer functional styles (ie pimping and typeclasses), but the language itself doesn't have easy syntax for these things. I would love for typeclasses to be as easy to use in Scala as they are in Haskell. I hoped a compiler plugin could make this possible.

Olivier Chafik

unread,
Feb 18, 2013, 11:37:00 AM2/18/13
to nativel...@googlegroups.com
Well, I didn't say it was impossible, it's just hard to do it in a solid way :-D

Here's a quick example of a compiler plugin that runs between parser and typer:


(implements a syntax `@on(Int) def foo = self.toString` to define a new method on Int)

What's hard here is to come up with something hygienic (making sure @on and Int are not locally redefined, etc...).

Eric Christiansen

unread,
Feb 18, 2013, 1:47:12 PM2/18/13
to nativel...@googlegroups.com
That is very cool. I'll be trying this myself soon.

Olivier Chafik

unread,
Feb 19, 2013, 7:13:51 AM2/19/13
to nativel...@googlegroups.com
I've updated the sample with comments (including tips on how to reproduce), improvements (use value classes when appropriate) and proper plugin support:

2013/2/18 Eric Christiansen <ericmartinc...@gmail.com>

Eric Christiansen

unread,
Mar 14, 2013, 1:53:50 AM3/14/13
to nativel...@googlegroups.com
Any idea how to make that plugin more general? See this question. Maybe you did it already using your macro stuff?
Reply all
Reply to author
Forward
0 new messages