Void not enforced?

Community Forums/Monkey2 Talk/Void not enforced?

therevills(Posted 2016) [#1]
Looks like Void as the "return type" is not enforced... is it meant to be?




marksibly(Posted 2016) [#2]
Not sure what you mean, but :Void is optional for function declarations, eg: this...

Function blah()
End

...and this...

Function blah:Void()
End

...are the same.


therevills(Posted 2016) [#3]
Hey Mark - I always used Strict for MX1 so I'm use to the compiler telling me off if I dont put Void at the end and I thought MX2 had MX1 strictness on by default?


marksibly(Posted 2016) [#4]
I guess it could be considered an 'anomoly' in an otherwise fairly strict language, but I'm OK with it.

What I didn't like about 'auto int' return was you'd get so used to it, you'd start forgetting to add return types here and there, and suddenly functions that tried to return .5 or "hello" were returning 0 - and it was often hard to find out what was going on!

But there's no danger of that with 'auto void' return types, because the function *can't* return anything, and trying to call the function and expecting a value back will fail.

Also, readability-wise, to me it's clear that this returns 'nothing' (which is what void means-ish):

Function Blah() 'no type = void type.
End

I did consider allowing 'void' to be drop from function pointer var decls, but IMO it just looked too weird, eg:

Local t:() 'WTF?

vs...

Local t:Void() 'better...


therevills(Posted 2016) [#5]
For a MX/BMX language I prefer not putting Void. At the moment I'm mostly coding in BMX and just playing with MX2, so I'm copying some MX1 code with the Void and when typing I'm in BMX mode so I'm leaving it out... which makes my MX2 code a mix with methods/functions with and without the Void (looks a bit messy).


Leo Santos(Posted 2016) [#6]
It makes sense to me that you only need to declare the return type if you're actually returning something, otherwise it's assumed to be "Void".

Even though I learned M1 in strict mode, recently I was really enjoying non-strict mode. It gives you a "scripting language like" simplicity, without any performance hit.
I'm glad I can keep some of those practices (like not declaring :Void) in M2.

P.S. I know that not declaring the return type in non strict M1 implies "Int", but it doesn't really matter if you're not actually returning anything. I prefer M2's way.