1

How do make a function return *, by star, I mean the function is able to return anything, and then I can typecast it accordingly?

private function get_current_files_view():TileList
    {
        var tlist:TileList;
        //switch(this[fm_model.files_display_type])
        switch(vs_file_display.selectedChild)
        {
            case cvs_user_files:    
                tlist = tlist_files;
                break;

            case bx_shared_data:                    
                tlist = tlist_shared_with_me;
                break;
            default:    
                throw new Error('Invalid container found');
        }
        return tlist;
    }

Suppose in this function, I want this function to return both tilelist and datagrid (according to the case) what should be changed.

Plz let me know Thanks you.

3 Answers 3

11

Suppose in this function, I want this function to return both tilelist and datagrid (according to the case) what should be changed.

If that is the case, then you should return ListBase, which is a parent class of both the List and the DataGrid.

I you want to be able to return any object, you can specify the return type as Object.

If you really want to return anything, including primitve values, such as integers you can use the *, as was stated in another answer. IF this is truly what you need, my intuition is that you may need to refactor your application.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 Yes, I agree with that. Very often (but certainly not always), * is a cop out... especially if you know you want it to be one of two types only. I like using ListBase. Returning * is great when you really CAN return anything. If we ever get generics in AS3, then the majority of * returns will likely be generic returns instead.
8

You don't NEED to define a return type. If you don't, then you can return anything or nothing (not recommended):

private function get_current_files_view() { }

Or, you can define it that it must return something, but anything

private function get_current_files_view():* { }

Or, you could always use an interface or base class if you want to be specific:

private function get_current_files_view():ISomeInterface {}

Does this help? Or am I completely misunderstanding your question?

4 Comments

+1 for the suggestion to use an interface. I believe that is a much better development approach than having an * as the return type.
The Flex compiler in strict mode wont accept a method with no return type. It is best practice to always define a return type.
@TandemAdam: Agreed. I like to specifically define my routines as functions (return something... *, Type, Object, etc) or procedures (returns void). It is clear to the caller whether to expect a return or not. I will edit my response to add that suggestion. My variables, however, I am happy to mix in static typing and dynamic typing. In functions, however, we should specify something.
@TandemAdam I think the claim that the Flex Compiler "won't accept a method with no return type" in strict mode is a bit inaccurate. The Flex Compiler gives a warning, but a compile time warning doesn't prevent you from compiling the app.
2
private function whatsMyObject:(parameter:*):*
     {
     switch  (parameter.constructor)
             {
             case TileList:     trace("parameter is TileList");
                                break;

             case DataGrid:     trace("parameter is DataGrid");
                                break;

             default:           trace("parameter is neither TileList nor DataGrid");
             }

     return parameter;
     }

2 Comments

Always better to use is rather than the undocumented constructor property. It'll catch subclasses too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.