Explicit contructor needed for string in UDT?

General FreeBASIC programming questions.
badidea
Posts: 1463
Joined: May 24, 2007 22:10
Location: The Netherlands

Explicit contructor needed for string in UDT?

Postby badidea » May 13, 2019 21:04

Just curious. Why is this not allowed?

Code: Select all

type font_type
   dim as string face
   dim as integer size
end type

dim as font_type f = type("arial", 18)
print f.face, f.size
Results in: testString.bas(6) error 98: No matching overloaded function, FONT_TYPE.constructor() in 'dim as font_type f = type("arial", 18)'

This does work, but why is the constructor needed?

Code: Select all

type font_type
   dim as string face
   dim as integer size
   declare constructor(fontFace as string, fontSize as integer)
end type

constructor font_type(fontFace as string, fontSize as integer)
   face = fontFace
   size = fontSize
end constructor

dim as font_type f = type("arial", 18)
print f.face, f.size
Tourist Trap
Posts: 2762
Joined: Jun 02, 2015 16:24

Re: Explicit contructor needed for string in UDT?

Postby Tourist Trap » May 14, 2019 2:54

badidea wrote:Just curious. Why is this not allowed?

Code: Select all

type font_type
   dim as string face
   dim as integer size
end type

dim as font_type f = type("arial", 18)
print f.face, f.size
Results in: testString.bas(6) error 98

Hi badidea,

From what I think I understood about this, it's because assignment TYPE<...>(...) is meant to be fast and simply fill the fields without looking deep in the intricacy of the data sub-structure.
Here the string stores its descriptor (size=12) in the UDT. And you would have to read it before you can write to the corresponding data section. Otherwise, the size and even the position in memory is not known. Too slow for the straightforward Type(...) syntax if we want to keep it faster than a constructed stuff.
It's at least my interpretation.

You can use string*'fixed_size', it would work.
fxm
Posts: 9129
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Explicit contructor needed for string in UDT?

Postby fxm » May 14, 2019 5:14

A var-len string is a pseudo-object which has its own (hidden) default constructor.
So an UDT containing such a string also has its own (hidden) default constructor.
Referring to the 'Type (Temporary)' documentation page, when an UDT has any constructor, 'Type(parameters)' is allowed only if a constructor matching with 'parameters' also exists.

Other examples very simple:

Code: Select all

Type UDT
  Dim As Integer I  '' no constructor
End Type

Dim As UDT u = Type(123)  '' works

Code: Select all

Type UDT
  Dim As Integer I = 1  '' initialyser with constant value => (hidden) default constructor
End Type

Dim As UDT u = Type(123)  '' does not work
Dim As UDT u1 = Type()    '' works (a constructor matches)

Code: Select all

Type UDT
  Dim As Integer I = 1  '' initialyser with constant value => (hidden) default constructor
  Declare Constructor (Byval As Integer)
End Type
Constructor UDT (Byval i0 As Integer)  '' explicit constructor (with one integer parameter)
  This.I = i0
End Constructor

Dim As UDT u = Type(123)  '' works now (the explicit constructor matches)
Dim As UDT u1 = Type()    '' no longer works (explicit constructor(s) cancel(s) all implicit constructor(s))
badidea
Posts: 1463
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Explicit contructor needed for string in UDT?

Postby badidea » May 26, 2019 22:24

fxm wrote:A var-len string is a pseudo-object which has its own (hidden) default constructor.

Which it needs for memory allocation I suppose?
I ran into this again today. A bit annoying.
Anyway, thanks for the explanation. I think I understand the issue :-)

Return to “General”

Who is online

Users browsing this forum: No registered users and 6 guests