Dim Byref syntax

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Dim Byref syntax

Post by dkl »

Hi,

recently I started some work on adding support for Dim'ing references (Type &name in C++), because I think it'd be nice to have this not just for Byref parameters, but also local variables and Type fields.

I started with this syntax idea:

Code: Select all

dim i as integer
dim byref ri as integer = i  '' ri is an "alias" for i

dim byref r1 as integer = i, byref r2 as integer = i

dim byref as integer r1 = i, r2 = i, r3 = i

var byref ri = i
What do you think about the syntax? I like it because it matches the syntax for Byref parameters, and Byref function results, and it doesn't require a new keyword...

A possible alternative that I saw suggested on this forum a while ago, by adding the Ref keyword:

Code: Select all

dim ri as integer ref = i
dim as integer ref ri = i
var ref ri = i '' maybe a little odd? I like "var byref" better
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

I prefer the syntax with which you started!
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Re: Dim Byref syntax

Post by stylin »

I like the first syntax as well, very excited to see the development of this. :)
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Dim Byref syntax

Post by VANYA »

With Byref better.
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Dim Byref syntax

Post by grindstone »

Hello,

I don't understand which effect this should have. Would

Code: Select all

dim i as integer
dim byref ri as integer = i  '' ri is an "alias" for i
be the same as

Code: Select all

dim i as integer
dim ri as integer ptr = @i
?

Greetings
grindstone
dafhi
Posts: 1641
Joined: Jun 04, 2005 9:51

Re: Dim Byref syntax

Post by dafhi »

interesting concept!

i think i'd prefer "dim as byref integer"
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

A reference can be thought of as a constant pointer with automatic dereferencing (the compiler will apply the '*' operator for you).
You must think of 'ri' as a macro for '(*p)', where 'p' is a const pointer to 'i'.

That's like if you code this:

Code: Select all

#macro DimByRef (typename, ref, obj)
  Dim As typename Const Ptr ptr##ref = @(obj)
  #Define ref (*ptr##ref)
#endmacro
#macro VarByRef (ref, obj)
  Var ptr##ref = Cptr(Typeof(obj) Const Ptr, @(obj))
  #Define ref (*ptr##ref)
#endmacro


Dim As Integer i = 123

''Dim Byref As Integer ri = i
  DimByRef(Integer, ri, i)

Print ri
ri = 456
Print i
[edit]
better equivalent code
Last edited by fxm on May 18, 2015 16:42, edited 5 times in total.
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Dim Byref syntax

Post by grindstone »

Yes, I see, the effect is that i and ri will always return the same value, no matter which of them you change. It surely works fine, but I can't see any practical benefit. In the bottom line it only means that the same variable can be accessed by two (or more) different names.
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Re: Dim Byref syntax

Post by stylin »

@grindstone Hi, it would not only alias named objects, but also things like,

dim byref i as integer = *(some_complex_pointer_math)

Other than superficial syntactic sugar, references give you a few guarantees that pointers don't:
  • You're dealing with an actual object. Pointers can be invalid.
  • You're dealing with a single object. It's not immediately clear whether a pointer points to an array or not.
Basically, you have the same practical benefits as passing by reference to a procedure.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

Two other examples (see also my previous post):

Code: Select all

#macro DimByRef (typename, ref, obj)
  Dim As typename Const Ptr ptr##ref = @(obj)
  #Define ref (*ptr##ref)
#endmacro
#macro VarByRef (ref, obj)
  Var ptr##ref = Cptr(Typeof(obj) Const Ptr, @(obj))
  #Define ref (*ptr##ref)
#endmacro


Dim As Integer I = 123

Dim As Integer CI = I
''Dim Byref As Integer RI = I
  DimByRef(Integer, RI, I)

Print I, CI, RI
CI = 456
Print I, CI, RI
RI = 789
Print I, CI, RI
Print

'------------------------------------------------------------------

''Var Byref RNI = *New Integer(1234)
  VarByRef(RNI, *New Integer(1234))

Print RNI
RNI = 5678
Print RNI
Delete @RNI

Sleep

Code: Select all

 123           123           123
 123           456           123
 789           456           789

 1234
 5678
- First example:
'CI' is a copy of 'I' ('@CI <> @I')
'RI' is a reference of 'I' ('@RI = @I')

- Second example:
'RNI' is the only reference of the "new" integer
Last edited by fxm on Mar 31, 2016 12:56, edited 5 times in total.
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Dim Byref syntax

Post by grindstone »

Thanks, I think I've got it now. But that means (if I understood it right), there can only be Dim'd a reference to a variable that already exists. In this case it's needless to mention the type, for it's already defined. I suggest the syntax should be similar to the 'SizeOf' statement, something like:

Code: Select all

Dim ri As RefTo(i)
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

Re: Dim Byref syntax

Post by marpon »

Hi
I aggree with that :
dim i as integer
dim byref ri as integer = i '' ri is an "alias" for i

be the same as

dim i as integer
dim ri as integer ptr = @i

I think in fact better with ptr ( that explain really it's a pointer), an for me (sorry if i don't understand ) it's not really needed.
Adding complexity to the compiler , is always risk for bugs , regression... , so better to select the extensions on not previously covered features, if is some bug is much more acceptable on new features than on previously existing ones.
( I have in mind the Line Input # ... that made me crazy ,until i understood it was a bug)

I would be much more interrested on addition of very efficient native string functions , such : replace , remove, split ... as discussed on previous post, it is a lot of posts around these kind of functions
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

grindstone wrote:But that means (if I understood it right), there can only be Dim'd a reference to a variable that already exists. In this case it's needless to mention the type
I think with 'Dim Byref R As DataType = ...', we will could define a compatible type reference of an object!
(as we already can do it for parameters passed by reference and for functions returning by reference, with same compatibility rules than with their pointers)
Last edited by fxm on May 21, 2015 11:26, edited 2 times in total.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

marpon wrote:I think in fact better with ptr ( that explain really it's a pointer), an for me (sorry if i don't understand ) it's not really needed.
Simplification of expression compared to pointer (avoid to use operator '@' and especially '*'):

Code: Select all

#macro DimByRef (typename, ref, obj)
  Dim As typename Const Ptr ptr##ref = @(obj)
  #Define ref (*ptr##ref)
#endmacro
#macro VarByRef (ref, obj)
  Var ptr##ref = Cptr(Typeof(obj) Const Ptr, @(obj))
  #Define ref (*ptr##ref)
#endmacro

Dim As Zstring Ptr pz = @"FreeBASIC Zstring Ptr"
Print *pz
*pz &= " 1.2.1"
Print *pz
Print

''Dim Byref As Zstring rz = "FreeBASIC Zstring Ref"
  DimByRef(Zstring, rz, "FreeBASIC Zstring Ref")
Print rz
rz &= " 1.3.0"
Print rz

Sleep

Code: Select all

FreeBASIC Zstring Ptr
FreeBASIC Zstring Ptr 1.2.1

FreeBASIC Zstring Ref
FreeBASIC Zstring Ref 1.3.0
Last edited by fxm on May 18, 2015 16:43, edited 2 times in total.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Dim Byref syntax

Post by D.J.Peters »

I wonder me that the byref stuff is on the to do list.

Some of us waiting years of much more important things.

Joshy

Code: Select all

SCREENRES w,h,d,,FB.GFX_RESIZEABLE
' or
SCREENCONTROL SET_WIN_RESIZEABLE,1

dim waiting as BOOL

class FreeBASIC extends BASIC, QBASIC
....
Post Reply