Jump to content

Initializing a variable with an expression


Angel
 Share

Recommended Posts

The Local, Global and Dim documentation says that it is possible to initialize the variables when they are declared:

You can also declare multiple variables on a single line:

Dim $a, $b, $c

And pre-initialize, if variable is not an array:

Dim $a = 2, $b, $c = 20

But, is it correct to initalize them with a funcition call or any other expression, like, for instance:

Dim $x = String(3+ 4 * 12)

And what if the expression returns a vector, would that still be allowed? The doc says that you can pre-initialize, if the variable is not an array... I think the docs are a bit unclear in this regard. If this is possible to do it should say so and there should be an small example in there.

Thanks,

Angel

P.S.- Wow, this is my 3rd post in the support forum today! :">

Link to comment
Share on other sites

Can't you just test this and find out?

<{POST_SNAPBACK}>

I did test it and it worked. But it seems to contradict what it says in the doc (i.e. that you can only pre-initialize non-arrays). That is why I ask. I want to make sure that I am not just using the language in a way that I am not supposed to. That is all.

Also, if this is really possible, I think that the wording of the Dim documentation should be modified a little to make this more clear.

Ah! And thanks to the answer to my other post (the one about DllCall) :lmao:

I also tried that before asking, but again I was not sure that it was a safe thing to do...

Cheers,

Angel

Link to comment
Share on other sites

I think the confusion here is you couldn't do something like;

Dim $a[1][2]=???;what can you put here?

Doesn't make sense, you can't initialize in that way obviously.

If you are assigning something to a variable as it's created that's slighty different I would've thought. I dunno how it works technically, and don't wanna get shot down by guessing, but this is why the docs say you can't initialize an array, because, well, you can't really.

Link to comment
Share on other sites

I think the confusion here is you couldn't do something like;

Dim $a[1][2]=???;what can you put here?

Doesn't make sense, you can't initialize in that way obviously.

If you are assigning something to a variable as it's created that's slighty different I would've thought. I dunno how it works technically, and don't wanna get shot down by guessing, but this is why the docs say you can't initialize an array, because, well, you can't really.

<{POST_SNAPBACK}>

That is exactly what confused me. I thought that that is what the doc meant, but I was not sure. It seems it is possible (and allowed) to take a function whose output is an array and directly assign it to a variable as it is declared, but I find the doc a bit confusing in this regard.

So it seems that is not possible to initialize an array like Chris_1013 said, but you can use a function that returns an array to initialize a variable. Perhaps this clarification should be added to the doc?

Cheers,

Angel

Link to comment
Share on other sites

I think the confusion here is you couldn't do something like;

Dim $a[1][2]=???;what can you put here?

Doesn't make sense, you can't initialize in that way obviously.

If you are assigning something to a variable as it's created that's slighty different I would've thought. I dunno how it works technically, and don't wanna get shot down by guessing, but this is why the docs say you can't initialize an array, because, well, you can't really.

<{POST_SNAPBACK}>

Well....you "could" pre-init an array by running it in a loop after creating it...but why? If the values you initialize the array to are going to be destroyed at some later time, it's just as easy to check if the array location is a null string, or if it contains something.

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...