Request : if local myint:int = somefunc()

BlitzMax Forums/BlitzMax Programming/Request : if local myint:int = somefunc()

skn3(Posted 2005) [#1]
Just a small request. It would be great (for shorthand) if blitz were able to do this..
function SomeFunc()
	return rand(0,9)
end function

if local myint:int = SomeFunc() = 1
	print "the myint was initialized with the number 1"
end if


This is how you have to do it at the moment
local myint:int = SomeFunc()
if myint = 1
	print "the myint was initialized with the number 1"
end if


Currently you can do it with for loops:
for local:i = 0 to 10
	print "i = "+i
next


It is purely for shorthand, but it would be nice.


Robert(Posted 2005) [#2]
Won't this cause some scope issues.

In the first code example, myint belongs to the IF statement, doesn't that mean that it will 'die' before any ELSE or ELSEIF statements?


skn3(Posted 2005) [#3]
Well it is upto the creator of the language. I assume that anything "inside" the block is local to the block. The local declaration should belong to the parent block of that if.


ImaginaryHuman(Posted 2005) [#4]
How about

function SomeFunc()
        return rand(0,9)
end function

Local myint:int=1
if myint & SomeFunc() = myint 'if both are 1
        print "the myint was initialized with the number 1"
end if

Or is that not what you want to do?

Otherwise why not do:
function SomeFunc()
        return rand(0,9)
end function

Local myint:int
if SomeFunc() = 1
        myint=1
        print "the myint was initialized with the number 1"
end if

??

I kind of see what you're saying... you want to be able to test the value of SomeFunc() at the same time as putting that value into the myint variable?

Umm....
function SomeFunc(myint var)
        myint=rand(0,9)
end function

if SomeFunc() = 1
        print "the myint was initialized with the number 1"
end if

???????


skn3(Posted 2005) [#5]
Read what I said :)
local myint:int = SomeFunc()
if myint = 1
print "the myint was initialized with the number 1"
end if


I was suggesting that it be possible to set a variable and test the value set on the same line, yes.


marksibly(Posted 2005) [#6]
No, I wont be doing this.

The problem is the dual use of '=' to mean both assignment and comparison.

For/Next is 'special' because it already uses '=' to mean assignment, therefore adding the 'Local' bit doesn't change the semantics of it at all.

However, having...

If a=x

...mean something entirely different from...

If Local a=x

...is not cool.


Bot Builder(Posted 2005) [#7]
I agree, it just adds some unecessary shortcut that really makes your code a bit harder to read. This isnt C++ :)

Might be a bit faster to type, but there's not too much point.


skn3(Posted 2005) [#8]
Ok :) I guess my suggestion can only really work if the language incases expressions in ()


ImaginaryHuman(Posted 2005) [#9]
Or give us macro's ;-)