2

At my work we use a typical heavy enterprise stack of Hibernate, Spring, and JSF to handle our application, but after learning Scala I've wanted to try to replicate much of our functionality within a more minimal Scala stack (Squeryl, Scalatra, Scalate) to see if I can decrease code and improve performance (an Achilles heal for us right now).

Often my way of doing things is influenced by our previous stack, so I'm open to advice on a way of doing things that are closer to Scala paradigms. However, I've chosen some of what I do based on previous paradigms we have in the Java code base so that other team members will hopefully be more receptive to the work I'm doing. But here is my question:

We have a domain class like so:

class Person(var firstName: String, var lastName: String)

Within a jade template I make a call like:

.section
  - view(fields)

The backing class has a list of fields like so:

class PersonBean(val person: Person) {
  val fields: Fields = Fields(person,
    List(
      Text(person.firstName),
      Text(person.lastName)
    ))
}

Fields has a base object (person) and a list of Field objects. Its template prints all its fields templates. Text extends Field and its Jade template is supposed to print:

<label for="person:firstName">#{label}</label>: <input type="text" id="person:firstName" value="#{value}" /> 

Now the #{value} is simply a call to person.firstName. However, to find out the label I reference a ResourceBundle and need to produce a string key. I was thinking of using a naming convention like:

person.firstName.field=First Name

So the problem then becomes, how can I within the Text class (or parent Field class) discover what the parameter being passed in is? Is there a way I can pass in person.firstName and find that it is calling firstName on class Person? And finally, am I going about this completely wrong?

3
  • your question is 3 times longer than the average question here and I still have no clue what you want. "So the problem then becomes, how can I within the Text class (or parent Field class) discover what the parameter being passed in is?" That could as well be chinese. What text class are you talking about? Why would you need to "discover" a parameter?
    – Kim Stebel
    May 3, 2011 at 11:16
  • Well its a possibility that he is referring to the Text class he is using in the code posted. Of course that is just an idea of myself. May 3, 2011 at 12:35
  • I apologize, I wanted to give my background motivation so completely different alternatives could be proposed if I were headed in a poor direction. The basic gist is if I have a function that I pass person.firstName to, can I determine the class 'Person' and field 'firstName' from within that function? I think oxbow_lakes has definitively told me that's impossible. May 3, 2011 at 12:42

4 Answers 4

4

If you want to take a walk on the wild side, there's a (hidden) API in Scala that allows you to grab the syntax tree for a thunk of code - at runtime.

This incantation goes something like:

scala.reflect.Code.lift(f).tree

This should contain all the information you need, and then some, but you'll have your work cut out interpreting the output.

You can also read a bit more on the subject here: Can I get AST from live scala code?

Be warned though... It's rightly classified as experimental, do this at your own risk!

3
  • Whatever you want it to be :) May 3, 2011 at 17:49
  • Well, if it was the parameter to the method, what would you get back? Presumably you'd have to get a stack trace to find the call point... and then what? May 4, 2011 at 10:48
  • It's not a stack trace, it's a fragment of the compiler's internal syntax tree. May 4, 2011 at 11:01
1

You can never do this anywhere from within Java, so I'm not wholly clear as to how you are just following the idiom you are used to. The obvious reason that this is not possible is that Java is pass-by-value. So in:

public void foo(String s) { ... }

There is no sense that the parameter s is anything other than what it is. It is not person.firstName just because you called foo like:

foo(person.firstName);

Because person.firstName and s are completely separate references!

3
  • Thank you for your answer, this is the first time I've asked a question but I've found your answers helpful for months! Yes, I am aware that it wasn't possible from Java. The parallel with our current framework is we pass a String and use reflection to call the method on the class. I was hoping for a way that Scala could simply pass the reference so refactoring code could be more automatic. May 3, 2011 at 12:28
  • PlayFramework does it in Scala and Java. Also theres a library, called Paranamer, that does that in Java (and from my tests last year, it worked on Scala also)
    – Felipe
    May 3, 2011 at 14:01
  • @Felipe - could you post, or link to a snippet of code that does exactly what the OP asked for? May 4, 2011 at 10:50
0

What you could do is replacing the fields (e.g. firstname) with actual objects, which have a name attribute.

I did something similiar in a recent blog post:http://blog.schauderhaft.de/2011/05/01/binding-scala-objects-to-swing-components/

The property doesn't have a name property (yet), but it is a full object but is still just as easy to use as a field.

1
  • This looks very interesting, thanks! I'll have to look into this more when I have free time this evening. May 3, 2011 at 12:57
0

I would not be very surprised if the following is complete nonsense:

  • Make the parameter type of type A that gets passed in not A but Context[A]
  • create an implicit that turns any A into a Context[A] and while doing so captures the value of the parameter in a call-by-name parameter
  • then use reflection to inspect the call-by-name parameter that gets passed in

For this to work, you'd need very specific knowledge of how stuff gets turned into call-by-name functions; and how to extract the information you want (if it's present at all).

1
  • I think this idea is awesome, too. I'll have to look into it further. May 3, 2011 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.