Procedures inside Structures

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
inc.
Enthusiast
Enthusiast
Posts: 406
Joined: Thu May 06, 2004 4:28 pm
Location: Cologne/GER

Re: Procedures inside Structures

Post by inc. »

akj wrote:I also like Aonym's syntax but I think the variation below is neater.

Code: Select all

Structure foobar
  ; Members
  x.i
  y.i
  ; Methods
  Declare.i bar(z.i)
  Declare   set(x.i, y.i)
EndStructure
Why using "Declare"? Its not needed as in PureBasic's syntax you can declare the return type at the end of the methods name like its already possible in interfaces:

Code: Select all

Structure foobar
  ; Members
  x.i
  y.i
  ; Methods
  bar.i(z.i)
  set(x.i, y.i)
EndStructure
Check out OOP support for PB here!
akj
Enthusiast
Enthusiast
Posts: 665
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Re: Procedures inside Structures

Post by akj »

@inc:

I did not realise you could do that for procedures within structures. Thank you.
Anthony Jordan
Zach
Addict
Addict
Posts: 1654
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: Procedures inside Structures

Post by Zach »

Adding my vote for this idea. I think it would be neat to see this and I would probably use it a lot.
Image
User avatar
bembulak
Enthusiast
Enthusiast
Posts: 572
Joined: Mon Mar 06, 2006 3:53 pm
Location: Austria

Re: Procedures inside Structures

Post by bembulak »

Adding my vote for this idea. I think it would be neat to see this and I would probably use it a lot.
+1
cheers,

bembulak
moogle
Enthusiast
Enthusiast
Posts: 372
Joined: Tue Feb 14, 2006 9:27 pm
Location: London, UK

Re: Procedures inside Structures

Post by moogle »

I'd like to have this too.
Image
buddymatkona
Enthusiast
Enthusiast
Posts: 252
Joined: Mon Aug 16, 2010 4:29 am

Re: Procedures inside Structures

Post by buddymatkona »

Without OOP, "procedures inside a structure" are a good way to organize and store everything in one place however it might lead to some confusion with multiple source versions of a procedure. By the way, I hope as hardware gets faster and faster, compiler writers will again prefer a 2-pass approach so the tool can automate simple tasks rather than pass them on to the user. (mindless things like prototyping procedures present in the source code :D )
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Procedures inside Structures

Post by skywalk »

buddymatkona wrote:(mindless things like prototyping procedures present in the source code )
Prototypes are not always explicit :!:
Given pointers, you can write multiple prototypes for the same function, given your preference. :wink:
...example...

Code: Select all

WinLib = OpenLibrary(#PB_Any, "kernel32.dll")
If WinLib
  Prototype.l GetTempPath(nBufferLength.l, *lpBuffer.i)
  Global GetTempPath.GetTempPath = GetFunction(WinLib, "GetTempPathA")
  
  Prototype.l GetTempPathSTR(nBufferLength.l, lpBuffer.s)
  Global GetTempPathSTR.GetTempPathSTR = GetFunction(WinLib, "GetTempPathA")
EndIf
buf.s = Space(513)
fn = GetTempPath(512, @buf) 
Debug Trim(buf) ; -> "C:\DOCUME~1\SKYWALK\LOCALS~1\Temp\"

buf = Space(513)
fn = GetTempPathSTR(512, buf) 
Debug Trim(buf) ; -> "C:\DOCUME~1\SKYWALK\LOCALS~1\Temp\"
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
buddymatkona
Enthusiast
Enthusiast
Posts: 252
Joined: Mon Aug 16, 2010 4:29 am

Re: Procedures inside Structures

Post by buddymatkona »

I was not talking about all prototypes. Those containing mindless repetition can be supplied by the compiler. A 1-pass compiler may need a simple prototype just because the procedure comes after its first reference in the source file. A 2-pass compiler can find all the definitions before it deals with references.
From a user interface point of view, the goal should be to never require the user to supply the same information more than once unless required to resolve ambiguity.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Procedures inside Structures

Post by skywalk »

@buddymatkona
I hear you, and I think some of this would go away, if PB adopted modules in some fashion.
ColeopterusMaximus
User
User
Posts: 37
Joined: Fri Oct 29, 2010 11:29 am

Re: Procedures inside Structures

Post by ColeopterusMaximus »

+1 to this idea
Amundo
Enthusiast
Enthusiast
Posts: 191
Joined: Thu Feb 16, 2006 1:41 am
Location: New Zealand

Re: Procedures inside Structures

Post by Amundo »

+1 !!!!!!!!!!!!!!!!!

Come on Fred, Freak, anyone???? Please comment....
Win8.1, PB5.x, okayish CPU, onboard video card, fuzzy monitor (or is that my eyesight?)
"When the facts change, I change my mind" - John Maynard Keynes
ricardo
Addict
Addict
Posts: 2402
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: Procedures inside Structures

Post by ricardo »

This is very nice :)

Look this one:

Code: Select all

Prototype Suma(uno,dos)
Prototype Resta(uno,dos,this)

Structure Js
  ;Propiedades
  a.l
  b.l
  c.l
  ;Method
  Suma.Suma
  Resta.Resta
EndStructure
Procedure Suma(uno,dos)
  Result = uno + dos
  ProcedureReturn  Result
EndProcedure

Procedure Resta(uno,dos,*this.Js)
  dos = *this\a
  Result = uno - dos 
  ProcedureReturn  Result
EndProcedure

Probemos.Js

Probemos\Suma = @Suma()
Probemos\Resta = @Resta()

Debug Probemos\Suma(Probemos\a,2)

Probemos\a=2
Debug Probemos\Resta(10,2,Probemos)


Probemos\a=7
Debug Probemos\Resta(10,2,Probemos)
ARGENTINA WORLD CHAMPION
Marlin
Enthusiast
Enthusiast
Posts: 406
Joined: Sun Sep 17, 2006 1:24 pm
Location: Germany

Re: Procedures inside Structures

Post by Marlin »

I love to see my favorite thread injected with new life. :)

However there is no suggestion about Procedures inside Structures
as nice, easy, sensible, simple, ... as was posted at the beginning of this thread.

Just see the first post.
xorc1zt
Enthusiast
Enthusiast
Posts: 276
Joined: Sat Jul 09, 2011 7:57 am

Re: Procedures inside Structures

Post by xorc1zt »

interface allow oop, devs should finish the works with a less crappy way.

Code: Select all

; interface example
; xorc1zt

Interface MY_INTERFACE
    abcd( arg1.s )
    f1234( arg1.i, arg2.i )
    get()
    set( val.i )
    destroyInterface()
EndInterface

Structure MY_INTERFACESTR
    VirtualTablePointer.i
    myvar.i
EndStructure

;-----------------------------------------
;---------- INTERFACE FUNCTIONS ----------
;-----------------------------------------
Procedure debugString (*Self.MY_INTERFACESTR, str.s )
    Debug "debugString()"
    Debug str
    Debug ""
EndProcedure

Procedure debugInt (*Self.MY_INTERFACESTR, int1.i, int2.i )
    Debug "debugInt()"
    Debug int1
    Debug int2
    Debug ""
EndProcedure

Procedure.i getMyVar(*Self.MY_INTERFACESTR)
    Debug "getMyVar()"
    Debug ""
    ProcedureReturn *Self\myvar
EndProcedure

Procedure setMyVar(*Self.MY_INTERFACESTR, newvalue.i)
    Debug "setMyVar()"
    Debug ""
    *Self\myvar = newvalue
EndProcedure

Procedure destroy(*Self.MY_INTERFACESTR)
    Debug "destroy()"
    Debug "delete "+ Hex(*Self)
    Debug ""
    FreeMemory(*self)
EndProcedure

;-----------------------------------------
;-------------- Constructor -------------- 
;-----------------------------------------
Procedure.i myInterfaceConstructor()
    Define *temp.MY_INTERFACESTR
    *temp = AllocateMemory( SizeOf ( MY_INTERFACESTR ) )
    *temp\VirtualTablePointer = ?VMT
    *temp\myvar = 45
    Debug "Create interface at "+Hex(*temp)
    Debug "VMT at "+Hex(?VMT)
    Debug ""
    ProcedureReturn *temp
EndProcedure


;-----------------------------------------
;--------- Vitual Methods Table ----------
;-----------------------------------------

DataSection 
    VMT: ; THE INTERFACE MUST RESPECT THE SAME ORDER!!!
    Data.i @debugString()
    Data.i @debugInt()
    Data.i @getMyVar()
    Data.i @setMyVar()
    Data.i @destroy()
EndDataSection



;-----------------------------------------
;----------------- TEST ------------------
;-----------------------------------------

test.MY_INTERFACE = myInterfaceConstructor()

test\abcd( "HELLO" )
test\f1234( 45, 37 )
Debug "var = "+Str( test\get() )
test\set( 7777 )
Debug "var = "+Str( test\get() )
test\destroyInterface()

; test\abcd( "HELLO" ) will produce a error, the inferface is no more.
Rinzwind
Enthusiast
Enthusiast
Posts: 636
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Procedures inside Structures

Post by Rinzwind »

Yup here we are still waiting for the last few bits to be available in a compact easy to follow and maintain way. Obviously the need/wish is there for many years now. Just add the final pieces please. See first post in this topic..
Post Reply