- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
get accessor function name
Fri, 2009-02-13, 20:39
hi,
do you know if there is a way to obtain the name (as a string) of a
given function?
let me explain. i'm interested in an api that allows passing a
property expression (dotted path - like "person.address.city") for a
field, so i can call its accessor and mutator.
def doSomethingWith(propertyExpression: String) = {
// do some magic with reflection, knowing that to access field 'city'
its setter is 'city_=' and its getter 'city'
}
but in scala, to enforce type safety, i could redefine this using a
by-name parameter to:
def doSomethingWith[T](propertyExpression: => T) = {
// something
propertyExpression
}
the latter would be used like doSomethingWith(person.address.city) and
would return for example "buenos aires". as my function would also be
able to *set* values to that field, it needs to know about the setter
(mutator).
in order to avoid having to pass-in the setter [ e.g.
doSomethingWith(person.address.city, person.address.city_=) ], i would
like to "guess" the name of the setter (given the name of the getter)
and invoke it via reflection.
my question is: how do i get the name of the getter? i had a look at
trait Function0 but nothing really interesting there.
thank you!
francisco
Wed, 2009-02-18, 21:27
#2
Re: get accessor function name
thanks, jon.
for those who are interested, as an exercise i actually started to
create an implementation of wicket's imodel with scala features (a
sort of type-safe version of propertymodel). but for it to make sense
i needed to know the "path", which in scala coincides with the getter.
is there a place to keep track of this eventual language feature?
thanks,
francisco
On Sat, Feb 14, 2009 at 10:06 PM, Jon Pretty wrote:
> Hi Francisco,
>
> There was some talk about modifying the language to provide a convenient
> way of doing this about a year ago, but AFAIR, nothing came of it.
>
> You've got a few options, none of them ideal:
>
> 1. Define a Field type, something like:
>
> class Field[T](var get : T) {
> def set(v : T) : Unit = get = v
> }
>
> and pass around your field object for these purposes. You could also
> define an implicit method:
>
> def fieldValue[T](f : Field[T]) : T = f.get
>
> to avoid having to call .get on all your fields, though note that this
> isn't entirely transparent. (Notably, fieldA == fieldB is not the same
> as fieldA.get == fieldB.get).
>
>
> 2. Use Java reflection.
>
> I'll let someone else fill in the details here if they feel inclined to,
> but you might get some unbelievably dodgy reflection code to do just
> what you want it to under certain conditions. But it's so horrible, I
> really shouldn't even be suggesting it...
>
>
> (3. Use a language feature which doesn't exist yet.)
>
> I think there's an opportunity to improve the Scala language with
> support for inferring setters. Maybe one day in the near future I'll
> write a SIP on this...
>
> Cheers,
> Jon
>
> --
> Jon Pretty | Sygneca Ltd.
>
>
Thu, 2009-02-19, 12:47
#3
Re: get accessor function name
Hi Francisco,
> for those who are interested, as an exercise i actually started to
> create an implementation of wicket's imodel with scala features (a
> sort of type-safe version of propertymodel). but for it to make sense
> i needed to know the "path", which in scala coincides with the getter.
have you taken a look at the slides of the Scala-Wicket presentation I gave in
London 2 weeks ago? There's a 'VarModel' which should suffice as a type-safe
replacement for Wicket's PropertyModel.
Cheers, --- Jan.
Thu, 2009-02-19, 13:17
#4
Re: get accessor function name
hi jan,
(hope this is not too off-topic for the scala mailing list)
yes, i came across the slides and VarModel is roughly what i have
achieved so far. i see you can either instantiate it as a read-only
model, or read/write in which case you need to pass-in the setter.
however, i had defined getObject like so:
def getObject: T = {
try {
getter
} catch {
case npe: NullPointerException => null.asInstanceOf[T]
}
}
to take care of nulls like propertymodel does.
but i wanted to go further, and actually make it a kind of
compoundpropertymodel, where you just provide the getter (or "dotted
path") and it figured out the rest (setter + wicket id == dotted
path). not easy task as you can't right now grab the getter expression
in scala. just experimenting, anyway =)
cheers, and nice slides btw.
francisco
On Thu, Feb 19, 2009 at 12:38 PM, Jan Kriesten
wrote:
>
> Hi Francisco,
>
>> for those who are interested, as an exercise i actually started to
>> create an implementation of wicket's imodel with scala features (a
>> sort of type-safe version of propertymodel). but for it to make sense
>> i needed to know the "path", which in scala coincides with the getter.
>
> have you taken a look at the slides of the Scala-Wicket presentation I gave in
> London 2 weeks ago? There's a 'VarModel' which should suffice as a type-safe
> replacement for Wicket's PropertyModel.
>
> Cheers, --- Jan.
>
>
>
Thu, 2009-02-19, 15:27
#5
Re: get accessor function name
I'd be very interested in those slides, where can I find them?
Cheers,
-Tako
On Thu, Feb 19, 2009 at 12:38, Jan Kriesten <kriesten@mail.footprint.de> wrote:
Cheers,
-Tako
On Thu, Feb 19, 2009 at 12:38, Jan Kriesten <kriesten@mail.footprint.de> wrote:
Hi Francisco,
> for those who are interested, as an exercise i actually started to
> create an implementation of wicket's imodel with scala features (a
> sort of type-safe version of propertymodel). but for it to make sense
> i needed to know the "path", which in scala coincides with the getter.
have you taken a look at the slides of the Scala-Wicket presentation I gave in
London 2 weeks ago? There's a 'VarModel' which should suffice as a type-safe
replacement for Wicket's PropertyModel.
Cheers, --- Jan.
Thu, 2009-02-19, 15:57
#6
Re: get accessor function name
Hi Tako,
> I'd be very interested in those slides, where can I find them?
there you go: http://www.footprint.de/fcc/2009/02/london-wicket-presentation/
Best regards, --- Jan.
Hi Francisco,
There was some talk about modifying the language to provide a convenient
way of doing this about a year ago, but AFAIR, nothing came of it.
You've got a few options, none of them ideal:
1. Define a Field type, something like:
class Field[T](var get : T) {
def set(v : T) : Unit = get = v
}
and pass around your field object for these purposes. You could also
define an implicit method:
def fieldValue[T](f : Field[T]) : T = f.get
to avoid having to call .get on all your fields, though note that this
isn't entirely transparent. (Notably, fieldA == fieldB is not the same
as fieldA.get == fieldB.get).
2. Use Java reflection.
I'll let someone else fill in the details here if they feel inclined to,
but you might get some unbelievably dodgy reflection code to do just
what you want it to under certain conditions. But it's so horrible, I
really shouldn't even be suggesting it...
(3. Use a language feature which doesn't exist yet.)
I think there's an opportunity to improve the Scala language with
support for inferring setters. Maybe one day in the near future I'll
write a SIP on this...
Cheers,
Jon