B4X++ — A Proof of Concept for OOP-style B4X Library Development
Hi everyone,
I would like to introduce
B4X++, a small proof-of-concept project that explores how more advanced OOP-style features could be used when writing B4X code, especially for developers creating
B4XLib libraries.
GitHub repository:
https://github.com/soinalastudio/B4XPP
First of all, I want to be very clear:
B4X++ is not intended to replace the B4X IDEs, and it is not a definitive proposal for changing the B4X language.
I fully understand that one of the strengths of B4X is its simplicity, and that we do not necessarily want to overload the language with too many new keywords or concepts.
The idea is rather similar to
TypeScript generating JavaScript:
B4X++ code would be written with some additional syntax, then precompiled into standard B4X code that can be opened, compiled, and used normally in the existing B4X IDEs.
The main goal is to experiment with features that could be useful for
library authors, especially when building reusable components where there is often a lot of repeated code.
For example, B4X++ currently explores concepts such as:
- #Extends
- #Override
- Super.Method
- #Property
- inherited designer properties
- inherited events
- generation of flattened .bas files compatible with normal B4X projects
To demonstrate the idea, I included a simple
Animals example written in B4X++, showing how inheritance and overrides could help structure code in a cleaner way.
I also attached a more concrete example:
B4XAnalogClock, converted as a B4X++ experiment and generated as a compilable
B4XLib project, directly usable from standard B4X.
#Interface B4XClockDrawable
Public Sub Draw(Canvas As B4XCanvas, CenterX As Float, CenterY As Float, Radius As Float)
#End Interface
#Class B4XClockDrawableBase Abstract
#Property Visible As Boolean = True
#Property Color As Int = 0xFF000000
Sub Class_Globals
End Sub
Public Sub Initialize
End Sub
Virtual Sub Draw(Canvas As B4XCanvas, CenterX As Float, CenterY As Float, Radius As Float)
End Sub
#End Class
#Class B4XClockHand Extends B4XClockDrawableBase
#Property AngleDegrees As Float = 0
#Property LengthRatio As Float = 0.70
#Property TailRatio As Float = 0.08
#Property HandWidth As Float = 3
Sub Class_Globals
Private mKind As String
End Sub
Public Sub Initialize(Kind As String)
Super.Initialize
mKind = Kind
End Sub
Override Sub Draw(Canvas As B4XCanvas, CenterX As Float, CenterY As Float, Radius As Float)
If mVisible = False Then Return
Dim r1 As Float = Radius * mLengthRatio
Dim r2 As Float = Radius * mTailRatio
Dim x1 As Float = B4XClockMath.PointX(CenterX, r1, mAngleDegrees)
Dim y1 As Float = B4XClockMath.PointY(CenterY, r1, mAngleDegrees)
Dim x2 As Float = B4XClockMath.PointX(CenterX, -r2, mAngleDegrees)
Dim y2 As Float = B4XClockMath.PointY(CenterY, -r2, mAngleDegrees)
Canvas.DrawLine(x2, y2, x1, y1, mColor, mHandWidth)
End Sub
Public Sub getKind As String
Return mKind
End Sub
#End Class
#Class B4XClockHourHand Extends B4XClockHand Final
#Constructor
Super.Initialize("hour")
mLengthRatio = 0.48
mTailRatio = 0.06
mHandWidth = 5dip
#End Constructor
#End Class
#Class B4XClockMinuteHand Extends B4XClockHand Final
#Constructor
Super.Initialize("minute")
mLengthRatio = 0.68
mTailRatio = 0.08
mHandWidth = 3dip
#End Constructor
#End Class
Again, this is still very early and should be considered only as a
proof of concept. Nothing is final, and the syntax or implementation may change completely.
My main objective is to explore whether this kind of precompiler could help B4X library developers avoid repetition, improve code organization, and build larger reusable components more comfortably, while still keeping the final output fully compatible with the existing B4X ecosystem.
I would be very interested to hear your thoughts, suggestions, criticism, or ideas.
Thanks!