No check of "undefined" possible

Monkey Forums/Monkey Bug Reports/No check of "undefined" possible

Midimaster(Posted 2012) [#1]
There is no check in monkey, whether a string is "undefined". If I get back a string from a Java- or JS- Modules, they can return me "undefined" strings. I cannot handle this, Monkey will crash if I use this string.

Do I do something wrong or is it a bug?

What is the difference between an empty string and an undefined string? Testing an undefined string with...

[monkeycode]If Result=NULL[/monkeycode]

...will crash


Samah(Posted 2012) [#2]
There are no nullable types in Monkey other than Object. The closest you can get with String is an empty string.


skid(Posted 2012) [#3]
IMHO Its hardly a bug, more a design strength.

Can you use Object as your return type and test result with null before casting to String?


muddy_shoes(Posted 2012) [#4]
You can sort of test for an undefined string because Monkey will convert a declaration of a String without a value to an empty string definition in the target language. An "If myString" check is converted to a test for zero length. Therefore...

    Local test:String
    
    If test
        Print "defined"
    Else
        Print "undefined"
    End


...will print "undefined".

It's all a bit opaque though and the whole "a string is sort of a primitive and sort of an object" doesn't help (same with arrays). As you're not saving anything by not declaring the string as empty in the first place you may as well just get into the habit of doing it.