Menu

#749 For UDT with constructor, NEW does not support ANY as initializer while DIM supports this

open
nobody
None
compiler
2025-08-09
2014-11-15
No

I don't understand this restriction for using 'Any' with 'New' for an UDT with constructor (explicit or implicit), while a static instance of a such UDT (with constructor explicit or implicit) can be constructed with 'Dim ... = Any' ('Any' as fake initializer) and this last syntax works well!

Example with 'New (placement)':

Type UDT
  Declare Constructor ()
  Dim As Integer I = 1234
End Type
Constructor UDT ()
  This.I = 5678
End Constructor

Dim As UDT u = Any                      '' OK
Print u.I

'Dim As UDT Ptr pu = New(@u) UDT (Any)  '' error 7: Expected ')', found 'Any'
Dim As UDT Ptr pu = @u                  '' workaround
Print pu->I

Sleep

More generally:

If we want to keep the interest of these 4 different syntaxes:

  • Dim As datatype Ptr p = New datatype (Any)
  • Dim As datatype Ptr p = New datatype[n] {Any}
  • Dim As datatype Ptr p = New(address) datatype (Any)
  • Dim As datatype Ptr p = New(address) datatype[n] {Any}

compared respectively to the 4 followings:

  • Dim As datatype Ptr p = Allocate(Sizeof(datatype))
  • Dim As datatype Ptr p = Allocate(n*Sizeof(datatype))
  • Dim As datatype Ptr p = address
  • Dim As datatype Ptr p = address

it would require that the first 4 syntaxes work also for an UDT with constructor.
Otherwise, for easier use, perhaps completely ban 'Any' (with 'New') for all UDT.


Referring to forum, from the post:
http://www.freebasic.net/forum/viewtopic.php?p=201863#p201863

Discussion

  • fxm (freebasic.net)

    See also this other bug report:
    https://sourceforge.net/p/fbc/bugs/1013/
    1013 Non-optimization bug for 'New[]' / 'Delete[]' when datatype has no 'Constructor' (and without '{Any}' initializer)
    Both could be addressed simultaneously.

     
  • fxm (freebasic.net)

    I started the topic "Validity/Behavior of 'ANY' initializer versus datatypes with default constructor":
    https://www.freebasic.net/forum/viewtopic.php?t=33278
    to discuss and decide on the usage and behavior of 'Any' versus objects with constructors.
    Currently, the behavior of 'Any' varies greatly, depending on the data type and declaration type.
    Therefore, processing of this bug report is suspended for the time being.

     

    Last edit: fxm (freebasic.net) 2025-08-09

Log in to post a comment.

MongoDB Logo MongoDB