Re: Static Reflection in Scala
Aravindh S <[email protected]>
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <CADA+0AMBRyqn-UGZn0fYSAv6ZMWhoh3tY-S8Lugkags1_THg8A@mail.gmail.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 <https://code.google.com/p/nbuilder/>. On Sun, Feb 8, 2015 at 6:43 PM, Oliver Ruebenacker <[email protected]> wrote: > > 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 <http://scalacheck.org/>, for example, supports > writing and using such generators. > > Best, Oliver > > On Sun, Feb 8, 2015 at 12:07 AM, Aravindh S <[email protected]> wrote: > >> 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 >> >> -- >> 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 [email protected]. >> For more options, visit https://groups.google.com/d/optout. >> > > > > -- > Oliver Ruebenacker > Solutions Architect at Altisource Labs <http://www.altisourcelabs.com/> > Be always grateful, but never satisfied. > > -- > 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 > [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- Nandri (Thanks) @r@v!ndh.$ -- 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 [email protected]. For more options, visit https://groups.google.com/d/optout.