Re: Static Reflection in Scala
Oliver Ruebenacker <[email protected]>
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <CAA=X4OB5w34GcCT76DHTckWredp778x=+MFmOFRsC+Sd7G9ETg@mail.gmail.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 <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 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.