Open a740g opened 1 year ago
Since type sizes are calculated at compile time, it looks like a call to len(type name) would just need to be replaced with a constant when basic->c++ translation is in progress; the existing behavior allows for len(a&) to return 4, for example, since len already takes numeric variables
Doesn't QB64 allow types and variables to both share the same name?
TYPE foo .... DIM foo AS LONG
Print Len(foo) <-- how does this distinguish between the type and the long?
Doesn't QB64 allow types and variables to both share the same name?
TYPE foo .... DIM foo AS LONG
Print Len(foo) <-- how does this distinguish between the type and the long?
I was thinking that as well, but it's easy enough to just defer to the variable rather than the Type since that's what it would currently do anyway. I think it would also be worth producing a warning in that situation if the type of the variable isn't the Type of the same name.
IE: If there's a Type foo
then Dim foo as Foo: Print Len(foo)
would not produce a warning, while Dim foo as Long: Print len(foo)
would. In either case it prints the size of the variable though, not the Type
.
If it's a big concern that you wouldn't be able to do Len(type)
when a variable with that name is in scope then we could add a keyword to go along with the type name. It could probably even just be Type
: Len(Type foo)
. I'm not sure how much it's worth doing that though.
Could always go with a new command for _TypeLen and avoid that issue.
True, we could introduce a new command. The nice thing about using Len()
though is that Len(typename)
feels natural enough that I frequently attempt it before forgetting it's not actually allowed. I'm guessing @a740g has done the same thing, and that suggests to me that if we can add it to Len()
people will just naturally use it without having to learn another command.
Another idea, and one that shouldn't break existing code, or have conflicts with anything else, would be to add an optional parameter:
LEN(x) is the variable. LEN(x, _Var) is the variable. LEN(x, _Type) returns the size of the type x. LEN(x, _Const) returns the size of the const x.
LEN
works correctly and returns the size of variable, whether the variable is of the QB64 fundamental type or an UDT.This correctly prints
8
.However,
LEN
should also be extended to report the size when it is used with aTYPE
name (or possibly even numeric literals?). For example:This would obviously require special handling of something like
LEN(STRING)
.