create a new unknown type?

BlitzMax Forums/BlitzMax Programming/create a new unknown type?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

For reference, I refer to types as 'classes' throughout this post. Mentioning that since a lot of people I've talked to are confused by that.

Strict

Function make_another:Object( wimmens:Object )
    ' making this three lines instead of one so you see the normal steps
    Local id:TTypeID = TTypeID.ForObject( wimmens )
    Local other:Object = id.NewObject()
    Return other
End Function

Type Foo
    Global fooc%
    Field id%

    Method New()
        id = fooc
        fooc :+ 1
    End Method

    Method ToString$()
        Return "Foo "+id
    End Method
End Type

Type Bar
    Global barc%
    Field id%

    Method New()
        id = barc
        barc :+ 1
    End Method
    
    Method ToString$()
        Return "Bar "+id
    End Method
End Type

Local beep:Object = New Foo
Print beep.ToString()
beep = make_another( beep )
Print beep.ToString()

beep = New Bar
Print beep.ToString()
beep = make_another( beep )
Print beep.ToString()


This'll make a new one of anything. It's simple, and I don't know why you'd need to do this when you seem to have no need for a shallow or deep copy of an object, so who knows.

As for the second, you could also do that with reflection fairly easily, but you might have to have some means of distinguishing the difference between two classes that inherit from the same base class. If you have only one class and only ever will have one class that extends a base class, then you should just merge the two into a single class.