static class "Functions" within a namespace

This forum is meant for questions and discussions about the X# language and tools
Post Reply
Karl-Heinz
Posts: 774
Joined: Wed May 17, 2017 8:50 am
Location: Germany

static class "Functions" within a namespace

Post by Karl-Heinz »

the code below throws two errors:

error XS0103: The name 'Test1' does not exist in the current context
error XS0103: The name 'Test2' does not exist in the current context

Test1() and Test2() exist, but only when qualified names are used they become visible to the compiler. when you activate the #DEFINE __OK__ the name of the static class changes from "Functions" to "Funcs". Now it compiles. It seems the compiler gets confused when the name "Functions" is used within this context.


Code: Select all

// #DEFINE __OK__

#IFDEF __OK__
 USING STATIC khr.Funcs

#ELSE
 USING STATIC khr.Functions
 
#ENDIF 



FUNCTION Start( ) AS VOID

     #ifndef __OK__ 

     // ok, gives no compiler error
 
	 ? khr.Functions.Test1()
	 ? khr.Functions.Test2() 
	 
     #endif 


	? Test1()
	? Test2() 
	
	
RETURN 



BEGIN NAMESPACE khr

#IFDEF __OK__
	STATIC CLASS Funcs
#ELSE		
    STATIC CLASS Functions
#ENDIF     	
    	
     STATIC METHOD Test1()  AS STRING
     	 	
     RETURN "Test1() called"
	
     	
     STATIC METHOD Test2()  AS STRING
     	 	
     RETURN "Test2() called"	
      	
     	
     END CLASS 	


END NAMESPACE

regards
Karl-Heinz
User avatar
Chris
Posts: 4913
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

static class "Functions" within a namespace

Post by Chris »

Hi Karl-Heinz,

Yes, this is because the compiler also creates a class named "Functions" under the hood, as a placeholder for all FUNCTIONs, GLOBALs and DEFINEs declared in the code, so your Functions class conflicts with the compiler generated class. I am afraid it will not be easy to fix that, so I suggest lways doing what you did, name your own class as "Funcs", this is what I always do as well. Having said that, it's probbaly a good idea for the compiler to identify this situation and give a better error message, though, will log this, thanks!

Chris
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
Karl-Heinz
Posts: 774
Joined: Wed May 17, 2017 8:50 am
Location: Germany

static class "Functions" within a namespace

Post by Karl-Heinz »

Hi Chris,

ok i see what you mean, and what´s added to such a "Functions" class. Question: When i create a DLL from the code below with undef __INCLUDE__ such a compiler generated "Functions" class would not really be needed, right ?

Code: Select all


// #define __INCLUDE__ 
 
BEGIN NAMESPACE khr2

#IFDEF __INCLUDE__
    // ---------------
	GLOBAL GLOBAL_Khr2 := 0 AS INT          // part of the compiler generated static "Functions" 
	DEFINE DEFINE_Khr2 := "1234" AS STRING  // part of the compiler generated static "Functions"  
   FUNCTION GLOBALFunc_khr2() AS INT        // part of the compiler generated static "Functions"  
   	RETURN 12 
   	// ----------------
#ENDIF
 
		
	STATIC CLASS Funcs
   	
     STATIC METHOD Foo()  AS STRING
     	 	
     RETURN "X# Foo() called"			
   
   END CLASS 
   

END NAMESPACE 


BEGIN NAMESPACE khr

#ifdef __INCLUDE__
    //  -----------------
	GLOBAL GLOBAL_Khr := 0 AS INT          // part of the compiler generated static "Functions" 
	DEFINE DEFINE_Khr  := "1234" AS STRING  // part of the compiler generated static "Functions" 
    //  -----------------	
#endif        
                  

	STATIC CLASS Globals
		
       CONST BUFFER_SIZE := 512 AS INT 
       STATIC FILE_NAME := "Output.txt" AS STRING  
       STATIC INITONLY CODE_PREFIX := "US-" AS STRING   
       
	END CLASS   
	
#IFDEF __INCLUDE__
   // ---------
   FUNCTION GLOBALFunc_khr() AS INT         // part of the compiler generated static "Functions"  
   	RETURN 12	
    GLOBAL GLOBAL2_Khr := "asdasd" AS STRING  // part of the compiler generated static "Functions"  
    // -------------
#ENDIF     
    


	STATIC CLASS Funcs
   	
     STATIC METHOD Test1()  AS STRING
     	 	
     RETURN "X# Test1() called"	
     	
     STATIC METHOD Test2()  AS STRING
     	 	
     RETURN "X# Test2() called"	
      	
     	
     END CLASS 

 
END NAMESPACE


to make it easier to see what i mean i´ve attached both dlls.

regards
Karl-Heinz
Attachments
Dlls.ZIP
(2.98 KiB) Downloaded 74 times
User avatar
Chris
Posts: 4913
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

static class "Functions" within a namespace

Post by Chris »

Hi Karl-Heinz,

Yes, theoretically it is possible to make the compiler generate a Functions class only when it is really needed, but I am afraid this will add much complication to the compiler code, intellisense etc, to check if each referenced library does have such a class or not etc. Unless Robert thinks it's relatively easy to allow this...

Chris
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
FFF
Posts: 1581
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

static class "Functions" within a namespace

Post by FFF »

Guys,
why has the compiler to use "Functions" ? It knows what is meant, so it could use whatever, e.g., "Others"... How many people will study the compiler output vs the many end coders?
Just a thought...
Karl
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
robert
Posts: 4522
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

static class "Functions" within a namespace

Post by robert »

Karl
We have chosen the Functions name to be compatible with Vulcan.
When we are fully self supporting we will most likely switch to another name that has no risk of conflicts, such as XSharp$Functions.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
Jamal
Posts: 315
Joined: Mon Jul 03, 2017 7:02 pm

static class "Functions" within a namespace

Post by Jamal »

Hi,

I wish you guys just disassociate X# from Vulcan baggage for good. I doubt there are many Vulcan developers who actually use it.

Jamal
Karl-Heinz
Posts: 774
Joined: Wed May 17, 2017 8:50 am
Location: Germany

static class "Functions" within a namespace

Post by Karl-Heinz »

Guys,

ok, compatibility with Vulcan is a valid point. And yes, at least another name like "XSharp$Functions" would be a step forward.

JFTR: Based on the KHRFuncsLibX_2.dll content i´ve attached a C# compiled Dll - of course with a namespace class "Functions" ;-)


regards
Karl-Heinz
Attachments
KHRFuncsLibC.ZIP
(1.32 KiB) Downloaded 73 times
Post Reply