Strict - Function return values

BlitzMax Forums/BlitzMax Programming/Strict - Function return values

Scott Shaver(Posted 2005) [#1]
I would like to suggest that if Strict mode is used functions and methods not be allowed to return types that are not the type specified by the function/method.

function foo()
Local x:Float=1
return x
end function

should not compile. The coder should be forced to cast the value to an int in the above case. Actually I think this should be the case even when strict isn't used. At the very least a warning should be produced.


Dreamora(Posted 2005) [#2]
Numeric casts are done implicitely, so it would be more of a harm than use if you needed to cast it explicitely for returns. (means local a = float (b) is casted automatically)


Scott Shaver(Posted 2005) [#3]
I'm not suggesting that assignments don't get implicitely casted only the return values. If I can't return the incorrect Type from a method I shouldn't be able to return an incorrect primitive.

This is of course, my opinion. The use of the strict keyword should enforce the highest level of type checking.


N(Posted 2005) [#4]
I see no logic in this.


Chris C(Posted 2005) [#5]
what your asking for exists...

Strict
Local b:Int=blah()
Print "b="+b

Function blah:String()
Local a:Int=123
Return a
End Function

wont compile cause it says cant convert string to int...
the reason the return works is because int seem to get automatically cast to strings

strict
local a:string
a=123
print a

will work, just like return does...


Scott Shaver(Posted 2005) [#6]
however this will compile and is in my opinion incorrect:

function foo()
Local a:Float=1.2
return a
end function

the incorrect value is getting returned from the function.


Chris C(Posted 2005) [#7]
yeah automatic cast to int by the looks - not too strict is it!


N(Posted 2005) [#8]
This compiles in C:
int weeble( )
{
     return 5.55858473f;
}


So I don't see why BlitzMax shouldn't be able to automagically cast to and from different (numeric) data types unless you're some nutty pedantic person. ;)


Dreamora(Posted 2005) [#9]
He is just lazy or not willing to "use his brain" while sending out returns ;-)


Scott Shaver(Posted 2005) [#10]
Acutally I'm not the one that had a problem with this. It was another user on the forum and the since this is a "basic" language it should do what it can to keep less experienced folks from having these problems.

Lazy would be people that aren't willing to do a simple cast to the correct type. :)


Curtastic(Posted 2005) [#11]
Why does it force the exact type when initializing arrays?This code doesn't work and I find it very annoying. Think if the whole language was like this, you would have to convert all your Int and Double parameters to Float just for a drawrect command! I don't care if Cos() actually accepts Double only, convert it for me!
Local a:Float[2]
a=[1.1, 2]

Local a:Byte[2]
a=[1, 2]



Scott Shaver(Posted 2005) [#12]
As I said before:


I'm not suggesting that assignments don't get implicitely casted only the return values. If I can't return the incorrect Type from a method I shouldn't be able to return an incorrect primitive.

This is of course, my opinion. The use of the strict keyword should enforce the highest level of type checking.





Curtastic(Posted 2005) [#13]
ok, I was just taking this line out of context. its really easy to do
"The use of the strict keyword should enforce the highest level of type checking."