Duplicate Identifier (Method and Field same names)

BlitzMax Forums/BlitzMax Programming/Duplicate Identifier (Method and Field same names)

soja(Posted 2004) [#1]
It seems that you can't have a Method/Function and a Field/Global in a type with the same names. In other words, this reports a compile-time error: Duplicate identifier: 'foo'.
Type duh
	Field foo
	Method foo() ; End Method
End Type

Could someone explain why this is? It seems that the compiler would be able to know the difference between a function name and a variable name. Am I missing something?


Perturbatio(Posted 2004) [#2]
Bmax isn't the only language to not allow this, Delphi will not allow it. This:
Type testType = class
  test1 : integer;
  procedure test1;
end;

will not compile.

I would have to ask though, why would you want to add ambiguity by having duplicate identifiers?


soja(Posted 2004) [#3]
Well, in my type, I have a Global variable 'Count', and a Function called Count() which essentially returns Count. It doesn't seem that ambiguous to me because you need () for one and no () for the other. Ideally, Count wouldn't even be referenced by external objects (data hiding).

I could just name them differently, but i'd rather not if I didn't have to.


Perturbatio(Posted 2004) [#4]
I'd be tempted to prepend the field with a lowercase 'f' if I needed identifiers that were similar.


marksibly(Posted 2004) [#5]
Hi,

Unfortunately, allowing duplicate identifiers gets really messy really quickly.

For example, it *is* legal to refer to the Count function without using brackets. In this case, you get a function address which can be assigned to a function pointer etc.

There are other, more subtle variations on this and, all in all, disallowing such ambiguities means the language stays far more flexible.


soja(Posted 2004) [#6]
Aha, thanks for the explanation


FlameDuck(Posted 2004) [#7]
Incidently, traditionally accessor/mutator methods are prefixed with either 'Set' or 'Get', as fields are traditionally (in most non-BlitzMAX languages) 'private', which means inaccessable to other objects.

In your case the names would be SetCount and GetCount.


AaronK(Posted 2004) [#8]
I tend to prefix private field (And all my fields are generally private) with a '_' so if I don't want to use the get/set I go

Method size()
return _size
End Method

Field _size