Casting type and signature function

I wonder how I could sign my personal function (example: function onTweets (datas: RightType): Void), so I can iterate over the data directly. Results without extracting results before the for? / P> loop

private function onTweets(datas:Dynamic):Void {
    var tweets:Array<Tweet> = new Array<Tweet>();
    var results:Hash<Dynamic> = datas.results;
    for (data in  results ) {
        var tweet:Tweet = new Tweet( { from_user: data.from_user , created_at : data.created_at , text : data.text } );
        tweets.push(tweet);
    }
    this._datas = datas ;
    this._tweets = tweets ;
    this._next(tweets);
}

Here's what my object looks like in Chrome :

enter image description here

Tweets come from a call to $ .getJSON. thank

+3
source share
1 answer

Why do you cast in Hash if the results seem to be an array?

private function onTweets(datas:{ results : Array<Dynamic> }):Void {
    var tweets:Array<Tweet> = new Array<Tweet>();
    for (data in datas.results ) {
        var tweet:Tweet = new Tweet( { from_user: data.from_user , created_at : data.created_at , text : data.text } );
        tweets.push(tweet);
    }
    this._datas = datas ;
    this._tweets = tweets ;
    this._next(tweets);
}

Is this what you want?

edit: You can even define a more specific type:

typedef TweetData = {
    created_at: String,
    from_user: String,
    from_user_id: Int,
    from_user_id_str: String
};

private function onTweets(datas:{ results:Array<TweetData> }):Void
+1
source

All Articles