Type Question

New to FreeBASIC? Post your questions here.
Post Reply
nimdays
Posts: 236
Joined: May 29, 2014 22:01
Location: West Java, Indonesia

Type Question

Post by nimdays »

I don't know, but similar name doesn't throw warning or error.

Code: Select all

type a
    as integer b,c
end type

dim as a a
a.b = 10
a.c = 20

dim as a ptr ap = @a
?ap->b
?ap->c

ap->b = 30
ap->c = 40

?a.b
?a.c

sleep
Thanks.
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: Type Question

Post by sancho3 »

Do you think there should be a warning?
It seems to me that it is a feature of the compiler to be able to correctly distinguish between the two.
Everything works as expected in your sample code and there could be no way that you meant for the code to do anything else.
Also if I remember correctly C# allows this, and Python as well.
nimdays
Posts: 236
Joined: May 29, 2014 22:01
Location: West Java, Indonesia

Re: Type Question

Post by nimdays »

sancho3 wrote:Do you think there should be a warning?
It seems to me that it is a feature of the compiler to be able to correctly distinguish between the two.
Everything works as expected in your sample code and there could be no way that you meant for the code to do anything else.
Also if I remember correctly C# allows this, and Python as well.
Thanks, A bit curious about this one.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Type Question

Post by fxm »

This is allowed as long as the UDT does not have a member procedure or member field initializer, but I do not recommend declaring two different entities with the same name.
nimdays
Posts: 236
Joined: May 29, 2014 22:01
Location: West Java, Indonesia

Re: Type Question

Post by nimdays »

fxm wrote:This is allowed as long as the UDT does not have a member procedure or member field initializer, but I do not recommend declaring two different entities with the same name.
Thanks, I'll use different name next time.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Type Question

Post by Tourist Trap »

nimdays wrote:
Thanks, I'll use different name next time.
Hi,
from my side I'm using a convention that helps me knowing at one glance what is the familly of names I'm using.

Code: Select all

type A
   as integer _a
end type

dim a as A
a._a => 9
But you can follow fxm advices, they are valuable. Anyway, having a strong convention of naming helped me writing relatively long code without error.

For pointers I would append Ptr to the name:

Code: Select all

dim as A ptr    aPtr
SARG
Posts: 1755
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Type Question

Post by SARG »

Hi,

A month ago I found that about naming guidelines. Could be usefull :
http://www.sourceformat.com/standard/as ... ADING1-779

There is also a page about comments :
http://www.sourceformat.com/standard/as ... ADING1-645
nimdays
Posts: 236
Joined: May 29, 2014 22:01
Location: West Java, Indonesia

Re: Type Question

Post by nimdays »

@Tourist Trap, Thanks for the advice.
@SARG, Thanks for the guideline.

For function name, Usually i use something like "Is_func" than "IsFunc"
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Type Question

Post by Tourist Trap »

Hi nimdays, you are welcome. To continue on the steps of SARG's comment, I would propose you if you re interested this tutorial of mine, written some monthes ago when I tried to explore the question of naming in FB. fxm has answered there so there are some interesting advices, at least from him ;)
https://freebasic.net/forum/viewtopic.p ... t=alias%2A
Post Reply