Page 1 of 5 123 ... LastLast
Results 1 to 10 of 46

Thread: TypeOf- ideas

  1. #1
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,530
    Rep Power
    171

    TypeOf- ideas

    I urge for some way to determine TypeOf a variable and to conserve it somehow..so it can be used as an Alias for variable-types.
    I did different attempts using thinBasic_VariableGetInfoPtr but somehow seems to return randomized different values in MainType and also does not work on Udt-Subsets because can not pass "VarPtr(myVar.X)" and expect valid results from it. TypeOf should work on all primitives - for UDTs it probably would just return %Type_UDT...

    Currently I have a function which could use TypeOf-Results - in this case just some Peek-Replacement so I can set arbitrary type to Peek at ptr.

    Uses "console"
    
    Begin Const
      %Type_Unknown = 0
      %Type_Byte
      %Type_Integer
      %Type_Boolean
      %Type_Word
      %Type_DWord
      %Type_Long
      %Type_Quad
      %Type_Single
      %Type_Double
      %Type_Currency
      %Type_Ext
      %Type_Number        = 20
      %Type_AsciiZ        = 25   
      %Type_String        = 30 
      %Type_GUID          = 40
      %Type_Variant       = 50
      %Type_UDT           = 60
      %Type_PTR           = 70
      %Type_Object        = 80
      %Type_Class         = 90
    End Const
    
    String sTest = MKL$(12345)
    ' test Memory_Read:
    PrintL Memory_Read StrPtr sTest, %Type_Long 
    
    sTest = MKD$(12.345)
    PrintL Memory_Read StrPtr sTest, %Type_Double
    
    sTest = "Test"
    PrintL Memory_Read StrPtr sTest, %Type_String 
    
    PrintL $CRLF + "key to end" + $CRLF
    
    WaitKey  
    
    ' ----------------------------------------------------
    ' some Peek-Any-Replacement...
    Function Memory_Read( ByVal mPtr    As DWord, _ 
                          ByVal as_Type As Long   _
                         ) As Any 
    
      If mPtr Then
        Select Case as_Type
          Case %Type_Byte
            Function = Peek(mPtr)
          Case %Type_Integer    
            Function = Peek(Integer, mPtr)
          Case %Type_Boolean              
            Function = Peek(Boolean, mPtr)
          Case %Type_Word                 
            Function = Peek(Word, mPtr)
          Case %Type_DWord, %Type_PTR             
            Function = Peek(DWord, mPtr)
          Case %Type_Long               
            Function = Peek(Long, mPtr)
          Case %Type_Quad             
            Function = Peek(Quad, mPtr)
          Case %Type_Single
            Function = Peek(Single, mPtr)
          Case %Type_Double
            Function = Peek(Double, mPtr)
          Case %Type_Currency     
            Function = Peek(Currency, mPtr)
          Case %Type_Ext
            Function = Peek(Ext, mPtr)
          Case %Type_String
            Function = Memory_Get(mPtr, Peek(DWord, mPtr-4) )
          
          'Case Else
           ' not possible - maybe optional passed 
           ' Long Size could extend this -
           ' but could just use Memory_Get then...
           
        End Select                       
        
      EndIf
    End Function
    
    Now the idea is to have a TypeOf-function that returns some "Replacement" or Value for
    BYTE, INTEGER, WORD, DWORD, LONG, QUAD, SINGLE, DOUBLE, EXT, CURRENCY, STRING, ASCIIZ, VARIANT, GUID, (UDT)
    that could be used instead of these keywords as well in Peek+Poke and also to dim as ...(except %Type_UDT of course) - even if it would mean to do it alike that:

    X = Peek( VarType(myType),  myPtr)
    Dim Y As VarType(%Type_Long) ' where %Type_Long could be replaced by any variable
    Poke( VarType(TypeOf(Z)), Varptr(Z), newValue ) ' String probably not possible here...
    
    Last edited by ReneMiner; 31-08-2013 at 19:43.
    I think there are missing some Forum-sections as beta-testing and support

  2. #2
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,530
    Rep Power
    171
    OK- forget the above- some different way:


    Imagine in pre-parsing before script runs the magic behind the curtains would just count types of current code in memory- starting with core: Byte = 1, Long = 2,...
    thereafter count the UDTs contained within the used modules - and enumerate them. Finally count UDTs from Script. And if I write

    ' =====================================
    Dim X As 30 ' only means "Dim X as String"
    ' =====================================
    
    Type t_Type
       dPtr   As Dword
       dType As Long
       Addition As Function
    End Type
    
    Function t_Type.Addition() As Any
    
      Local localData As Me.dType At Me.dPtr  ' uses the "magic number"
    
       Function = localData.Addition() ' call a function of current datatype...
    
    End Function
    
    
    Type t_SubType1
      A As Long
      B As Byte
      Addition As Function
    End Type
    
    Function t_SubType1.Addition() As Long
      Function = Me.A + Me.B
    End Function
    
    Type t_SubType2
      X As Double
      Y As Double
      Addition As Function
    End Type
    
    Function t_SubType2.Addition() As Double
      Function = Me.X + Me.Y
    End Function
    
    ' -- - - -- - - -- - - -- - - --
    
    Dim Data(2) As t_Type
    
    Data(1).dPtr = Heap_AllocByStr(MKL$(123)+MKByt$(127))
    Data(1).dType = TypeOf(t_SubType1)  ' - give Data information about subtype to use
    
    Data(2).dPtr = Heap_AllocByStr(MKD$(12.3)+MKD$(45.6))
    Data(2).dType = TypeOf(t_SubType2) 
    
    '...
    
    I think there are missing some Forum-sections as beta-testing and support

  3. #3
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,530
    Rep Power
    171
    I can't let go of it...

    Today I have another idea - what if

    Dim X As "String" were valid

    - and TypeOf(X) would return "String" ?
    I think there are missing some Forum-sections as beta-testing and support

  4. #4
    OxygenBasic has typeof

    It is a member of then "of" keyword family (sizeof spanof ...) and mostly used for diagnostics.

    It can also be used to create variables, though I am not sure if this is good programming practice

    double d
    ..
    typeof (d) e 'type of e is now double

  5. #5
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,530
    Rep Power
    171
    But could I do this in oxygen:

    Dim A As t_myUDT

    X = TypeOf(A)

    Dim B As X ' - B should be of Type t_myUDT now...

    - so "to conserve" the type for later to create some absolute variable of the "conserved type" at some memory?
    I don't know maybe X ist just a Long that holds a Type-Number - maybe it's a string that holds the types name or maybe it's a pointer to the sturcture of the type...
    Last edited by ReneMiner; 05-10-2013 at 16:30.
    I think there are missing some Forum-sections as beta-testing and support

  6. #6
    No, Oxygen would treat X as a string "myUDT".

    O2 is statically typed: all types have to be resolved at compile-time.

  7. #7
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404
    How does c++ do it with templates? I have been trying to figure out how to implement data containers of any type: int, float, class, udt... in oxygen.


    http://en.wikipedia.org/wiki/Template_(C%2B%2B)

  8. #8
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,530
    Rep Power
    171
    When I check out the linked wiki-page above then I'm so happy that we are dealing with flexible, readable basic here and not with brainpain C++.

    The type exists at start - and the variable to create is just a virtual, mostly a local one - never really allocating space, but just to interpret already stored data the right way...

    Might it be possible to allow a function (string directly would serve as well) as type-name-substitute ?

    Perhaps I should change from "TypeOf" to some new expressions - both ways
    - MKType$() + CVType()

    Dim A As CVType("Byte") At pMyData
    '...
    
    Type t_myType1  ' arbitrary udt...
      A As Long
      B As Byte
      C As Single
    End Type
    
    Dim foo As t_myType1
    
    String sType = MKType$(foo)
    '...sType would hold "t_myType1" now
    
    Dim dummy As CVType( sType ) At Varptr foo
    

    I realized Overlay already being a keyword - but does it have any functionality?
    I think there are missing some Forum-sections as beta-testing and support

  9. #9
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404
    Just putting this link as a reference for others interested in this sort of thing. A pretty good explanation between templates and generics.
    http://msdn.microsoft.com/en-us/libr.../sbh15dya.aspx

  10. #10
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,530
    Rep Power
    171
    Is there any chance that keyword "AS" could accept a type stored in a string?
    I think there are missing some Forum-sections as beta-testing and support

Page 1 of 5 123 ... LastLast

Similar Threads

  1. OOP ideas
    By ErosOlmi in forum Suggestions/Ideas discussions
    Replies: 13
    Last Post: 26-08-2013, 20:26
  2. More Ideas
    By peter in forum Sources, Templates, Code Snippets, Tips and Tricks, Do you know ...
    Replies: 3
    Last Post: 27-10-2012, 14:47

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •