- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
scala and reflection
Mon, 2009-03-16, 17:04
hi all,
coming from java-land i am familiar with java reflection.
i now have a scala application that needs to map some json into a
domain model. i already got json pairs into their typed scala
counterparts through parser combinators, and now need to stick them
into my model.
before proceeding with java reflection calls, i thought i might ask if
scala provides a more elegant way of achieving the same thing - i.e.
is package scala.reflect of any help in this usecase (i have found
sparse documentation on this subject). is it possible to do so
avoiding reflection? (i don't think so - but just double checking)
thanks,
francisco
Mon, 2009-03-16, 22:27
#2
Re: scala and reflection
You probably want to use pattern matching rather than reflection for this.
--j
On Mon, Mar 16, 2009 at 9:04 AM, francisco treacy <francisco.treacy@gmail.com> wrote:
--j
On Mon, Mar 16, 2009 at 9:04 AM, francisco treacy <francisco.treacy@gmail.com> wrote:
hi all,
coming from java-land i am familiar with java reflection.
i now have a scala application that needs to map some json into a
domain model. i already got json pairs into their typed scala
counterparts through parser combinators, and now need to stick them
into my model.
before proceeding with java reflection calls, i thought i might ask if
scala provides a more elegant way of achieving the same thing - i.e.
is package scala.reflect of any help in this usecase (i have found
sparse documentation on this subject). is it possible to do so
avoiding reflection? (i don't think so - but just double checking)
thanks,
francisco
Tue, 2009-03-17, 20:27
#3
Re: scala and reflection
naftoli, thanks - yes i was casting everything to AnyRef.
jorge, i'm afraid i don't exactly get what you suggest.
would you go something like:
string match {
case "address" => instance.address_=(value)
...
}
instead of setter.invoke(instance, value) ?
i can't see how to turn a bunch of json values (in a generic fashion)
into its model representation in scala without recurring to
reflection. would you mind developing your thought a bit?
thanks,
francisco
On Mon, Mar 16, 2009 at 10:11 PM, Jorge Ortiz wrote:
> You probably want to use pattern matching rather than reflection for this.
>
> --j
>
> On Mon, Mar 16, 2009 at 9:04 AM, francisco treacy
> wrote:
>>
>> hi all,
>>
>> coming from java-land i am familiar with java reflection.
>>
>> i now have a scala application that needs to map some json into a
>> domain model. i already got json pairs into their typed scala
>> counterparts through parser combinators, and now need to stick them
>> into my model.
>>
>> before proceeding with java reflection calls, i thought i might ask if
>> scala provides a more elegant way of achieving the same thing - i.e.
>> is package scala.reflect of any help in this usecase (i have found
>> sparse documentation on this subject). is it possible to do so
>> avoiding reflection? (i don't think so - but just double checking)
>>
>> thanks,
>>
>> francisco
>
>
Tue, 2009-03-17, 20:57
#4
Re: scala and reflection
Show me what your json model looks like (how is your data being represented) and I'll show you what I mean.
--j
On Tue, Mar 17, 2009 at 12:21 PM, francisco treacy <francisco.treacy@gmail.com> wrote:
--j
On Tue, Mar 17, 2009 at 12:21 PM, francisco treacy <francisco.treacy@gmail.com> wrote:
naftoli, thanks - yes i was casting everything to AnyRef.
jorge, i'm afraid i don't exactly get what you suggest.
would you go something like:
string match {
case "address" => instance.address_=(value)
...
}
instead of setter.invoke(instance, value) ?
i can't see how to turn a bunch of json values (in a generic fashion)
into its model representation in scala without recurring to
reflection. would you mind developing your thought a bit?
thanks,
francisco
On Mon, Mar 16, 2009 at 10:11 PM, Jorge Ortiz <jorge.ortiz@gmail.com> wrote:
> You probably want to use pattern matching rather than reflection for this.
>
> --j
>
> On Mon, Mar 16, 2009 at 9:04 AM, francisco treacy
> <francisco.treacy@gmail.com> wrote:
>>
>> hi all,
>>
>> coming from java-land i am familiar with java reflection.
>>
>> i now have a scala application that needs to map some json into a
>> domain model. i already got json pairs into their typed scala
>> counterparts through parser combinators, and now need to stick them
>> into my model.
>>
>> before proceeding with java reflection calls, i thought i might ask if
>> scala provides a more elegant way of achieving the same thing - i.e.
>> is package scala.reflect of any help in this usecase (i have found
>> sparse documentation on this subject). is it possible to do so
>> avoiding reflection? (i don't think so - but just double checking)
>>
>> thanks,
>>
>> francisco
>
>
Tue, 2009-03-17, 22:17
#5
Re: scala and reflection
i actually don't have a "json model" , the parser is filling in my
entities. i'm pasting an example entity here:
class Comment extends Identifiable {
var id: String = _
var author: String = _
var url: String = _
var message: String = _
}
... along with the JSONParser i use. (warning: scala rookie experiment
ahead) => http://paste.pocoo.org/show/108384/
thanks,
francisco
On Tue, Mar 17, 2009 at 8:54 PM, Jorge Ortiz wrote:
> Show me what your json model looks like (how is your data being represented)
> and I'll show you what I mean.
>
> --j
>
> On Tue, Mar 17, 2009 at 12:21 PM, francisco treacy
> wrote:
>>
>> naftoli, thanks - yes i was casting everything to AnyRef.
>>
>> jorge, i'm afraid i don't exactly get what you suggest.
>>
>> would you go something like:
>>
>> string match {
>> case "address" => instance.address_=(value)
>> ...
>> }
>>
>> instead of setter.invoke(instance, value) ?
>>
>> i can't see how to turn a bunch of json values (in a generic fashion)
>> into its model representation in scala without recurring to
>> reflection. would you mind developing your thought a bit?
>>
>> thanks,
>>
>> francisco
>>
>>
>> On Mon, Mar 16, 2009 at 10:11 PM, Jorge Ortiz
>> wrote:
>> > You probably want to use pattern matching rather than reflection for
>> > this.
>> >
>> > --j
>> >
>> > On Mon, Mar 16, 2009 at 9:04 AM, francisco treacy
>> > wrote:
>> >>
>> >> hi all,
>> >>
>> >> coming from java-land i am familiar with java reflection.
>> >>
>> >> i now have a scala application that needs to map some json into a
>> >> domain model. i already got json pairs into their typed scala
>> >> counterparts through parser combinators, and now need to stick them
>> >> into my model.
>> >>
>> >> before proceeding with java reflection calls, i thought i might ask if
>> >> scala provides a more elegant way of achieving the same thing - i.e.
>> >> is package scala.reflect of any help in this usecase (i have found
>> >> sparse documentation on this subject). is it possible to do so
>> >> avoiding reflection? (i don't think so - but just double checking)
>> >>
>> >> thanks,
>> >>
>> >> francisco
>> >
>> >
>
>
Mon, 2009-03-23, 11:17
#6
Re: scala and reflection
hi jorge,
do you still think you can show me a quick example of what you meant?
thanks,
francisco
On Tue, Mar 17, 2009 at 9:56 PM, francisco treacy
wrote:
> i actually don't have a "json model" , the parser is filling in my
> entities. i'm pasting an example entity here:
>
> class Comment extends Identifiable {
>
> var id: String = _
> var author: String = _
> var url: String = _
> var message: String = _
>
> }
>
> ... along with the JSONParser i use. (warning: scala rookie experiment
> ahead) => http://paste.pocoo.org/show/108384/
>
> thanks,
>
> francisco
>
>
> On Tue, Mar 17, 2009 at 8:54 PM, Jorge Ortiz wrote:
>> Show me what your json model looks like (how is your data being represented)
>> and I'll show you what I mean.
>>
>> --j
>>
>> On Tue, Mar 17, 2009 at 12:21 PM, francisco treacy
>> wrote:
>>>
>>> naftoli, thanks - yes i was casting everything to AnyRef.
>>>
>>> jorge, i'm afraid i don't exactly get what you suggest.
>>>
>>> would you go something like:
>>>
>>> string match {
>>> case "address" => instance.address_=(value)
>>> ...
>>> }
>>>
>>> instead of setter.invoke(instance, value) ?
>>>
>>> i can't see how to turn a bunch of json values (in a generic fashion)
>>> into its model representation in scala without recurring to
>>> reflection. would you mind developing your thought a bit?
>>>
>>> thanks,
>>>
>>> francisco
>>>
>>>
>>> On Mon, Mar 16, 2009 at 10:11 PM, Jorge Ortiz
>>> wrote:
>>> > You probably want to use pattern matching rather than reflection for
>>> > this.
>>> >
>>> > --j
>>> >
>>> > On Mon, Mar 16, 2009 at 9:04 AM, francisco treacy
>>> > wrote:
>>> >>
>>> >> hi all,
>>> >>
>>> >> coming from java-land i am familiar with java reflection.
>>> >>
>>> >> i now have a scala application that needs to map some json into a
>>> >> domain model. i already got json pairs into their typed scala
>>> >> counterparts through parser combinators, and now need to stick them
>>> >> into my model.
>>> >>
>>> >> before proceeding with java reflection calls, i thought i might ask if
>>> >> scala provides a more elegant way of achieving the same thing - i.e.
>>> >> is package scala.reflect of any help in this usecase (i have found
>>> >> sparse documentation on this subject). is it possible to do so
>>> >> avoiding reflection? (i don't think so - but just double checking)
>>> >>
>>> >> thanks,
>>> >>
>>> >> francisco
>>> >
>>> >
>>
>>
>
On Mon, Mar 16, 2009 at 12:04 PM, francisco treacy <francisco.treacy@gmail.com> wrote: