PDA

View Full Version : type-constructor-ideas



ReneMiner
03-10-2013, 15:23
suggest MKUDT$ As t_Type

The new type-functions brought me to some point where I always have to setup some constructor-functions to a type which not necessary needs to be a function of this type since no element of that type exists before one has been created.
I have some idea to skip writing those always in the same manner appearing functions. I use mostly heap to organize dynamic sub-arrays to the type or to extend types with different other types or to store strings within types - so not to have troubles with theirs pointers.

Now to have some multiple useable constructor I thought of something like this to get the first steps into the direction:


#MinVersion 1.9.9.0.

Uses "console"

' have some arbitrary, different types

Type t_Type1
A As Long ' whatever...
B As Byte
Out As Function
End Type

' --- this the "constructor-function" t_Type1 ---
Function MKUDT_As_t_Type1( Optional A As Long, _
B As Byte _
) As String
' simplest way:
Function = MKL$(A)+ MKBYT$(B)
End Function

' this is just a regular type-function to print values out
Function t_Type1.Out() As Long
PrintL "Me.A =" + Str$(Me.A)
PrintL "Me.B =" + Str$(Me.B)
PrintL

End Function

' other type
Type t_Type2
C As Double
D As DWord
E As Ext
Out As Function
End Type

' --- this the "constructor-function" t_Type2 ---
Function MKUDT_As_t_Type2( Optional C As Double, _
D As DWord, _
E As Ext _
) As String

' use another approach here
Local locData As t_Type2

locData.C = C
locData.D = D
locData.E = E

'same result:
Function = Memory_Get( VarPtr locData, SizeOf t_Type2 )

End Function


Function t_Type2.Out() As Long
PrintL "Me.C =" + Str$(Me.C)
PrintL "Me.D =" + Str$(Me.D)
PrintL "Me.E =" + Str$(Me.E)
PrintL
End Function

' using string-way:
String sData = MKUDT_As_t_Type1( -123, 255 )

Dim check1 As t_Type1 At StrPtr(sData)
check1.Out

sData = MKUDT_As_t_Type2( 1.23, ,4.5678 )

Dim check2 As t_Type2 At StrPtr(sData)
check2.Out

' using heap-way:

DWord hData = HEAP_AllocByStr(MKUDT_As_t_Type1(-246,128))
SetAt( check1, hData )
check1.Out

HEAP_Free hData
hData = HEAP_AllocByStr(MKUDT_As_t_Type2(2.46,&HFFFFFFFF, 9.8765))

SetAt( check2, hData )
check2.Out

PrintL $CRLF + Repeat$(50,"-")
PrintL "key to end" + $CRLF
WaitKey


further thinking would be to create another function which will put the data directly to heap (HEAP_AllocByType) and return the pointer - but currently MKUDT$ would be just the functionality it needs...

ErosOlmi
03-10-2013, 21:05
Very shortly I will publish final thinBasic beta 1.9.9.0 with some few features regarding UDT functions: for example function piping (specify multiple cascading functions on the same UDT)



'
' Basic example of Vector type with OOP features
' Call UDT functions using cascading methods
'

Uses "console"

Type ctVector
' -- Vector components
x As Double
y As Double
z As Double

' -- Functions handling vector components
Init As Function
Zero As Function
Normalize As Function
Magnitude As Function
ToString As Function
End Type

Function TBMain()
' -- Define new variables
Dim a, b As ctVector


' -- Cascade methods
PrintL "String representation of vector a: ", a.Init(1, 2, 3).ToString
PrintL "String representation of vector b: ", b.Init(4, 5, 6).ToString

PrintL
' ---------------------------------------------

PrintL "Normalizing a: ", a.Normalize.ToString
PrintL "Normalizing b: ", b.Normalize.ToString

PrintL
' ---------------------------------------------

PrintL "Magnitude a: ", Format$(a.Magnitude, "#.00")
PrintL "Magnitude b: ", Format$(b.Magnitude, "#.00")

PrintL
' ---------------------------------------------

PrintL "Zeroing a: ", a.Zero.ToString
PrintL "Zeroing b: ", b.Zero.ToString

PrintL
' ---------------------------------------------

PrintL "Press any key to quit..."
WaitKey

End Function

' -- Initializes vector components
Function ctVector.Init(x As Single, y As Single, z As Single)

Me.x = x
Me.y = y
Me.z = z

End Function

' -- Zeros vector components
Function ctVector.Zero()

Me.x = 0
Me.y = 0
Me.z = 0

End Function

' -- Normalizes the vector to unit length
Function ctVector.Normalize()

Single length = Sqr(Me.x*Me.x + Me.y*Me.y + Me.z*Me.z)
Me.x /= length
Me.y /= length
Me.z /= length

End Function

' -- Returns vector magnitude = length
Function ctVector.Magnitude() As Single

Return Sqr(Me.x*Me.x + Me.y*Me.y + Me.z*Me.z)

End Function

' -- String representation of vector
Function ctVector.ToString() As String

Return "[" + Format$(Me.x, "#.00") + ", " + Format$(Me.y, "#.00") + ", " + Format$(Me.z, "#.00") + "]"

End Function


I have in mind what you are mentioning regarding initialization.
I would like to find an easy and elegant way.
I'm checking other languages like PHP and FreeBasic looking on what syntax they use for this purpose.

Ciao
Eros

Charles Pegge
03-10-2013, 22:09
A nice pair of curly of curly braces will do the job :)

dim as vector a={2,4,6}

for persistent (heap) objects:

dim as new vector a={2,4,6}

Petr Schreiber
04-10-2013, 17:39
I would vote for reserved <Type>.Create, <Type>.Destroy for such a tasks.

Petr

P.S. Function piping sounds great, I didn't expect it so soon!

ReneMiner
04-10-2013, 19:05
But Petr - how shall this work? I can't follow...


foo.Create... now what? - foo is of no type yet.

...my thoughts:

Dim Foo as t_Type = { 123, 456, ... } ' no good creating arrays this way interferes....

would make them undestroyable if they are no locals... so to stick with your suggest "Create":

Create as t_Type foo = { ... } - i don't care if curly or brackets or just parenthesis
Free foo - will leave one DWord left...

...destroy might be difficult - except all with "Create" made things are listed somewhere seperate from the variables. This then only works if these are treated as Variables but aren't: So to say - data stored to (internal managed) heap and some automatic setAt will place the virtual type-pattern over the data to access it if it were some variable.
The simplest that I can think of: if there were some new Variable-Type (built-in UDT) that can contain twice: a pointer to data "pData" and a (pointer to) Type-Description "pType" = how to use the data so it can create on call an absolute variable of the desired type at the data and then gets treated as if it were some variable - since it's a virtual one.
But how to make this?

I think this new variable-type has to be some special type containing not just "pData" and "pType" but also a function:



VariableOfNewType.Create() As t_myType


not to start building the house with roofing...

Edit:
call these things just "Entity" - not to confuse with objects...


[Dim As] Entity foo = Entity.Create(123,456,...) As t_myType

Petr Schreiber
04-10-2013, 19:25
Hi Rene,

good questions, but I see no issue - in case no <Type>.Create is defined, then it would work as it works in 1.9.9.0 test version.

When Create is defined, it is required to be used for instancing the object. In such a case, you would need to do something like:


DIM myObject AS tMyType = new ( params here )
myObject.DoSomething()

myObject = new (another params) ' -- here the old myObject calls Destroy first, if defined, to be uninitialized , and then the memory is reseted and reinitialised using Create method.


This will allow to make sure user assigns the needed parameters before further usage of the object. You would not call the Create method explicitly, but it would be used right after the memory is allocated by TB.

More practical example follows:


' -- This line first allocates ctFileWriter as such, and then calls ctFileWriter.Create with parameters passed after "new".
' -- The function will open specified file for writing
DIM textWriter AS ctFileWriter = new("C:\myFile.txt")
textWriter.WriteLine("Ciao")
textWriter.WriteLine("Rene")

' -- This line says, we want to create new instance to textWriter variable. Panic!
' -- First, it will call textWriter.Destroy, this function will close currently open file
' -- Second, ThinBASIC releases the memory
' -- Third, ThinBASIC allocates fresh memory for ctFileWriter, because variable was previously declared as ctFileWriter type
' -- Fourth, it calls ctFileWriter.Create on such a memory
textWriter = new("C:\myOtherFile.txt")

' -- In future, once inheritance is implemented, the NEW could be followed by type name
' -- In case no type name is specified, the default (the one of the variable) is presumed


Petr

ErosOlmi
04-10-2013, 21:26
Hi all,

at this stage TYPEs will remain TYPEs, I'm not going into too much into OOP. I'm just trying to add some life to TYPEs.
OOP syntax and OOP philosophy will be something for thinBasic 2.x so please do not kill me if for the moment I will concentrate on just TYPEs.

Ciao
Eros