How to compile two PRGs that declare classes with the same name (different definitions)?

113 views
Skip to first unread message

marcop...@gmail.com

unread,
Sep 15, 2025, 11:25:28 AM (4 days ago) Sep 15
to Harbour Users

Hi everyone,

I’m working on a project where I need to compile two independent modules together:

  • NFXML.prg – legacy code that builds the entire NF-e object model.

  • NFComXML.prg – new code that builds the NF-Com object model.

Both files are generated automatically (they can’t be merged easily) and each contains dozens of CREATE CLASS … blocks.
The problem is that several class names repeat in the two PRGs, and their definitions are not identical.
A minimal example:

*-- in NFXML.prg -----------------
CREATE CLASS Cofins
   VAR CST, vBC, pCofins, vCofins, qBCProd, vAliqProd
   METHOD new() CONSTRUCTOR
   METHOD toHash()
   METHOD toJson() INLINE hb_jsonEncode( Self:toHash(), .T. )
ENDCLASS

*-- in NFComXML.prg --------------
CREATE CLASS Cofins
   VAR CST, vBC, pCofins, vCofins          &&  ← fewer vars, slightly different meaning
   METHOD new() CONSTRUCTOR
   METHOD toHash()
   METHOD toJson() INLINE hb_jsonEncode( Self:toHash(), .T. )
ENDCLASS

When I compile the two PRGs together (Harbour 3.2.0dev + hbmk2) the compiler, as expected, reports that the class “Cofins” is already defined.

Question:
Is there any clean way (namespaces, static libraries, conditional compilation, etc.) to let two PRGs expose classes with the same identifier but different implementations, so that each module can keep its own definition?

Constraints / context:

  • Refactoring the generators to rename every duplicate class would be painful – the list is fairly long (Cofins, Pis, Icms, Totais, etc.).

  • At runtime the two object trees (NFXml and NFComXml) never mix – they are instantiated separately.

  • If the answer is “no, Harbour’s OOP model doesn’t allow duplicate class names in the same build”, that’s fine – I’ll look into renaming or pulling one set into a DLL/SO. But I’d love to confirm before going down that path.

Thanks in advance for any guidance or patterns you may have used in similar situations!


Marcelo Antonio Lázzaro Carli

unread,
Sep 15, 2025, 12:27:31 PM (4 days ago) Sep 15
to harbou...@googlegroups.com

Caro colega estou desenvolvendo classe para nfe/nfce (não trabalho com nfecom), mas daria para acrescentar na classe. Se deseja conhecer a classe está em https://github.com/malcarli1/Nfe_Classe . Acho que serviria como base de estudo. Não sou craque em classe, mas me viro

 

 

 

 

Att.

 

                Marcelo A. L. Carli

                      Marília/SP

         Capital Nacional do Alimento ®

      https://malc-informatica.ueniweb.com

                 Insta: @malcarli25

      Email / Skype: marcelo...@gmail.com

 

******************************************************************************

Se for repassar, apague o meu nome e endereço.

Ajude a combater a propagação de vírus e spams

coloque TODOS os destinatários em CÓPIA OCULTA (Cco / Bcc)

******************************************************************************

--
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: https://groups.google.com/group/harbour-users
---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/harbour-users/3f93c6bb-2bb9-457f-9ac0-49068943486fn%40googlegroups.com.


Não contém vírus.www.avast.com

oleksa

unread,
Sep 15, 2025, 12:31:03 PM (4 days ago) Sep 15
to harbou...@googlegroups.com
Hi,

add STATIC to create class...:

CREATE CLASS Cofins STATIC
...

Regards,
Oleksii

15 вересня 2025, 18:25:34, Від "marcop...@gmail.com" <marcop...@gmail.com>:

Message has been deleted
Message has been deleted

juanpere...@gmail.com

unread,
Sep 16, 2025, 5:45:12 AM (3 days ago) Sep 16
to Harbour Users
Hello,

It is not possible to use two classes with the same name where both are public.

This limitation exists not only in Harbour but also in Java, Python, or C# - it cannot be done directly without some form of differentiation.

In Java or Python, you cannot have two classes with the same name in the same namespace or package without differentiating them, but there are ways to use them if they are in different contexts:

- In Java, two classes with the same name can coexist if they are in different packages. They can be differentiated using the fully qualified package name. For example, if you have `package1.MyClass` and `package2.MyClass`, you can use both by specifying the package or importing them with aliases. Additionally, within the same .java file, you can have multiple classes, but only one public class, and the file must be named after that public class. The concept of polymorphism also allows different classes to respond to the same method names with different behaviors.

- In Python, there is no formal package system like in Java, but it can be achieved through modules and packages (.py files and folders with __init__.py). Classes with the same name can be differentiated by importing them from different modules. The fully qualified names would be `module1.MyClass` and `module2.MyClass`. This allows two classes with the same name to exist in different contexts. Python uses dynamic typing and polymorphism to handle methods with the same name performing different actions.

In C#, it is also possible to have two classes with the same name as long as they are in different namespaces. To differentiate them, you use the fully qualified name including the namespace, for example:

csharp
namespace Project1 {
    public class MyClass {
        public void Method() {
            Console.WriteLine("MyClass in Project1");
        }
    }
}

namespace Project2 {
    public class MyClass {
        public void Method() {
            Console.WriteLine("MyClass in Project2");
        }
    }
}

// Usage:
Project1.MyClass obj1 = new Project1.MyClass();
Project2.MyClass obj2 = new Project2.MyClass();

obj1.Method(); // Prints "MyClass in Project1"
obj2.Method(); // Prints "MyClass in Project2"


There is no mainstream or major programming language that allows this directly without mechanisms to differentiate contexts. All serious object-oriented languages require some mechanism to avoid name collisions, such as namespaces, packages, modules, or namespaces.

Languages that DO NOT allow having two classes with the same name without differentiation mechanisms:

- Java
- C#
- C++
- Python (requires modules to differentiate)
- Ruby
- Visual Basic .NET
- Objective-C
- Swift
- Kotlin
- Scala
- Go
- PHP
- Dart
- TypeScript
- Perl
- Lua
- Smalltalk
- Eiffel
- etc.

The mechanisms used to differentiate classes with the same name are essential for maintaining order, avoiding ambiguities, and enabling scalability in object-oriented programming.

This is a general rule in object-oriented language design.

marcop...@gmail.com

unread,
Sep 16, 2025, 5:45:20 AM (3 days ago) Sep 16
to Harbour Users
Thanks Oleksii, this solve my problem.

José M. C. Quintas

unread,
Sep 16, 2025, 7:33:55 AM (3 days ago) Sep 16
to harbou...@googlegroups.com

oClass := MyCrazyClass(1)

oClass2 := MyCrazyClass(2)

oClass3 := MyCrazyClass(3)

FUNCTION MyCrazyClass( nType )

   LOCAL oClass

   DO CASE

   CASE nType == 1; oClass := Class1():New()

   CASE nType == 2; oClass := Class2():New()

CASE nType == 3; oClass := Class3():New()

ENDCASE

CREATE CLASS Class1 STATIC

...

CREATE CLASS Class2 STATIC

...


or,,, not sure about your needs

oProduto := Produto():New()

oProduto:Icms:Base := 10

oProduto:Icms:Aliquota := 10

oProduto:Ipi:Base := 10

....


CREATE CLASS Produto

   VAR Icms

   VAR Ipi

   VAR Pis

   Method New() CLASS Produto

   ::Icms := ImpostoClass():New()

   ::ipi := ImpostoClass():New()

   ::Pis := ImpostoClass():New

...

CREATE CLASS Impostoclass STATIC

   VAR Base INIT 0

   VAR Aliquota INIT 0

   VAR Valor INIT 0

ENDCLASS


José M. C. Quintas

Reply all
Reply to author
Forward
0 new messages