1

I've dynamically defined a Scala class, but in order to use it "properly" it needs to have a ScalaSig.

So, how might I generate a ScalaSig outside of normal compilation? Perhaps from a tree? Maybe like:

val tb = runtimeMirror(getClass.getClassLoader).mkToolBox()
val classDef = """class MyRecord(x: String)"""
val tree = showRaw(tb.parse(classDef))

But where does the pickler come in?

Thanks for any advice -Julian

Julian Peeters
  • 853
  • 1
  • 6
  • 19
  • Have you considered using a toolbox to define a class? Then it would automatically generate the necessary signatures. – Eugene Burmako Jul 12 '13 at 13:40
  • Hi Eugene, yes, but the class definition happens at runtime. Am I correct in thinking that toolboxes will only work at compile time? Can I tell you how excited I am for type macros!? – Julian Peeters Jul 12 '13 at 16:38
  • 1
    Toolboxes work at runtime. This pull request lets them define publicly visible classes: https://github.com/scala/scala/pull/2662, but it's for 2.11. In the meanwhile, in 2.10 you can use https://gist.github.com/xeno-by/5845539. Please let me know if something doesn't work. – Eugene Burmako Jul 12 '13 at 19:48
  • Type macros are cool, but unfortunately they won't be in 2.11: http://scalamacros.org/paperstalks/2013-06-12-HalfYearInMacroParadise.pdf. They might materialize in form of macro annotations somewhen afterwards (maybe in 2.12?). – Eugene Burmako Jul 12 '13 at 19:54
  • 2
    Nevertheless there's a very cool emulation of type macros already available in 2.10.x: http://meta.plasm.us/posts/2013/07/11/fake-type-providers-part-2/. Take a look, maybe it'll be useful. – Eugene Burmako Jul 12 '13 at 19:55
  • These approaches are extremely interesting to me, thanks so much for sharing. RE toolboxes, I made an sbt project from your gist: https://gist.github.com/julianpeeters/6005627. I can get classes and objects as expected, but haven't been able to find a way to get its type to use as a type parameter, nor can I seem to get a scalaSig from the class (see the bottom of my gist). – Julian Peeters Jul 16 '13 at 04:01
  • RE fake-type-providers, thanks for providing a macro example (https://github.com/scalamacros/sbt-example), which I as a basis to make an sbt project (https://github.com/julianpeeters/fake-type-providers-example/tree/fake-type-providers) out of Travis Brown's post that you mention above. Although the scalaSig is `null` with this approach as well, I *can* get a type and its members by using the instance to define a type alias (a technique that does not work for normally defined classes, but worked also for my ASM-based approach that I asked about before: http://bit.ly/1dBhZRs). – Julian Peeters Jul 16 '13 at 04:24
  • RE fake-type-providers cont'd, so with a type, maybe I don't need a scalaSig in the long run, but for now, it's still the first hurdle. The second is that it appears to my novice eyes that in the example, the classes are all `java.net.URI`s, whereas, say, with a JSON schema, the classes will be unknown and will need to be defined at runtime. So far I can't figure out how adapt the code for a JSON schema. – Julian Peeters Jul 16 '13 at 04:42

1 Answers1

0

Artisanal-Pickle-Maker will reproduce a Scala pickled signature byte-for-byte (see restrictions).

Tapping into the compiler's pickler phase, as well as reuse of the Pickler's code, proved too challenging, so instead I used PickleBuffer, ShowPickled and a whole lotta diff -y to figure out how to generate arbitrary pickled Scala sigs.

Julian Peeters
  • 853
  • 1
  • 6
  • 19