mardi 27 mars 2018

Scala: reflection against named arguments

I'm trying to pass named arguments to a function from a regular Scala object like string/list/map, where the name of the argument and it's value are both variable (in my case from parsed user input). Is there a way to do this in Scala? I'm in principal looking for a short program in scala, similar to this in python:

def surprise(animal, color):
    print('Oh, a ' + color + ' ' + animal + '!')

arguments = {'animal': 'cat', 'color': 'black'}
surprise(**arguments)

Since python can unpack dictionaries into named arguments, this results in

Oh, a black cat!

I've been searching for this functionality in scala, but I could not find it. Can anyone give me an example on how to accomplish this in scala?

Thanks in advance!





Aucun commentaire:

Enregistrer un commentaire