[1.2.3] What's the best way to have a function return multiple values in Play?

61 views
Skip to first unread message

Arlo Duff

unread,
Jun 25, 2012, 9:54:42 AM6/25/12
to play-fr...@googlegroups.com
It sometimes happens in programming that you need a function which returns more than one value. In other languages, like C++, this is best done by passing arguments by reference and letting the function modify them. But in Java, there's no way to pass by reference at the language level.

One common suggestion is to create a new class that contains all the values you need to return, and return an instance of that class. I've never liked doing things that way, because you have to write a new class for each type of result set you need to return, but on top of it, for frameworks like Play, an error will be generated if you don't give the object its own file in the models directory and make it its own model. So in this case, I think that's a really inefficient solution, given that this is a problem that isn't uncommon in programming.

So I'm just wondering if there's some standard way of doing this that I'm missing.

christian sarnataro

unread,
Jun 25, 2012, 10:29:52 AM6/25/12
to play-fr...@googlegroups.com
Hi, 
of course you can return, a Map, an Object array... or you could also use a Map for "reference-like" parameters (if you modify a value in an Map, at the end of your method, the modification is still on place). Personally I don't like this kind of "side-effect" programming, since it's often error leading.

Or, try to see this project:

Anyway I don't understand your sentence: "an error will be generated if you don't give the object its own file in the models directory and make it its own model."

It seems you're mixing a Java question (i.e. returning multiple values from a function) with play Models... I don't get the connection between these two issues.

Arlo Duff

unread,
Jun 25, 2012, 12:19:59 PM6/25/12
to play-fr...@googlegroups.com
Hi, thanks for the response.

Well, basically what I mean is in the past, when I've tried to create a custom class to hold return values - say it holds a string, an int, and one of the models from my app - I get an error because I'm trying to declare a class outside of its own file. It's been a while since I've done this, so the syntax may have changed by now, or maybe I had to play around with it because I kept getting random errors. I don't remember enough offhand. But I know in the end, I was forced to make it its own model in the models directory, which I think is bad coding, since it's really not supposed to be its own model. Trying to do that right now in one of my controllers gives the error Execution error occured in template {module:crud}/app/views/tags/crud/types.tag. Exception raised was NullPointerException : null.

Maybe I'm just coding it wrong, and it is possible to create a custom class for a return value inside one of the controller files? I mean I guess that's bad coding too, but I think that's better than making the class its own model, because it's at least more closely related to the controller that has a function that needs to return the class, whereas it's not supposed to be a model at all.

If there's something I'm missing, feel free to let me know.

christian sarnataro

unread,
Jun 26, 2012, 10:08:20 AM6/26/12
to play-fr...@googlegroups.com
Hi, with "I'm trying to declare a class outside of its own file" do you mean you defined an inner class?
e.g something like this?

public class Outer {
    String aString;
    int anInt;
    MyModel myModel;

    public class MyModel extends Model {
        public Long id;
        public String field_0;
        public String field_1;

Tom Carchrae

unread,
Jun 26, 2012, 10:55:56 AM6/26/12
to play-fr...@googlegroups.com
Keep in mind that unless you want to access the outer class (called
Outer) below, you should probably use the static class modifier on the
inner class (MyModel). In general, if you want a compound result you
want to use the static class modifier. You only exclude the static
modifer if your inner class has a reference to the outer class (eg,
say MyModel wanted to access aString) which is something I'd recommend
against since it is a recipe for spaghetti code.

So, to be clear:

static public class MyModel extends Model {

This example code below makes me wonder though - I'm not sure I would
ever stick a model class as an inner class (also it is missing
@Entity) - I might go the other way around though and have something
like

static public class Result { public Long value; public String value2; }

Also, be careful with methods in classes that extend Controller - they
are heavily bytecode modified and don't behave as normal java calls.
For example, calling another static method in a controller will cause
a browser redirect. Keep controller code simple - and put all your
logic in service or model classes.

Tom
> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/play-framework/-/uNEngKcfVa4J.
>
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framewor...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/play-framework?hl=en.
Reply all
Reply to author
Forward
0 new messages