On 3/5/22 2:48 AM, Bram Moolenaar wrote:
Ernie Rael wrote:
I saw some recent msgs about comparing anything to null, and that it
would be allowed. I guess I got the wrong impression. Using vim:
"Included patches: 1-4507"
In my first vim9 script, I did (the real code conditionally return null)
def Get(): string
return null
enddef
And this doesn't compile because "Type mismatch; expected string but got
special".
Allowing null seems like an improvement.
I am still looking into how we should use null. In many places null is
handled the same as an empty string. In this example you might as well
return an empty string, since it will be the same for most purposes.
There are also complications, such as when assigning null to a variable,
when getting that value it looks like the variable wasn't initialized,
so it will be initialzed then and when it is a list, get an empty list
instead of null. That might be confusing.
It seems the idea of reading a null variable, except when comparing, gives
an empty item is OK, but since I haven't tried coding with it...
I said empty item is OK except when comparing. But, assuming a null string,
what about when compariing to something other than null? For example
if sval == ''
is that true or false when sval is null? I'd say true, which feels
consistent with the idea of null representing an empty item and
if sval == null
is true if and only if when sval is null. The comparison explicitly to null
would be the only way to detect null vs empty. This would seem to avoid the
java-NPE type issues. There seems to be little opportunity for confusion.
Also, a null list cannot store a type, because the type is kept on the
list. Not sure if this matters, since this only matters for declared
variables, which, as just mentioned, are initialized to an empty
list/dict/etc., and then the type is added.
This means that assigning null to a structured item looses the previous
type of the contents. I'm not familiar enough with vim9 to have an
opinion or feeling as to what issues might arise, but...
BTW, it is disconcerting that "string sval" or "list<string> l_sval" does
not work :-) But var works pretty well and it doesn't seem to be and issue.
At the end of this msg is a more complete example of where/how I want the
null assignment
In my case, I can do "def Get(): any", but then there's a runtime checking.
That is not a good solution, any reason you can't return an empty
string?
Would allowing it to be null incur some additional runtime check? The
following fails at runtime, so that can't be it entirely:
var xxx = 'foo'
xxx = 'bar'
xxx = null
I guess how to handle a "null string" might be part of the issue. In my
case, when the value is null, it only gets used in a comparison.
That's what was implemented so far, comparing with null.
vim9script
# Here's a version of what I'm after.
# Note: sNull is introduced to bypass vim9 null type checking
# since can't assign null to string
var sNull = '-=XYZZY=-'
var table = { foo: 'bar', baz: sNull }
# if not a global setting, do the default behavior
# global setting of '' or 'None' returns null
# null return means do not process the return
def Get(key: string): string
var val = g:->get('my_settings_' .. key, null) ### NOTE: vim likes this
if val == 'None' || val == ''
return sNull ### .vimrc say don't do anything
endif
if val == null ### not specified, do the default
val = table->get(key, sNull)
val = val != sNull ? '-' .. val : sNull
endif
return val
enddef
# if Get(xxx) != sNull
echo '1' Get('foo')
echo '2' Get('baz')
echo '3' Get('bad-key')
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups "vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/vim_dev/c73afdbe-21f0-b732-bb66-0c2c7b65982b%40raelity.com.