Dim Byref syntax

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

In C++, reference usage is limited (no reassignment, no pointer to a reference, no array of reference, ...), which probably will not be the case in FreeBASIC even at final stage, what is a good thing (leaving the user a greater number of cases for choice between references and pointers).

But in order to safe more the reassignment of references (compared to pointers assignment), could we apply similar controls on the reference reassignment than on the initializing of reference at the declaration level?.
For example, the last two lines could they be disallowed:

Code: Select all

Type Parent Extends Object
End Type
Type Child Extends Parent
End Type

Dim p As Parent, c As Child
Dim Byref As Parent rp = c

rp = p             '' OK
rp = c             '' OK
rp = Parent()      '' OK
rp = Child()       '' OK

@rp = @p           '' OK
@rp = @c           '' OK
@rp = New Child()  '' OK
Delete @rp         '' OK
@rp = @Parent()    '' NOK
@rp = @Child()     '' NOK
(obviously similar controls for reassignment of reference passed to a procedure)
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

By cons, leave the possibility of declaring an "empty" reference 'r'
Dim Byref As datatype r = *Cptr(datatype Ptr, 0)
or reassign it as an "empty" reference
@r = 0
can be interesting, because similarly to a pointer 'p' which can be "null" to signify that it points to nothing
if (p <> 0) Then ...
a reference could be "empty" to signify that it refers to nothing
if (@r <> 0) Then ...

Same possibility of syntaxes when a reference is returned from a function
@r = 0 : Return r
or
Return *Cptr(datatype Ptr, 0)
or
Return Byval 0
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

fxm wrote:By cons, leave the possibility of declaring an "empty" reference 'r'
Dim Byref As datatype r = *Cptr(datatype Ptr, 0)
or reassign it as an "empty" reference
@r = 0
can be interesting, because similarly to a pointer 'p' which can be "null" to signify that it points to nothing
if (p <> 0) Then ...
a reference could be "empty" to signify that it refers to nothing
if (@r <> 0) Then ...
.....
Following my previous post, I think that define an initializer should not be mandatory when a reference is declared (similarly to pointer declaration):
  • Because in FB (at opposite of C++), a reference can successively refer to different objects and even to an "empty" object (associated internal pointer = 0 in the reference implementation) as it is reminded above.
So if an initializer becomes optional when declaring a reference, there would be no barrier to also declare a var-len array of references (presently, initializer prohibited within a var-len array declaration).
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

No echo (from admin or someone else) on my proposal in the above post?
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

fxm wrote:No echo (from admin or someone else) on my proposal in the above post?
If that other possibility of "empty" reference is allowed (internal associated pointer is null), it would be better that compiler adds here also code for null-pointer checking when option '-exx' is requested.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Dim Byref syntax

Post by Tourist Trap »

fxm wrote:
fxm wrote:No echo (from admin or someone else) on my proposal in the above post?
I think this is all right that initializer is just a temporary requirement, and including ref to arrays would be pretty nice (as well as an array built-in comparison function, array1=array2 but it's another subject).

I've probably already mentioned that but "var byref" would be also very interesting.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

Is the 'Dimbyref' development is now stopped in its present capability?
I hoped a wider usage for this new feature, as discussed in the previous posts.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

fxm wrote:Is the 'Dimbyref' development is now stopped in its present capability?
I hoped a wider usage for this new feature, as discussed in the previous posts.
IMHO, the following capacities to be maintained and to be added in the future upgrades:

- Add the arrays of references (useful for a collection of compatible objects):
Dim Byref As datatype ra(...) = {...}

- Keep the present capacity to define that I name "empty references" (with associated internal null pointers):
Dim Byref As datatype ref = *Cast(datatype Ptr, 0)
or:
@ref = 0
or:
Return *Cptr(datatype Ptr, 0)
or:
Return Byval 0

- Before usage, the case "empty reference" can be checked by:
If @ref <> 0 Then .....

- Extend that capacity of "empty references" by means of a shorter definition without initializer, useful for big or un-sized reference arrays (with associated internal null pointers):
Dim Byref As datatype ref
Dim Byref As datadype ref(...)

- Adds the null-pointer checking (on the internal pointer) when compiling with -exx option.

- Authorise the implict conversions between references of compatible pointeur types:
Dim Byref As Integer Ptr ref = Cast(Any Ptr, 0)
or:
Dim Byref As Any Ptr ref = Cast(Integer Ptr, 0)

- Add a new specific operator, for example '{}' (reference index) to return a reference to a value some distance in memory from a base address (shortness syntax):
ref{i} = (@ref)
example:
Dim Byref As Integer ref = *New Integer[10]
Print ref{5} '' equivalent to Print (@ref)[5]
Delete @ref
Last edited by fxm on May 16, 2018 14:44, edited 2 times in total.
bat
Posts: 1
Joined: Dec 29, 2015 10:38

Re: Dim Byref syntax

Post by bat »

Null References: The Billion Dollar Mistake

"I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years", says Mr. Hoare
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Dim Byref syntax

Post by Tourist Trap »

fxm wrote: - Add a new specific operator, for example '{}' (reference index) to return a reference to a value some distance in memory from a base address (shortness syntax):
ref{i} = (@ref)
example:
Dim Byref As Integer ref = *New Integer[10]
Print ref{5} '' equivalent to Print (@ref)[5]
Delete @ref

Hi fxm!
Very nice proposals I think. However I wouldn't use curl brackets here personnally. This syntax could be rather precious to extend some day the behaviour of constant array notation: {a, b, c...}. So this would interfer.

Why not keeping the square brackets since they are already dedicated to pointers? It's even possible to think of double square brackets: ref[]. It's not that uggly and has the merit of keeping consistent with the pointers.

Other possibility: ref@, where @ would take its old usage in basic which mean "at given location". Maybe with this meaning in head, it's not so confusing.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

Only as example, I have chosen that symbol '{}'not used today for indexation (different from '()' and '[]') to avoid any confusion in the definition formula:
ref{i} = (@ref)
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Dim Byref syntax

Post by Tourist Trap »

fxm wrote:Only as example, I have chosen that symbol '{}'not used today for indexation (different from '()' and '[]') to avoid any confusion in the definition formula:
ref{i} = (@ref)

I'm proposing to leave appart the curls (for thy seems in my eyes belonging to mathematical sets world) and to do this in counterpart:
ref[] = (@ref)
or,
ref@ = (@ref)
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

This proposed new operator to be applied on a reference is rather a pure offset than any dereferencing, because for example:
ref{0} = ref

Perhaps by surrounding the offset between the two inequality symbols '<>':
ref<i> = (@ref) '' or ref<i> = *(@ref + i)
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Dim Byref syntax

Post by Tourist Trap »

fxm wrote:This proposed new operator to be applied on a reference is rather a pure offset than any dereferencing, because for example:
ref{0} = ref

Perhaps by surrounding the offset between the two inequality symbols '<>':
ref<i> = (@ref) '' or ref<i> = *(@ref + i)

Why not overloading defining a function a macro in the style of OFFSETOF() then? As for instance, REFATOFFSET(ref, offset_index).
WQ1980
Posts: 48
Joined: Sep 25, 2015 12:04
Location: Russia

Re: Dim Byref syntax

Post by WQ1980 »

FreeBASIC-1.05.0

Code: Select all

Type Nod
	As String sect
	As String key
	As Integer tp
End Type

Type ini
	As Nod a(30)
End Type

ReDim Shared aIn(10) As ini
Dim Shared ByRef aU_SeN As ini = aIn(0)

aU_SeN.a(0).sect = "0000000000000000000000"

?aU_SeN.a(0).sect

Sleep
Error "Aborting due to runtime error 12 ("segmentation violation" signal)"

Code: Select all

Dim Shared aIn(10) As ini
-> is no error
Post Reply