Static Reflection in Scala

133 views
Skip to first unread message

Aravindh S

unread,
Feb 8, 2015, 12:07:50 AM2/8/15
to scala-l...@googlegroups.com
Hi  All, I have been trying to achieve the following in scala for quite sometime now, but was not able to do so.  I am looking for a way to pass the name of a val inside a case class along with its type to another method. Below is an example:

case class Person(name:String, age:Int)

object Sample{
     def apply[T]={}
     def set(???) = ???
}


I would like the set method to be used like
Sample[Person].set(p => p.name = "random name") or simply Sample[Person].set(_.name="random name")

The above ideally wont work because I am trying to re assign to a val. But Would like to have something like this. I need to capture the name of the val, type of the val inside the set method and would want the compiler to do a type check when the above method is invoked. 

In C#, We have the expression API which helps in static compile time reflection which will allow you to define methods like above. How do I accomplish the above in scala?


Thanks
Aravindh.S

Oliver Ruebenacker

unread,
Feb 8, 2015, 8:13:40 AM2/8/15
to scala-l...@googlegroups.com

     Hello,

  I don't know how to do this, but would assume you need macros.

  I wonder what your use case is, though? If you want to initialize objects of arbitrary classes with random values, the problem will be that not all values may be valid for a particular class. Maybe it has an Int field that must be between 1 and 10. Or a String that must have length 7. Or maybe three Int fields that must add up to 15.

  You may end up writing individual value generators for your classes anyway. ScalaCheck, for example, supports writing and using such generators.

     Best, Oliver

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



--
Oliver Ruebenacker
Solutions Architect at Altisource Labs
Be always grateful, but never satisfied.

Aravindh S

unread,
Feb 8, 2015, 8:18:59 AM2/8/15
to scala-l...@googlegroups.com
Hi Oliver,
  You are precisely right. I am trying to generate random values for vals / vars in case classes so that I can use them as objects for testing. Having conditions that the length should be from 10 to 15 etc are next level of customization. First level, I am trying to generate just random values. I did see scala check first. But I would like to have a syntax as I said above because it feels much nicer. This is somewhat similar to a C# library named  NBuilder.

--
You received this message because you are subscribed to a topic in the Google Groups "scala-language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scala-language/mrrLfXcU31E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to scala-languag...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Nandri (Thanks)
@r@v!ndh.$

rssh

unread,
Feb 13, 2015, 7:22:48 PM2/13/15
to scala-l...@googlegroups.com
 


Also you can look at code which do very simular task:

 (retrieve all vals)
near line 202 you will see  retrieving tree where variable with the same type as defined in given class, is defined.
Reply all
Reply to author
Forward
0 new messages