QB64-Phoenix-Edition / QB64pe

The QB64 Phoenix Edition Repository
https://qb64phoenix.com
Other
131 stars 26 forks source link

Allow LEN to return the size of TYPEs using the TYPE name #345

Open a740g opened 1 year ago

a740g commented 1 year ago

LEN works correctly and returns the size of variable, whether the variable is of the QB64 fundamental type or an UDT.

Option _Explicit

Type Vector2
    As Single x, y
End Type

Dim v As Vector2, i As Long

Print Len(v), Len(i)

This correctly prints 8.

However, LEN should also be extended to report the size when it is used with a TYPE name (or possibly even numeric literals?). For example:

Option _Explicit

Type Vector2
    As Single x, y
End Type

Print Len(Vector2), Len(_Byte), Len(Long)

This would obviously require special handling of something like LEN(STRING).

FellippeHeitor commented 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

SteveMcNeill commented 1 year ago

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?

mkilgore commented 1 year ago

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.

SteveMcNeill commented 1 year ago

Could always go with a new command for _TypeLen and avoid that issue.

mkilgore commented 1 year ago

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.

SteveMcNeill commented 1 year ago

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.