Maybe we need some new rules, because this did not seem correct to me to allow:
Code: Select all
type parent
dim as integer i
end type
type child extends parent
dim as integer j
end type
dim as parent p = type<child>(1,2)
Maybe we need some new rules, because this did not seem correct to me to allow:
Code: Select all
type parent
dim as integer i
end type
type child extends parent
dim as integer j
end type
dim as parent p = type<child>(1,2)
For sure we can discuss.
Code: Select all
type parent extends object
dim as integer i
end type
type child extends parent
dim as integer j
end type
dim as parent p = type<child>(1,2)
'' No matching overloaded function, CHILD.constructor() in 'dim as parent p = type<child>(1,2)'
Code: Select all
type parent extends object
dim as integer i
end type
type child extends parent
dim as integer j
declare constructor()
declare constructor( arg1 as integer, arg2 as integer )
end type
dim as parent p = type<child>(1,2)
Code: Select all
type parent
dim as integer i
end type
type child extends parent
dim as integer j
end type
dim as parent p = type<child>(1,2)
Code: Select all
Type Parent
Dim As Integer I
End Type
Type Child Extends Parent
Dim As Integer J
End Type
Dim As Child c
Dim As Parent p = c '' error 24: Invalid data types in 'Dim As Parent p = c'
Where this comes from is the recursive nature of initializer lists. I disallowed derived types to initialize base types, because we get really confusing results when initializing fields if it is allowed:
Code: Select all
type udt1
dim as integer i1, i2
end type
type udt2 extends udt1
dim as integer i3, i4, i5, i6
end type
type udt3 extends udt2
dim as integer i7, i8, i9
end type
dim as udt2 u2 = (1,2,3,4,5,6)
dim as udt3 u3 = type<udt3>(u2, 7, 8, 9)
'' (1) allow derived types to initialize base types
'' (2) disallow derived types to initialize base types
''
'' '' allow(1) disallow(2)
print u3.i1 '' 1 1
print u3.i2 '' 2 2
print u3.i3 '' 7 3
print u3.i4 '' 8 4
print u3.i5 '' 9 5
print u3.i6 '' 0 6
print u3.i7 '' 0 7
print u3.i8 '' 0 8
print u3.i9 '' 0 9
I will try to correct for top-level variable assignment.
Code: Select all
type udt1
dim as integer i1, i2
end type
type udt2 extends udt1
dim as integer i3, i4, i5, i6
end type
type udt3 extends udt2
dim as integer i7, i8, i9
end type
dim as udt2 u20 = (1, 2, 3, 4, 5, 6)
dim as udt3 u30 = (1, 2, 3, 4, 5, 6, 7, 8, 9)
dim as udt1 u11 = u20 '' should work like: dim as udt1 u11 = cast(udt1, u20)
dim as udt1 u12 = u30 '' should work like: dim as udt1 u12 = cast(udt1, u30)
dim as udt2 u21 = u30 '' should work like: dim as udt2 u21 = cast(udt2, u30)
print u11.i1 '' should be: 1
print u11.i2 '' should be: 2
print
print u12.i1 '' should be: 1
print u12.i2 '' should be: 2
print
print u21.i1 '' should be: 1
print u21.i2 '' should be: 2
print u21.i3 '' should be: 3
print u21.i4 '' should be: 4
print u21.i5 '' should be: 5
print u21.i6 '' should be: 6
sleep
Code: Select all
Variable Initialization
dim as T x = <INIT>
Temporary Type
type(<INIT>)
Operator New Initialization
new T(<INIT>)
For/To/Step Operator Initialization
for i as T = <INIT> to <INIT> step <INIT>
Fields Initialization
type Q
as field T = <INIT>
end type
Base Constructor
constructor T()
base( <INIT> )
end constructor
Optional Expression
[declare] sub x( arg as T = <INIT> )
Code: Select all
Type Parent
Dim As Integer I
End Type
Type Child Extends Parent
Dim As Integer J
End Type
Dim Shared As Child c0 = (1, 2)
Dim As Parent p
p = c0
Print p.I
Sleep
Code: Select all
C:\.....\FBIde0.4.6r4-FreeBASIC1.09.0.win64\FBIDETEMP.c:28:29: warning: missing braces around initializer
28 | static struct $5CHILD C0$ = { 1ll, 2ll };
| ^
| { }
Code: Select all
Type Parent
Dim As Integer I
End Type
Type Child Extends Parent
Dim As Integer J
End Type
Dim Shared As Child c0 = (1, 2)
Sub s1(Byval p As Parent)
Print p.I
End Sub
s1(c0)
Sleep
For the first part of the problem with gcc, it is related to #936 initializing type containing anonymous union can cause gcc warning Some versions of gcc won't complain about this, but some versions enable this warning by default with gcc -Wall warnings.
Code: Select all
'' fbc gcc (pseudo code)
type parent type parent
i as long i as long
end type end type
type child extends parent type child
base$ as parent
j as long j as long
end type end type
We actually have a pretty big problem here going back many versions of fbc:
Code: Select all
type parent
dim as integer i
end type
type child extends parent
dim as integer j, k, l, m, n, o, p, q
end type
sub test
dim y1 as parent = type<child>(1,2,3,4,5,6,7,8,9)
end sub
test
Code: Select all
Type Parent
Dim As Integer I
End Type
Type Child Extends Parent
Dim As Integer J
End Type
Sub s1(Byval p As Parent)
Print p.I
End Sub
Dim As Parent p0
p0.I = 1
Dim As Child c0
c0.I = 1 : c0.J = 2
s1(p0) '' printed 1: OK
s1(c0) '' printed value <> 1: NOK (with gas64)
s1(Cast(Parent, c0)) '' printed value <> 1: NOK (with gas64)
Sleep
Code: Select all
Type Parent
Dim As Integer I
End Type
Type Child Extends Parent
Dim As Integer J
End Type
Dim As Parent p0
Dim As Child c0
Dim As Parent p1 = (1) '' allowed : OK
'Dim As Parent p2 = (1, 2) '' not allowed : OK
Dim As Parent p3 = Type(1) '' allowed : OK
'Dim As Parent p4 = Type(1, 2) '' not allowed : OK
Dim As Parent p5 = Type<Parent>(1) '' allowed : OK
'Dim As Parent p6 = Type(1, 2) '' Not allowed ! OK
'Dim As Parent p7 = Type<Parent>(1, 2) '' Not allowed : OK
Dim As Parent p8 = Type<Child>(1, 2) '' alloved for fbc 1.09.0 : OK, not allowed for fbc 1.10.0 : NOK
Dim As Parent p9 = p0 '' allowed : OK
Dim As Parent p10 = c0 '' allowed : OK
Sleep
Thanks, the latest changes follow your last post for variable initializers.
Code: Select all
type parent
dim as integer i
end type
type child extends parent
dim as integer j
end type
type B
p1(0 to 0) as parent
p2 as parent
end type
dim y1 as B = type<B> _
( _ '' fbc 1.09 1.10
{ type<parent>(1) }, _ '' ok ok
type<parent>(3) _ '' ok ok
)
print y1.p1(0).i
print y1.p2.i
dim y2 as B = type<B> _
( _ '' fbc 1.09 1.10
{ type<child>(1,2) }, _ '' *bad up-cast not allowed
type<child>(3,4) _ '' *bad up-cast not allowed
)
print y2.p1(0).i
print y2.p2.i