PDA

View Full Version : another weird Alias-thought



ReneMiner
01-10-2014, 11:00
imagine the following, you have some standard-vartype as byte, long, dword and want to give it functions...

so it's like an udt-variable that has no further udt-elements but can have functions so it's not required to create an additional list of subitems that have to be part of the variables name then

???

maybe like this



' you know what this does:

Alias Dword As DataPtr

' ???

' but now imagine this simple example:

Type Alias DWord As DataPtr ' means this Type DataPtr consists of one Dword only
' only functions allowed here !!!
' - but statics were thinkeable... ;)
GetData As Function
SetData As Function
End Type

Function DataPtr.GetData() As String
Return Heap_Get(Me)
End Function

Function DataPtr.SetData(Byval sData as String) As Boolean
If Heap_Size(Me) Then Heap_Free(Me)
If StrPtrLen(StrPtr(sData)) Then
Me = Heap_AllocByStr(sData)
Function = (Me <> 0 )
Else
Me = 0
Function = True
Endif
End Function



' lot's of ideas for this...
' examples:
Dim myPtrs(Expression) as DataPtr [At ...]

Type t_XYZ
pData as DataPtr
'...
End Type

Type Alias TBGL_tRGBA As myColor ' would built-in udt work too?
'...
End Type

Type myStuff Extends DataPtr ' also a nice one then...
'...
End Type


Type Alias Long As Windowstate ' should be simple then
Minimize As Function
Maximize As Function
'...
End Type

Type Alias String As Username ' could be a little tricky with strings...
'...
End Type
Type Alias String As Password ' but user can use either StrPtr(Me) etc.
'... or heap could help out here to store these
End Type


and we could have built-in-vartypes that have user-definded functions.... think of the possibilities - each variable could behave different by itself... :)

currently there's some "workaround" since it does not work using "Me" without ".pData" do some overlay at me

see below (runs)


Uses "console"

Type t_DataPtr
'since simply
' Dword '<<< will ERROR
pData As DWord ' this is just to occupy the space but never used as such...
SetData As Function
GetData As Function
End Type

Function t_DataPtr.GetData() As String
Local lPtr As DWord At VarPtr(Me)

Return HEAP_Get(lPtr)
End Function

Function t_DataPtr.SetData(ByVal sData As String) As Boolean
Local lPtr As DWord At VarPtr(Me)

If HEAP_Size(lPtr) Then HEAP_Free(lPtr)

If StrPtrLen(StrPtr(sData)) Then
lPtr = HEAP_AllocByStr(sData)
Function = ( lPtr <> 0 )
Else
lPtr = 0
Function = TRUE
EndIf
End Function

Dim x As t_dataptr

If x.setData("this test") Then
PrintL x.GetData() & " was successful"
Else
PrintL "test failed"
EndIf

WaitKey


not just for storing dataptrs and data at heap but also to have variables that may limit themselves or react on certain actions under certain circumstances.

Edit:
maybe no alias but just



Type As Long t_Windowstate
Minimize As Function
'...
End Type

:confused:

ReneMiner
01-10-2014, 20:44
... the thing is: i just want to have a long- or dword-, byte-, quad-, whatever-simple type with functions - according to it's name ("alias"). So no "x.pData" but just x since x is supposed to be just a dword, byte, quad, whatever. so if defining some udt-variable "alias/as" dword, byte etc. the udt just consists of that single variable-type and the udt-variables name should be enough to access it - but no ".UDT_SubElementsName" needed in this case because it's the only one.

mike lobanovsky
01-10-2014, 23:34
VisualBasic 6 (and FBSL v3, for that matter) utilize the word Default in a Type declaration to set forth the member in that UDT that can be assessed without referring to its member name. For example:


Type MySize
Default Area As Long
Length As Long
Breadth As Long
End Type

Dim Size As MySize

Size.Length = 2
Size.Breadth = 2
Size = Size.Length * Size.Breadth

MsgBox Size ' popups 4 that's actually Size's Area field

So Default would be a legit 3rd-generation BASIC keyword to denote what you want:


Type DoMinimize
Default Shrink As Function
End Type

Dim Minimize As DoMinimize

Minimize hMyWnd ' Shrink your window




(P.S. If thinBasic were not the only BASIC you care about, it would be much easier and less verbose for you to express your morning ideas.)

ReneMiner
02-10-2014, 11:08
however its possible to have variables as %myNumConst or $myStringConst to be unchangeable once initialized. So there's a way to block these somehow...

if this kind of udt that just can consist of one simple variable as in example above can just be changed through keyword "ME" when currently inside a function that's pointer should be identic to one that is stored in the udt-structure then we could have "privates".

it just would require that assigning or reading values from variables of this type are "unaccesseable" - except if replaced through "Me"
But it would also require that the functions (that are not declared as private) are accesseable if the real variables name is used


+Mike
thanks for your reply, it's indeed the case that i am not a professional programmer and none forces me to code. thinBasic in the meantime has implemented a few of my thoughts and it's becoming some perfectly customized language - it's fun to me. If i'd like to use another language then i would post into their forums ;)

mike lobanovsky
02-10-2014, 21:41
If i'd like to use another language then i would post into their forums :wink:

Perhaps. But there's at least one where you can't -- not any more.

ReneMiner
04-10-2014, 13:01
Back to the reason of this thread, not for the private matter but for the possibility to have "simple variables" with functions without the need of any additional subsets. So i can dim foo as t_Type where t_Type can only have functions but no variables noded below, instead it has a value of type "Byte","Long" etc.



Uses "console"

Alias DWord As DataPtr

Type any_Data Extends DataPtr ' :( won't work...
Be As Function
Is As Function
End Type

dim foo as any_Data ' foo should be a dword with functions now...
' foo.Be("some data") should be valid
' inside Type-function "Me" would be a simple Dword and not been followed by a dot.

PrintL " -- key to end --"
WaitKey



Mike,
might be that vb6-support was stopped in march 2008 - or is your forum down?