Jump to content

Struct Notation


Recommended Posts

Hi all,

I'll happily be shot down on this, but would it be worth bringing dot notation for structs back into the fold officially?
TBH I'm not particularly married to dot notation specifically - but if its there, working, and all over the forum it might be worth revisiting.

I mean there's probably little incentive to do this the correct way:

SomeFunc($hSomeHandle, _
    DllStructGetData($tRect, "Left"), _
    DllStructGetData($tRect, "Top"), _
    DllStructGetData($tRect, "Right"), _
    DllStructGetData($tRect, "Bottom"))

When this works perfectly fine:

SomeFunc($hSomeHandle, $tRect.Left, $tRect.Top, $tRect.Right, $tRect.Bottom)

Also (more of a pain point for me), it would be amazing to have a shorthand for DllStructGetPtr and DllStructGetSize.  So maybe something like $tStruct.ptr if we're to continue on a theme? I feel I'm forever declaring one-off $pStruct and $iStructLen variables purely for legibility.

So e.g. this

;Create Buffer
$tBuffer = DllStructCreate(StringFormat("byte[%d]", $iSize))

;create struct for a DllCall which references the buffer
$tMidiHdr = DllStructCreate($tag_midihdr)
$pMidiHdr = DllStructGetPtr($tMidiHdr) ;need to pass this to DllCall later!

;Set members
DllStructSetData($tMidiHdr, "lpData", DllStructGetPtr($tBuffer))
DllStructSetData($tMidiHdr, "dwBufferLength", DllStructGetSize($tBuffer))

could be expressed as:

$tBuffer = DllStructCreate(StringFormat("byte[%d]", $iSize))
$tMidiHdr = DllStructCreate($tag_midihdr)

$tMidiHdr.lpData = $tBuffer.ptr
$tMidiHdr.dwBufferLength = $tBuffer.size

Just improves legibility, and is like 100x faster to code when you're doing a lot of it.

Anyway, thats my thoughts FWIW. Probably just getting more irritable as I get older!

Link to comment
Share on other sites

I am using a lot of the dot notation on structures.  It is a bit slower than its function counterpart, but if it not use within a large loop, the difference is insignificant.

I do not agree with your suggestion of .ptr and .size cause it will interfere with some structure declaration like this one (making it script breaking) :

Local $tStruct = DllStructCreate("int size;long_ptr ptr")

However I do think the priority should be to fix the following bug :

Local $tStruct = DllStructCreate("byte data[10]")
Local $i = 5
$tStruct.data(5) = 1 ; working
ConsoleWrite($tStruct.data(5) & @CRLF)
$tStruct.data($i) = 2 ; not working == dangerous bug
ConsoleWrite($tStruct.data(5) & @CRLF)
$tStruct.data(($i)) = 3 ; working
ConsoleWrite($tStruct.data(5) & @CRLF)

 

Link to comment
Share on other sites

Posted (edited)
3 hours ago, Nine said:

I do not agree with your suggestion of .ptr and .size cause it will interfere...

sure, I'm not dying on this hill - but a shorthand any form was more what I was driving at.

yep, agree that the array bug would be the first thing to fix.
It's technically not a feature though hey? - I'd imagine a decision would first need to be made to pursue this at some level.

Edited by MattyD
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...