Discussions related to Visual Prolog
Harrison Pratt
VIP Member
Posts: 462 Joined: 5 Nov 2000 0:01
Post
by Harrison Pratt » 10 Aug 2025 14:51
The code below causes a fatal error in the last line of clause:
Code: Select all
class predicates
colorToBinary : ( gdiplus_native:: color Color ) -> binary Binary . % a 4 byte binary within custom color binary
clauses
colorToBinary( Color ) = Binary :-
R = Color : gdiplus :: redPart ( ) , % unsigned8
G = Color : gdiplus :: greenPart ( ) ,
B = Color : gdiplus :: bluePart ( ) ,
A = Color : gdiplus :: alphaPart ( ) ,
Binary = binary:: createAtomic ( 4 ) ,
binary:: setBinary ( Binary , 0 , $[ R ] ) ,
binary:: setBinary ( Binary , 1 , $[ G ] ) ,
binary:: setBinary ( Binary , 2 , $[ B ] ) ,
binary:: setBinary ( Binary , 3 , $[ A ] ) . % fatal error c098 : Illegal binary element 'A'
But if I change the last few lines to this, it compiles without complaint!
Code: Select all
...
binary:: setBinary ( Binary , 2 , $[ B ] ) ,
binary:: setBinary ( Binary , 3 , $[ A ] ) ,
succeed.
Am I not understanding something basic or is it a compiler quirk?
Thomas Linder Puls
VIP Member
Posts: 1473 Joined: 28 Feb 2000 0:01
Post
by Thomas Linder Puls » 12 Aug 2025 8:34
It is clearly a bug in the compiler.
Regarding the code. It "nags" me that you create binaries (e.g.
$[R] ) just to set an
unsigned8 in a binary.
I suggest you use
setIndexed_unsigned8 this instead:
Code: Select all
Binary = binary:: createAtomic ( 4 ) ,
binary:: setIndexed_unsigned8 ( Binary , 0 , R ) ,
binary:: setIndexed_unsigned8 ( Binary , 1 , G ) ,
binary:: setIndexed_unsigned8 ( Binary , 2 , B ) ,
binary:: setIndexed_unsigned8 ( Binary , 3 , A ) .
Or perhaps (a matter of taste):
Code: Select all
Color2 = A << 24 ++ B << 16 ++ G << 8 ++ R , % 0x_AA_BB_GG_RR
Binary = binary:: createAtomic ( 4 ) ,
binary:: setIndexed_unsigned ( Binary , 0 , Color2 ) .
Regards Thomas Linder Puls
PDC
Thomas Linder Puls
VIP Member
Posts: 1473 Joined: 28 Feb 2000 0:01
Post
by Thomas Linder Puls » 12 Aug 2025 8:45
By the way if you need to create (larger) binaries in a specific format then I can suggest this approach:
Code: Select all
S = outputStream_binary:: new ( ) ,
S : write ( hasDomain( unsigned8, R ) ) ,
S : write ( hasDomain( unsigned8, G ) ) ,
S : write ( hasDomain( unsigned8, B ) ) ,
S : write ( hasDomain( unsigned8, A ) ) ,
Binary = S : getBinary ( ) .
The usage of
hasDomain is not necessary here, because the values have the correct type. But in more complex cases it is a good safety precaution to explicitly state the type you want to write.
Regards Thomas Linder Puls
PDC
Harrison Pratt
VIP Member
Posts: 462 Joined: 5 Nov 2000 0:01
Post
by Harrison Pratt » 12 Aug 2025 11:37
Thank you very much for the tips on working with binary structures, Thomas!
I have not worked with binaries before ... and it shows.