Curious. it does not work with literals, but it does work with a String variable. This could be s-designed, and a side effect of how the constant string literal does not exist as an actual String class instance at tis stage. But i’ll bring it up with the team for review.
this works:
Dim t As Ptr(Of String)
't = AddressOf “Some Literal String”
Dim s = “Some Literal String”
t = AddressOf s
FWIW, the same happens in C#
string s;
string* x = &s;
string* y = "text"; // E62 Type mismatch, cannot assign "String" to "String*"
Oddly tough, in mercury this copmpiles without enablig Unsafe code; in C# it doesn’t i’ll log this too.
Thanks for the response. I’m glad your team has implemented pointers in your VB framework. I created a NuGet package years ago called VBUnmanaged. I had a similar concept in mind with a Ptr class. Are delegate function pointers functional at the moment in both directions?
I was going through the pointer code some more as my focus is on native CPU features and I noticed there isn’t a straightforward manner for allocating/freeing memory in the Ptr object. A workaround to obtain the direct underlying pointer is to use a TryCast to IntPtr. However, their isn’t any ability to assign a IntPtr directly to the object, especially if you manually allocated from the heap. This should be allowed either through a constructor, along with a cast operator.
Can you give a more concrete sample lof what you want to do/what doesn’t work? You can use malloc() to allocate memory, but note that you do not want to allocate memory for objects (class instances) yourself, as GC handles that.
For instance, I want to allocate enough bytes for a data structure in which I assign to the Ptr class to dereference. This is more so for pointers that will be passed to and from external code. I wouldn’t feel the need to use this for objects anyway as it seems there is a fair amount of control for the GC given if needed. My thoughts are along the lines of creating either a constructor that could manually allocate the bytes, or you could assign another pointer directly from malloc. I haven’t fully tested it yet, but I’m guessing malloc could be returned as a Ptr(as in void*) and this could be assigned to Ptr(Of type)?
Module Program
Sub Main(args as String())
writeLn("The magic happens here.")
Dim p As Ptr(Of Foo) = rtl.malloc(sizeOf(Foo))
p.a = 5
p.b = True
writeLn($"p {p}")
writeLn($"p.a {p.a}")
writeLn($"p.b {p.b}")
End Sub
End Module
Structure Foo
Dim a As Int64
Dim b As Boolean
End Structure
Indeed.
Correct, see above sample (which I just tested and works)