sub/function as datatype

Forum for discussion about the documentation project.
speedfixer
Posts: 287
Joined: Nov 28, 2012 1:27
Location: California

sub/function as datatype

Postby speedfixer » Jun 26, 2018 16:09

page https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgDim contains reference to https://www.freebasic.net/wiki/wikka.php?wakka=DataType

This page has references to:

https://www.freebasic.net/wiki/wikka.php?wakka=CatPgStdDataTypes
https://www.freebasic.net/wiki/wikka.php?wakka=CatPgUserDefTypes


Data types are listed and referenced on each page. Good.

I try to follow any interesting code posted: a good way to learn other methods and how to use keywords I wouldn't normally use.
angros47 has recently posted code about using ALSA services.

His code:

Code: Select all

...
Dim Shared snd_strerror as Function ( _
Byval ecode As LONG) As Zstring Ptr
...

I have seen this kind of use of function/sub before. Any new user would have a very hard time trying to find information on how this could work.
I do use references to functions: not a problem for me.
I don't think this is a quirk.

HOWEVER, they are not listed as a datatype.

I could not use the wiki to easily find a good description of how to use 'sub' or 'function' as a type, or what the characteristics of this basic 'type' would be.
I do see the references to them as members of types/classes/UDTs. While the mechanism that allows this usage in this case is, I am sure, what allows the reference in main code, the purpose to use this in non-UDT code use would NOT be the same thing. The only 'hint' I could find that this has been true for a long time is in the THREADCREATE and THREADCALL functions - but still no clarity added there.

Did I miss something, or should there be more documentation added somewhere?


Also, I think that references to subs/functions are very powerful. Someone should write a tutorial on the possibilities this opens up for a sharp programmer. (And maybe I do not understand correctly, but couldn't this be a doorway toward polymorphism?) (It also makes genetic algorithms much simpler to write.)

david
fxm
Posts: 8282
Joined: Apr 22, 2009 12:46
Location: Paris (suburb), FRANCE

Re: sub/function as datatype

Postby fxm » Jun 26, 2018 18:16

speedfixer wrote:Did I miss something, or should there be more documentation added somewhere?

Yes, you're right.
From the Dim/Static/Var keyword pages, there is no link (direct or indirect) to specify how to declare pointers to procedures, even from Pointers/Ptr keyword pages as well.
But currently, I do not see how to fill this lack in a simple and logical way.
How doing link to the single page defining the pointers to the procedures: Pointers to Procedures ?
speedfixer
Posts: 287
Joined: Nov 28, 2012 1:27
Location: California

Re: sub/function as datatype

Postby speedfixer » Jun 26, 2018 19:26

Very good. I missed that reference page.

ANY reference toward something is better than none.

Putting the possibility of using a function as a type in the first level pages can at least start someone down the road.

With some inspiration and imagination of some of the new, young people out there, a better explanation of good ways to use it can come later.

david
fxm
Posts: 8282
Joined: Apr 22, 2009 12:46
Location: Paris (suburb), FRANCE

Re: sub/function as datatype

Postby fxm » Jun 26, 2018 20:02

First stage:
    KeyPgDim → fxm [Added link to "Pointers to procedures" page in "See also"]
    KeyPgStatic → fxm [Added link to "Pointers to procedures" page in "See also"]
    KeyPgVar → fxm [Added link to "Pointers to procedures" page in "See also"]
    CatPgStdDataTypes → fxm [Added link to "Pointers to procedures" page in "See also"]
speedfixer
Posts: 287
Joined: Nov 28, 2012 1:27
Location: California

Re: sub/function as datatype

Postby speedfixer » Jun 26, 2018 21:41

Good start. Thank you.

--------------------

Perhaps the title 'Class types' could be changed in the future to 'Complex types'.
Then any non-simple, non-basic type could be listed there, also. I have no problem with a duplicate reference in different places on the same page.

Obvious that a class is a class type.

Also, a type-assigned pointer is actually a complex type.

Proposal: under Programmers Guide, Variable and Datatypes, someone could discuss *differences* in the variable types: descriptors, contructors and destructors, RTTI, etc. Here, a mention of the variable length string could be used as an example of a complex variable: all references are by pointer, default constructor and destructor, descriptor used 'under-the-hood' to make it all work correctly, other 'goodies' that come along with this extra info about the variable/type.

Just my thoughts. An experienced programmer owns this knowlege and assumes it is obvious. As a new programmer matures, these things are learned along the way. For a beginner, these are points that produce a lot of pain and wasted time. Not every language handles them the same way, also.

david
fxm
Posts: 8282
Joined: Apr 22, 2009 12:46
Location: Paris (suburb), FRANCE

Re: sub/function as datatype

Postby fxm » Jun 27, 2018 5:47

speedfixer wrote:Perhaps the title 'Class types' could be changed in the future to 'Complex types'.
Then any non-simple, non-basic type could be listed there, also. I have no problem with a duplicate reference in different places on the same page.

Obvious that a class is a class type.

Also, a type-assigned pointer is actually a complex type.

Normally in the catalog pages (CatPg...), the full-page links are only links to keyword pages (KeyPg...), not links to programmer pages (ProPg...) except in "See also" paragraph.
speedfixer
Posts: 287
Joined: Nov 28, 2012 1:27
Location: California

Re: sub/function as datatype

Postby speedfixer » Jun 27, 2018 8:31

Perhaps.

But gfxlib and default palette would be missed by most if it were not on the PALETTE page.
Opengl and pixel formats are on the SCREEN page.

More.

If it helps someone to understand better how to use the power of FB, isn't that enough reason to include a link?
I agree, generally, a keyword page should only cover a keyword. But it should cover that keyword completely, or point to a resource that can afford the space to cover the topic more completely. For example, you can't use the STRING page to know that $kkk{x] refers to a single character in the string. This is also NOT on the string functions page. To me, this is not a small problem.

Your choice, though. We don't need to beat this up any more.
Thanks for the additional links you did add.
coderJeff
Site Admin
Posts: 2680
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: sub/function as datatype

Postby coderJeff » Jun 28, 2018 15:54

Added:
SUB pointer
FUNCTION pointer
It's a start anyway. There is a mention of procedure pointers on several keyword pages, maybe these two additional pages can be a place to concentrate the links.

Updated:
Standard Data Types
User Defined Types - added one line descriptions

I opted to add to standard data types because procedure pointers are built in to the language.

Also this page talks about using TYPE aliases for pointers to function pointers, if you want to stretch your brain more.
Pointers to procedure pointers

Yeah,
CatPg* to organize pages
KeyPg* for a specific keyword or use of keyword (some keywords have multiple uses)
ProPg* to connect several concepts or keyword uses together.


EDIT:
Here's something neat I think might not be shown anywhere, try it:

Code: Select all

function Add2( a as single, b as single ) as single
   return a + b
end function

'' automatically make z the same type as Add2 declaration
var z = procptr( Add2 )

#print typeof( procptr(Add2) )
#print typeof( @Add2 )
#print typeof( z )

print z( 1, 2 )
fxm
Posts: 8282
Joined: Apr 22, 2009 12:46
Location: Paris (suburb), FRANCE

Re: sub/function as datatype

Postby fxm » Jun 28, 2018 16:27

Well decided coderJeff, I dared not create a keyword page for each syntax (because no new keyword !).

Maybe it would be necessary to specify all the forms of syntax available for sub pointer declaration (I can do it) ?:
(dim|static) [shared] variable as Sub [CDecl|Pascal|StdCall] [( [parameter_list] )] [= initializer]
or
(dim|static) [shared] as Sub [CDecl|Pascal|StdCall] [( [parameter_list] )] variable1 [= initializer1][, variable2 [= initializer2], ...]
or
var [shared] variable = initializer

idem for function pointer declaration:
(dim|static) [shared] variable as Function [CDecl|Pascal|StdCall] [( [parameter_list] )] [ ByRef ] [As return_type] [= initializer]
or
(dim|static) [shared] as Function [CDecl|Pascal|StdCall] [( [parameter_list] )] [ ByRef ] [As return_type] variable1 [= initializer1][, variable2 [= initializer2], ...]
or
var [shared] variable = initializer

I have already added a link to "Pointers to procedures" in the three pages of keywords "Dim", "Static" and "Var".
Perhaps it should also be done for these 2 new pages.
speedfixer
Posts: 287
Joined: Nov 28, 2012 1:27
Location: California

Re: sub/function as datatype

Postby speedfixer » Jun 28, 2018 19:56

Thank you, both.
coderJeff
Site Admin
Posts: 2680
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: sub/function as datatype

Postby coderJeff » Jun 28, 2018 22:21

@fxm, I wanted to show "SUB" and "FUNCTION" as a datatype, like BYTE, or SINGLE, or POINTER. Is it important to show the storage classes to show this syntax?

Other forms:

Code: Select all

sub S
   print "yo"
end sub

'' function return data type
function F() as SUB
   return procptr( S )
end function
F()()

'' as type member
type T
   proc as SUB = procptr( S )
end type

dim x as T
x.proc()

'' as union member
union U
   proc as SUB
end union

dim y as U
y.proc = procptr( S )
y.proc()

'' as array element
dim A(0) as SUB = { procptr( S ) }

A(0)()


Also, CASTing it to another pointer type, pass it as a BYREF or BYVAL parameter, etc
fxm
Posts: 8282
Joined: Apr 22, 2009 12:46
Location: Paris (suburb), FRANCE

Re: sub/function as datatype

Postby fxm » Jun 29, 2018 5:13

OK.
Just one declaration syntax, like the other data type pages.
coderJeff
Site Admin
Posts: 2680
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: sub/function as datatype

Postby coderJeff » Jun 30, 2018 22:58

Yeah, I know what you are going for though. Something more formal to describe the syntax, and some pages have. Some pages use the "syntax" section to show the whole grammer, if possible, and the "usage" section to show something a little more user friendly. I think if it gives the user the specifics for what the keyword is about, and links to other pages that give more information, I think that's the best we can do. Of course, we can't have every page link to every other page, but should offer enough suggestions of related pages.
Tourist Trap
Posts: 2381
Joined: Jun 02, 2015 16:24

Re: sub/function as datatype

Postby Tourist Trap » Jul 17, 2018 19:04

Thanks for those new additions. I remember having been in need for this in the past. I would like to make a suggestion however. Procedure pointer seems to be best illustrated in tbe case of the callback technique. Paul Doe posted a terrific example days ago:
viewtopic.php?p=245419#p245419
Maybe a similar thing could be added in relation to this topic? Proc ptr + callback is really cool stuff. Thanks again!
fxm
Posts: 8282
Joined: Apr 22, 2009 12:46
Location: Paris (suburb), FRANCE

Re: sub/function as datatype

Postby fxm » Jul 17, 2018 19:46

I also wrote in 2011 such an example:
UDT Screen_Event_Thread (methods and event pointers)

Return to “Documentation”

Who is online

Users browsing this forum: No registered users and 1 guest