Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4057750
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:59:31+00:00 2026-05-20T14:59:31+00:00

I’d like to be able to pass Vectors around as references . Now, if

  • 0

I’d like to be able to pass Vectors around as references. Now, if a method takes a Vector.<Object>, then passing a Vector.<TRecord>, where TRecord inherits directly from Object does not work. Where a method takes just plain Object; say vec: Object, then passing the Vector is possible. Once inside this method, an explicit cast at some stage is required to access vec as a Vector again. Unfortunately, a cast seems to make a copy, which means wrapping one up in multiple Flex ListCollectionViews is useless; each ListCollectionView will be pointing to a different Vector.

Using Arrays with ArrayCollection presents no such problem, but I lose out out the type safety, neatness (code should be clean enough to eat off of) and performance advantages of Vector.

Is there a way to cast them or pass them as references in a generic manner without copies being made along the way?

Note in this example, IRecord is an interface with {r/w id: int & name: String} properties, but it could be a class, say TRecord { id: int; name: String} or any other usable type.

protected function check(srcVec: Object): void
{
  if (!srcVec) {
    trace("srcVec is null!");
    return;
  }
  // srcVec = (@b347e21)
  trace(srcVec.length); // 4, as expected
  var refVec: Vector.<Object> = Vector.<Object>(srcVec);
  // refVec = (@bc781f1)
  trace(refVec.length); // 4, ok, but refVec has a different address than srcVec
  refVec.pop();
  trace(refVec.length); // 3 ok
  trace(srcVec.length); // 4 - A copy was clearly created!!!
}

protected function test(): void
{
  var vt1: Vector.<IRecord> = new Vector.<IRecord>;  // (@b347e21) - original Vector address
  var vt2: Vector.<Object> = Vector.<Object>(vt1);   // (@bbb57c1) - wrong
  var vt3: Vector.<Object> = vt1 as Vector.<Object>; // (@null)    - failure to cast
  var vt4: Object = vt1;                             // (@b347e21) - good
  for (var ix: int = 0; ix < 4; ix++)
    vt1.push(new TRecord);
  if (vt1) trace(vt1.length); // 4, as expected
  if (vt2) trace(vt2.length); // 0
  if (vt3) trace(vt3.length); // vt3 is null
  if (vt4) trace(vt4.length); // 4
  if (vt1) trace(Vector.<Object>(vt1).length); // 
  trace("calling check(vt1)");
  check(vt1);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:59:32+00:00Added an answer on May 20, 2026 at 2:59 pm

    This is not possible. If a type T is covariant with type U, then any container of T is not covariant with a container of type U. C# and Java did this with the built-in array types, and their designers wish they could go back and cut it out.

    Consider, if this code was legal

    var vt1: Vector.<IRecord> = new Vector.<IRecord>;  
    var vt3: Vector.<Object> = vt1 as Vector.<Object>; 
    

    Now we have a Vector.<Object>. But wait- if we have a container of Objects, then surely we can stick an Object in it- right?

    vt3.push(new Object());
    

    But wait- because it’s actually an instance of Vector.<IRecord>, you can’t do this, even though the contract of Vector.<Object> clearly says that you can insert Object. That’s why this behaviour is explicitly not allowable.

    Edit: Of course, your framework may allow for it to become a non-mutable reference to such, which is safe. But I have little experience with ActionScript and cannot verify that it actually does.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.