Returning Type generates errors

Previous topic - Next topic

aroldo

I am trying to return a type from a function inside the Type, I am getting this error:
Quote*** Configuration: WIN32 ***
precompiling:
GPC - GLBasic Precompiler V.9.829 SN:b4f5b7db - 3D, NET
"Vectors1-10.gbas"(207) error : wrong argument type :

The compiler points to the last line in the code saying the error is there.

I get this error on both GLB V10.xxx and V11.
Searching the forum I found this post from Slydog that say we can return a type,
here is my code.

Code (glbasic) Select
http://www.glbasic.com/forum/index.php?topic=8048.msg67541#msg67541

This is what I trying to accoumplish, the code below is in JAVASCRIPT
Code (glbasic) Select
PVector mouse = new PVector(mouseX,mouseY);
PVector dir = PVector.sub(mouse,location);


I am using the Type as a class.
Code (glbasic) Select
TYPE TObject
x#
y#
z#
FUNCTION set: x, y
self.x = x
self.y = y
ENDFUNCTION
FUNCTION add: v AS TObject
    self.x = self.x + v.x
    self.y = self.y + v.y
    ENDFUNCTION
    FUNCTION subv: v AS TObject
    self.x = self.x - v.x
    self.y = self.y - v.y
    ENDFUNCTION
    FUNCTION subv2: v1 AS TObject, v2 AS TObject
    v1.x = v1.x - v2.x
    v1.y = v1.y - v2.y
   RETURN v1 /// <====== THIS GENERATES THE ERROR
    ENDFUNCTION

ENDTYPE
[a http://apd-games.com/][img http://apd-games.com/images/APDGames135.png][/a]
MacBook Pro OS X El Capitan
XCode Version 7
iPhone 6 running  iOS 9
iPad Mini running  iOS 7.1
Galaxy S5
Dell Latitude Windows 8 Enterprise
Palm Pre, Palm Pre2

MrTAToad

It would be more to do with aesthetic value than anything else (it would retain the look and logic of the original code).

Anyhoo, this works :

Code (glbasic) Select
TYPE TObject
        x#
        y#
        z#
        FUNCTION set: x, y
                self.x = x
                self.y = y
        ENDFUNCTION
        FUNCTION add: v AS TObject
            self.x = self.x + v.x
            self.y = self.y + v.y
    ENDFUNCTION
    FUNCTION subv: v AS TObject
            self.x = self.x - v.x
            self.y = self.y - v.y
    ENDFUNCTION
    FUNCTION subv2 AS TObject: v1 AS TObject, v2 AS TObject
            v1.x = v1.x - v2.x
            v1.y = v1.y - v2.y
           RETURN v1 /// <====== THIS DOES NOT GENERATE THE ERROR
    ENDFUNCTION
ENDTYPE[/code[

aroldo

MrTAToad,

Thank you! :nw:
It works very nice and now I don't have to rely on GLOBAL variables!

The GLBasic documentation is not up to date and if you read it it doesn't tells you how to return TYPES. I thought the same way that OCEAN said
QuotePosted by: Ocean
« on: Today at 10:48 am » Insert Quote
do you really need to return 'v1', as such objects are passed by reference anyway?
It took me a while to find out that I could do classes using TYPES and functions inside TYPES.
More over I did not know the existence of self. inside a Function and neither that we could declare the return type on the function signature like you did.
QuoteFUNCTION subv2 AS TObject:

This definitely will help me to use the inheritance and make my code cleaner.

I can contribute and help the GLBasic team updating for instance the documentation on the TYPE topic.

Let me know if you do.
[a http://apd-games.com/][img http://apd-games.com/images/APDGames135.png][/a]
MacBook Pro OS X El Capitan
XCode Version 7
iPhone 6 running  iOS 9
iPad Mini running  iOS 7.1
Galaxy S5
Dell Latitude Windows 8 Enterprise
Palm Pre, Palm Pre2

MrTAToad

#3
Yes, documentation in that area could be a bit clearer - however, I do mention it in my book :)

aroldo

Nice to know about your books.

I have a question does the Programming CookBook IV includes all the material of I , II and III?

Also where can I see the table of contents of you book or some excerpt of them?
[a http://apd-games.com/][img http://apd-games.com/images/APDGames135.png][/a]
MacBook Pro OS X El Capitan
XCode Version 7
iPhone 6 running  iOS 9
iPad Mini running  iOS 7.1
Galaxy S5
Dell Latitude Windows 8 Enterprise
Palm Pre, Palm Pre2

MrTAToad

There may be a slight overlap of code between the different CookBook books, but it would only be very slight.

There aren't any TOC's for those.  However, for the PRG book, this is the table of contents :

QuoteIntroduction..............................................................................3
Tutorial #1...............................................................................5
Tutorial #2..............................................................................15
Tutorial #3..............................................................................20
Tutorial #4..............................................................................22
Tutorial #5..............................................................................23
Tutorial #6..............................................................................26
Tutorial #7A.............................................................................30
Tutorial #7B.............................................................................34
Tutorial #8..............................................................................39
Tutorial #9..............................................................................47
Tutorial #10.............................................................................48
Tutorial #11.............................................................................50
Anatomy of a GLBasic Program.............................................................58
Executable sizes.........................................................................59
2D Graphics..............................................................................62
Text Font................................................................................63
Sprites..................................................................................64
3D Graphics..............................................................................65
Graphics In General......................................................................67
Sounds And Music.........................................................................68
File I/O.................................................................................69
Using C/C++ (or perhaps another language)................................................70
Optional Subroutines.....................................................................79
Variables................................................................................81
Compiling................................................................................83
Compiling for webOS......................................................................84
Compiling for Android....................................................................98
Movement And Animations.................................................................103
Fading Up and Down......................................................................104
Portrait and Landscape..................................................................105
Icons And Graphics......................................................................106
Network Lobby...........................................................................107
Limitations.............................................................................109
Programming Guidelines..................................................................113
Style Guidelines........................................................................115
Installation Problems...................................................................116
Error Messages..........................................................................117
Available Commands in GLBasic...........................................................122
Available Commands in GLBasic (Category Order)..........................................135
   ?.................................................................................139
   A.................................................................................145
   B.................................................................................156
   C.................................................................................161
   D.................................................................................170
   E.................................................................................186
   F.................................................................................193
   G.................................................................................202
   H.................................................................................224
   I.................................................................................225
   J.................................................................................236
   K.................................................................................237
   L.................................................................................239
   M.................................................................................252
   N.................................................................................260
   O.................................................................................272
   P.................................................................................274
   R.................................................................................284   S.................................................................................298
   T.................................................................................339
   U.................................................................................344
   V.................................................................................348
Index (Cont)

Section.......................................................................Page

   W.................................................................................349
   X.................................................................................355
   Z.................................................................................390
   b.................................................................................391
   g.................................................................................393
   s.................................................................................394
   Member Operator(.)................................................................395
   Comments (//).....................................................................396

aroldo

Thank you for the info,  I just purchased 2 books:


  • Programming CookBook IV - GLBasic Routines
  • Programming Cookbook
[a http://apd-games.com/][img http://apd-games.com/images/APDGames135.png][/a]
MacBook Pro OS X El Capitan
XCode Version 7
iPhone 6 running  iOS 9
iPad Mini running  iOS 7.1
Galaxy S5
Dell Latitude Windows 8 Enterprise
Palm Pre, Palm Pre2

MrTAToad

#7
Thanks for that!  Hope you find them useful!