Re: pointer confusion

2022-12-05 Thread Ralph Mellor
On Mon, Dec 5, 2022 at 10:20 PM ToddAndMargo via perl6-users
 wrote:
>
> > use NativeCall;
> > my Pointer $foo .= new: 42;
> > say $foo;   # NativeCall::Types::Pointer<0x2a>
> > print $foo; # NativeCall::Types::Pointer<5895604297984>



`say` concatenates the `.gist`s of each of its arguments. In this case
`say` only has one argument, `$foo`, which contains a `Pointer`.

The `.gist` of a `Pointer` is a concatenation of:

* The full type name (`NativeCall::Types::Pointer`)

and

* `<...>` enclosing the pointer's value (the address it represents)
  in HEXADECIMAL literal form.



`print` concatenates the `.Str`s of each of its arguments. In this case
`print` only has one argument, again `$foo` which contains a `Pointer`.

The `.Str` of a `Pointer` is a concatenation of:

* The full type name (`NativeCall::Types::Pointer`) as with `say`.

and:

* `<...>` enclosing some OTHER number (NOT the pointer's value,
  ie not the address the `Pointer` represents) in DECIMAL literal form.

I think this OTHER number is a number associated with the `ObjAt` of
the `Pointer`.

`ObjAt` is a high level Raku Object ID that's unique to the associated object.

See https://docs.raku.org/type/ObjAt

The key thing is that the integer number included in the `.Str` of a `Pointer`
is NOT the address that that `Pointer` points to. It's something completely
unrelated. Unless you know you need to pay attention to its `ObjAt` you
are best advised to ignore it.



To return to the outputs of the `say`/`.gist` and `print`/`put`/.Str` forms
and add the `.raku` at the start:

```
...Pointer.new(42)
...Pointer<0x2a>
...Pointer<5895604297984>
```
You thought the three numbers should be the same integer.

That's part of the problem. It's a surmise that leads to surprise.



Your original exploration was in the REPL.

By default this REPL line and display uses `.gist`/`say`
```
[3] >  my Pointer $Ptr2Ptr  = NativeCall::Types::Pointer[BYTES].new($x);
NativeCall::Types::Pointer<0xfe45ddcc>
```
(You can change from the default but let's move on.)

If a REPL line explicitly writes to STDOUT, then the REPL uses
that instead. So this line does NOT use `.gist`/`say`:

```
[4] > print "x = <" ~ $x ~ "> <0x" ~ $x.base(16) ~ ">   Ptr2Ptr <" ~
$Ptr2Ptr ~ ">\n";
x = <4265991628> <0xFE45DDCC>   Ptr2Ptr
>
```
You wrote `print`, and, as explained at the start, if you do that, the
REPL accepts its output and displays that as is, no `.gist` involved.

In the meantime, the `$Ptr2Ptr` is concatenated with `...<" ` and `">..."
either side of it via two `~`s. The `~` op calls `.Str` on its operands.

As explained above the `.Str` of a `Pointer` is not its `.gist` and instead
returns a string that includes an `ObjAt` integer inside the `<...>` and
that integer is not the address the `Pointer` points to.



So, factors that are confusing.

Please sleep on what you've learned, and perhaps rethink it through
the next day before deciding whether you now find it makes sense or
if it remains confusing. Then I'll follow up if you still find it confusing.

Hth!

--
raiph


Re: pointer confusion

2022-12-05 Thread ToddAndMargo via perl6-users

On 12/5/22 09:25, Ralph Mellor wrote:

On Sat, Dec 3, 2022 at 11:44 AM ToddAndMargo via perl6-users
 wrote:


I am confused


I think the following is a golf of your confusion:
```
use NativeCall;
my Pointer $foo .= new: 42;
say $foo;   # NativeCall::Types::Pointer<0x2a>
print $foo; # NativeCall::Types::Pointer<5895604297984>
```
I can see how someone might be confused by the two numbers:

* Decimal `42` which is the same as hex `0x2a`.

* Decimal `5895604297984` (or whatever) which isn't `42`/`0x2a`.

Why isn't the third number the same as the first two?

Am I right in thinking this distills what is confusing to you?

--
raiph

Yes


Re: Session ID

2022-12-05 Thread ToddAndMargo via perl6-users

On 12/5/22 09:17, Ralph Mellor wrote:

On Mon, Dec 5, 2022 at 9:45 AM ToddAndMargo via perl6-users
 wrote:


Answer 3:
https://stackoverflow.com/questions/74665162/how-do-i-assign-the-value-in-carray-that-contains-a-memory-address-to-a-poi#74674303

Håkon Hægland is astonishing good at this stuff.


Indeed he is!

If one of his answers is acceptable to you, please click its Accept button.

Note that your above link is to his second answer, but it seems like it's
his third answer that you found most satisfying. If that's right, then the
best answer to Accept imo is the third one.

--
raiph



Clicked!


Re: When to use .new?

2022-12-05 Thread Ralph Mellor
I forgot to mention one other shortcut that is always available
if you do have to use `.new` (which is the case for most types).

You can write:
```
my $foo = 42;
```
The `42` on the RHS of the `=` is the shortest way to create
an integer value corresponding to `42`.

But you could also write:
```
my $foo = Int.new: 42;
```
It's odd, but you could that. You could also write:

```
my Int $foo .= new: 42;
```
Note the `.=` instead of `=` and the `new` instead of `Int.new`.

That is, if you constrain a variable's type, and want to initialize
that variable to a value of exactly that type, you don't have to
repeat the type name on the RHS of the `=` if you write it as above.

For types that have literal forms it's not helpful. And for short type
names it's helpful, but only a little. But let's say you really wanted
to write out the full `NativeCall::Types::Pointer` and you wanted a
variable constrained to that type and initialized to have a value of
that type. Now it's a big deal:

```
my NativeCall::Types::Pointer $foo .= new: 42;
```

--
raiph


Re: When to use .new?

2022-12-05 Thread Ralph Mellor
On Thu, Dec 1, 2022 at 4:28 AM ToddAndMargo via perl6-users
 wrote:
>
> Why can I get away with `my Str $x = "";`
>
> But I have to use .new here (an other places too) `my $ppSession = 
> NativeCall::Types::Pointer.new();`

There are ways to write the value of some data types with minimum fuss.

This is typically the case for oft used types like strings and numbers.

Many programming languages have "literals" as the ultimate no fuss way.

Raku has them for strings and numbers:

```
"this is a string"
42 # <-- `42` is a number, an integer
```

You create a string by just writing its value inside quote marks.

You create a number by just writing its value.

The `.new` is implicit, happening behind the scenes.



It's not practical to have "literal" short cuts for *all* data types.

While code corresponding to pointers (and dereferences of them)
is *explicitly* written a LOT in C, explicit pointer coding is almost
completely eliminated in Raku.

The only exception is in some NativeCall code that makes a lot of
use of its `Pointer` type for representing a Raku wrapped C Pointer.

Is that enough to justify introducing a "literal" form for `Pointer`s?

I doubt it. But if you wanted to explore that, Raku makes it possible.

> Is there some rule I can follow that let me know when I have to
> use `.new and when I do not?

About the only rule there can be for arbitrary Raku code is whether
you already know a way to create a value without using `.new`, or
have time available to investigate the two approaches. For builtin
types that means reading the official Raku doc. For user defined
types defined in modules you use that means reading the doc of
those modules.

But in general the vast majority of types require use of `.new`.

The main exceptions I can think of are strings, numbers, pairs,
and basic collections. (Untyped lists, captures, positional arrays,
and associative arrays -- hashes etc.)

> (I am getting tired of figuring it out the hard way.)

I found it easy because I didn't *try* to find out but was just
delighted each time I realized there was yet another nice and
even shorter way to create values.

One thing I note in this context here is that one doesn't have to
write out the fully qualified type name `NativeCall::Types::Pointer`.

One can just write `Pointer`.

(NativeCall introduces this short form by default if you write `use
NativeCall;`)

--
raiph


Re: pointer confusion

2022-12-05 Thread Ralph Mellor
On Sat, Dec 3, 2022 at 11:44 AM ToddAndMargo via perl6-users
 wrote:
>
> I am confused

I think the following is a golf of your confusion:
```
use NativeCall;
my Pointer $foo .= new: 42;
say $foo;   # NativeCall::Types::Pointer<0x2a>
print $foo; # NativeCall::Types::Pointer<5895604297984>
```
I can see how someone might be confused by the two numbers:

* Decimal `42` which is the same as hex `0x2a`.

* Decimal `5895604297984` (or whatever) which isn't `42`/`0x2a`.

Why isn't the third number the same as the first two?

Am I right in thinking this distills what is confusing to you?

--
raiph


Re: Session ID

2022-12-05 Thread Ralph Mellor
On Mon, Dec 5, 2022 at 9:45 AM ToddAndMargo via perl6-users
 wrote:
>
> Answer 3:
> https://stackoverflow.com/questions/74665162/how-do-i-assign-the-value-in-carray-that-contains-a-memory-address-to-a-poi#74674303
>
> Håkon Hægland is astonishing good at this stuff.

Indeed he is!

If one of his answers is acceptable to you, please click its Accept button.

Note that your above link is to his second answer, but it seems like it's
his third answer that you found most satisfying. If that's right, then the
best answer to Accept imo is the third one.

--
raiph


Re: Session ID

2022-12-05 Thread ToddAndMargo via perl6-users

On 11/14/22 12:54, ToddAndMargo via perl6-users wrote:

Hi All,

Windows 11 22H2

Is there a way to find session ID of the
current running program?  Any predefined
system variable for that?

Many thanks,
-T



Answer 3: 
https://stackoverflow.com/questions/74665162/how-do-i-assign-the-value-in-carray-that-contains-a-memory-address-to-a-poi#74674303


Håkon Hægland is astonishing good at this stuff.