Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
X# examples - Hello world
Message
From
21/10/2019 01:41:49
 
 
To
10/10/2019 03:00:12
General information
Forum:
Visual FoxPro
Category:
VFP Compiler for .NET
Miscellaneous
Thread ID:
01671443
Message ID:
01671598
Views:
87
Macro compilation:

We all loved the XBase macro-compilation and in the early days of DBase it was the only way to emulate arrays:
LOCAL mVar, i
FOR i = 1 to 10
  mVar = "MEM" + PadL(Str(i), 2, "0")
  STORE i TO &mVar
ENDFOR
X# still support this.

Clipper however took this even further and created a type that was called a CODEBLOCK. It was a variable type that could contain compiled code and had the following syntax:
&& LOCAL oCB = { | [<paramList, ...>] | Comma separated list of Function/Procedures to be executed } -> Return last statement as return value.
LOCAL oCB = { | cText | Hello(cText) }
? Eval(oCB, "Johan") && Hello Johan
? oCB:Eval("Joe Soap") && Hello Joe Soap, the OOP coding style with the same effect

FUNCTION Hello(cTxt)
  RETURN "Hello " + cTxt
ENDFUNC
It was even taken further which opened the door for creating Dynamic Applications by providing a Macro Compilation feature for codeblocks:
LOCAL cMath = "{ | a, b, c | b = a * 2, c = a + b }"
LOCAL oMath = &cMath
? Eval(oMath, 2) && 6, a is here passed by value while b and c are used as "local" variables to the codeblock.
LOCAL cHello = "{ | txt | StrTran(Lower(Hello(txt)), 'o', 'O') }"
LOCAL oHello = &cHello
? Eval(oHello, "Johan") && hellO jOhan
? Eval(oHello, "ooooo") && hellO OOOOO
Since this was also part of Visual Objects, it is also part of X#, however in VO one could only use "lazy" declared functions and procedures for macro-compilation. X# took it further and even .NET strict types can be used provided they are loaded before the MacroCompiler is loaded:
LOCAL cNet = "{ | a | System.Math.Multiply(a, 2) + a }"
LOCAL oNet = &cNet
oNet:MCompile(cNet) && This also works in the OOP syntax
oNet = MCompile(cNet) && And if you rather want the XBase functional style
? oNet.Eval(2) && 6 is the answer again using OOP syntax
? Eval(oNet, 2) && And guess what the functional XBase works here too... with a surprise result of 6
So we have a tool where we can save some of our code in a DBF or SQL database and don't need to recompile when we make changes to our application.

For those who are interested I am busy writing an article series that can be downloaded from https://www.xsharp.info/itm-downloads?folder=general%252FLessons%252FDataDriven

Hope this is of interest to some and provide some late night humor developing a Hello World application in the Macro Compiled environment.
Johan Nel
George, South Africa
Friend of XSharp (FOX) member
E-mail : johan(dot)nel(at)xsinet(dot)co(dot)za
Web site : https://xsharp.info
X# runtime : https://github.com/X-Sharp/XSharpPublic
Radio talk : https://blog.aksel.com/2019/07/why-xsharp-might-be-tool-foxpro.html
Previous
Reply
Map
View

Click here to load this message in the networking platform