Scope and Types

For other topics related to the FreeBASIC project or its community.
stephanbrunker
Posts: 62
Joined: Nov 02, 2013 14:57

Scope and Types

Postby stephanbrunker » Jun 11, 2014 12:08

Hello,

unwillingly I encountered a hard-to-find problem. If you're designing a program which includes the windows.bi or anything else with a lot of prefinied types, there is a bug (or inexpected behavior) if you dimensionate a variable which name is already one of the types. And this is unaffected by a scope block (or function, loop etc.):

Code: Select all

type abc
    a as string
    b as integer
end type


dim x as string = "adkfd"
scope
    Dim abc As String = x
    print abc
    print sizeof(abc)
    print len(abc)
    dim st as string = abc
    print st
    print len(st)
    print sizeof(st)
end scope


The variable works correct, but the LEN and SIZEOF functions ignore it if there's a TYPE of the same name. The problem is, you don't know all types in the header files and if your program doesn't work, that error is one very hard to find.

Is there a possible solution? (Except for changing the name of your variable after a error occurs ...)
fxm
Posts: 9707
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Scope and Types

Postby fxm » Jun 11, 2014 12:34

dkl
Site Admin
Posts: 3212
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Scope and Types

Postby dkl » Jun 11, 2014 14:01

It's possible to use extra parentheses to force len() to treat it's argument as expression instead of type:

Code: Select all

type a
   i as integer
end type

dim a as string

print len((a)), len(a)
print sizeof((a)), sizeof(a)


It's an ugly work-around, but sizeof()/len() just are pretty ambigious by design, since they accept both types and expressions as argument. As far as I know it worked like that in QB aswell (in case of len()). Also see this post in #718.
stephanbrunker
Posts: 62
Joined: Nov 02, 2013 14:57

Re: Scope and Types

Postby stephanbrunker » Jun 11, 2014 14:34

Thanks,

I saw that solution in the link to the bugreport, too. It's now just to make it a habit to use len(()) for expressions. Better that than searching for hours why your code don't work ...

Perhaps you can update the documentation? That LEN searches first for types, and then for expressions?
fxm
Posts: 9707
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Scope and Types

Postby fxm » Jun 14, 2014 5:53

stephanbrunker wrote:Perhaps you can update the documentation? That LEN searches first for types, and then for expressions?

- Already explained at KeyPgLen
- KeyPgSizeof ⇒ FxMwikki [As already done for LEN, added similar explanation on usage of SIZEOF((variable))]

Return to “Community Discussion”

Who is online

Users browsing this forum: No registered users and 5 guests