Code: Select all
type mytype
declare operator cast as single
dim as long dummy
end type
operator mytype.cast as single
return rnd
end operator
dim as mytype my_t
for i as long = 1 to 9
? cint(my_t)
next
Code: Select all
type mytype
declare operator cast as single
dim as long dummy
end type
operator mytype.cast as single
return rnd
end operator
dim as mytype my_t
for i as long = 1 to 9
? cint(my_t)
next
dafhi wrote: ↑Mar 09, 2022 7:35 bug?Code: Select all
type mytype declare operator cast as single dim as long dummy end type operator mytype.cast as single return rnd end operator dim as mytype my_t for i as long = 1 to 9 ? cint(my_t) next
Code: Select all
0.3300996
0.3290385
0.5324828
0.759945
0.6424803
0.6527108
0.2956116
0.9701146
0.9696021
Code: Select all
type mytype
declare operator cast as single
dim as long dummy
end type
operator mytype.cast as single
dim as double d = rnd
? cast(single, d),
return d
end operator
dim as mytype my_t
for i as long = 1 to 9
? cint(my_t)
next
Code: Select all
type mytype
declare operator cast as single
dim as long dummy
end type
operator mytype.cast as single
return rnd
end operator
dim as mytype my_t
for i as long = 1 to 9
? cint(cast(single,my_t))
next
Code: Select all
type mytype
declare operator cast as string
dim as long dummy
end type
operator mytype.cast as string
return str(2*rnd)
end operator
dim as mytype my_t
for i as long = 1 to 9
? cint(my_t) '' NOK
'? cint(cast(string, my_t)) '' OK
next
No it is not a BUG, Print (?) Shows what is specified and CINT () even if it does the conversion is ignored by PRINT, to force the display of the converted content it is necessary to keep the data in a variable of the requested type.fxm wrote: ↑Mar 09, 2022 10:35 In case of conversion to string by overloading the cast operator, an error is well provided for the automatic (non explicit) conversion:Code: Select all
type mytype declare operator cast as string dim as long dummy end type operator mytype.cast as string return str(2*rnd) end operator dim as mytype my_t for i as long = 1 to 9 ? cint(my_t) '' NOK '? cint(cast(string, my_t)) '' OK next
- C:\...\FBIde0.4.6r4_fbc1.10.0\FBIDETEMP.bas(14) error 20: Type mismatch, before ')' in '? cint(my_t)'
Code: Select all
dim as mytype my_t
dim as integer x
for i as long = 1 to 9
x = cint(my_t)
print x
next
I do not agree.
Well then we have to see if it's a known bug or if we need to report it.fxm wrote: ↑Mar 09, 2022 12:05I do not agree.
cint is declared to always return an integer.
By adding 'dim as integer x:x=.....', you add a final implicit conversion to the closest integer.
Same bug (IMHO) for all conversion keywords to integral types and floating point types, from an overloaded cast operator member applying on a numeric type.