Author Topic: Messing around with types and overloading.  (Read 4550 times)

0 Members and 1 Guest are viewing this topic.

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Just as the topic says, and I came up with this little bit of code but it could be taken much further.

Code: [Select]
type vector2d
    x as single
    y as single
end type

type vector3d
    x as single
    y as single
    z as single
end type

function length overload(byref vector as vector2d)as single
    function=sqr(vector.x^2+vector.y^2)
end function

function length(byref vector as vector3d)as single
    function=sqr(vector.x^2+vector.y^2+vector.z^2)
end function

sub normalise overload(byref vector as vector2d)
    dim lr as single=1.0/length(vector)
    vector.x*=lr
    vector.y*=lr
end sub

sub normalise(byref vector as vector3d)
    dim lr as single=1.0/length(vector)
    vector.x*=lr
    vector.y*=lr
    vector.z*=lr
end sub

function set_vector overload(byval x as single,byval y as single)as vector2d
    dim vector as vector2d
    vector.x=x
    vector.y=y
    function=vector
end function

function set_vector(byval x as single,byval y as single,byval z as single)as vector3d
    dim vector as vector3d
    vector.x=x
    vector.y=y
    vector.z=z
    function=vector
end function

sub print_vector overload(byref vector as vector2d)
    print vector.x;",";vector.y
end sub

sub print_vector(byref vector as vector3d)
    print vector.x;",";vector.y;",";vector.z
end sub




sub main
   
    dim v2d as vector2d=set_vector(-3.0,7.0)
    print_vector v2d
    normalise v2d
    print_vector v2d
   
    print

    dim v3d as vector3d=set_vector(-5.0,6.0,2.0)
    print_vector v3d
    normalise v3d
    print_vector v3d
   
end sub

main
sleep
end

Offline rdc

  • Pentium
  • *****
  • Posts: 1495
  • Karma: 140
  • Yes, it is me.
    • View Profile
    • Clark Productions
Re: Messing around with types and overloading.
« Reply #1 on: March 11, 2007 »
Nice. You could extend this further by using the extended types in the CVS version.

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Messing around with types and overloading.
« Reply #2 on: March 11, 2007 »
Cool rdc, I've not gone into types in a lot of depth myself so far but for what I have used them has been useful. Even knowing a bit about them I found the exampes with FB not particularly easy to follow and thought it might be helpful to have a topic with bits n pieces from anyone showing how they can be used.

EDIT:
can FB do inline functions?
« Last Edit: March 11, 2007 by Stonemonkey »

Offline rdc

  • Pentium
  • *****
  • Posts: 1495
  • Karma: 140
  • Yes, it is me.
    • View Profile
    • Clark Productions
Re: Messing around with types and overloading.
« Reply #3 on: March 11, 2007 »
Inline function aren't supported, at least yet. I am not sure if that is in the works or not. It would be quite handy though.

Offline rdc

  • Pentium
  • *****
  • Posts: 1495
  • Karma: 140
  • Yes, it is me.
    • View Profile
    • Clark Productions
Re: Messing around with types and overloading.
« Reply #4 on: March 11, 2007 »
Here is your code using the new extended type syntax, which is available in the CVS version.

Code: [Select]
Type vector2d
    x As Single
    y As Single
End Type

Type vector3d
    x As Single
    y As Single
    z As Single
End Type

'Used to identify which type of vector in union
Const is2d = 1
Const is3d = 2

Type Vector
    Private:
    vtype As Integer 'this identifies a 2D or 3D vector
    Union         'we can use a union here to save some space
        v2 As vector2d
        v3 As vector3d
    End Union 'Set up our overloaded constructors
    Public:
    Declare Constructor (Byval xx As Single,Byval yy As Single)
    Declare Constructor (Byval xx As Single,Byval yy As Single, zz As Single)
    Declare Property GetVectType() As Integer 'Read-only property
    Declare Function Length() As Single
    Declare Sub Normalise ()
    Declare Operator Cast() As String
End Type

Constructor Vector (Byval xx As Single, Byval yy As Single)
    this.vType = is2d
    this.v2.x = xx
    this.v2.y = yy
End Constructor

Constructor Vector (Byval xx As Single, Byval yy As Single, Byval zz As Single)
    this.vType = is3d
    this.v3.x = xx
    this.v3.y = yy
    this.v3.z = zz
End Constructor

Property Vector.GetVectType() As Integer
    Return this.vType
End Property

Function Vector.Length () As Single
    If this.vType = is2d Then
        Return Sqr(this.v2.x^2 + this.v2.y^2)
    Else
        Return Sqr(this.v3.x^2 + this.v3.y^2 + this.v3.z^2)
    End If
End Function

Sub Vector.Normalise()
    Dim lr As Single
   
    lr = 1.0 / this.Length
    If this.vType = is2d Then
        this.v2.x  *= lr
        this.v2.y *= lr
    Else
        this.v3.x  *= lr
        this.v3.y *= lr
        this.v3.z *= lr
    End If   
End Sub

Operator Vector.Cast() As String
    If this.vtype = is2d Then
        Return this.v2.x & ", " & this.v2.y
    Else
        Return this.v3.x & ", " & this.v3.y & ", " & this.v3.z
    End If
End Operator

Sub main
    Dim vectType As Integer

    Dim v2d As Vector = vector(-3.0,7.0)
   
    vectType = v2d.GetVectType
    If vectType = is2d Then
        Print "Vector is 2D."
    Else
        Print "Vector is 3D."
    End If
    Print v2d
    v2d.normalise
    Print v2d
   
    Print

    Dim v3d As Vector = vector(-5.0,6.0,2.0)

    vectType = v3d.GetVectType
    If vectType = is2d Then
        Print "Vector is 2D."
    Else
        Print "Vector is 3D."
    End If
    Print v3d
    v3d.normalise
    Print v3d
   
End Sub

main
Sleep
End

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Messing around with types and overloading.
« Reply #5 on: March 11, 2007 »
Ah, nice one rdc. I'll have a look through this.

Offline rdc

  • Pentium
  • *****
  • Posts: 1495
  • Karma: 140
  • Yes, it is me.
    • View Profile
    • Clark Productions
Re: Messing around with types and overloading.
« Reply #6 on: March 11, 2007 »
I just converted your code over; there are probably other things you might need here such as member properties for the individual vector elements. The external consts aren't really needed. You could use an internal identifier such as 2 for 2D and 3 for 3D. I added the consts to illustrate Properties and so that you could determine what type of vector this is (such as passing it to sub of function).
« Last Edit: March 12, 2007 by rdc »

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Messing around with types and overloading.
« Reply #7 on: March 12, 2007 »
operator overloading and typing tricks is getting quite C++y there!

Jim
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Messing around with types and overloading.
« Reply #8 on: March 12, 2007 »
rdc-"I just converted your code over"

yep, it's good to see it done like that though, I know what's going on so thanks.


Jim-"operator overloading and typing tricks is getting quite C++y there!"

Even just the basics have helped me so far to getting into c++, I'm slowly getting there.

Offline rdc

  • Pentium
  • *****
  • Posts: 1495
  • Karma: 140
  • Yes, it is me.
    • View Profile
    • Clark Productions
Re: Messing around with types and overloading.
« Reply #9 on: March 13, 2007 »
The extended types have their uses, but it will get much more interesting when we have true classes in FB. I am not sure when that will happen, but it can't be too far off.