Hi Brad,

I don't know why you are getting an error.

But I think that this is one case where you should be writing `BUILD` instead of `TWEAK`.
Note though that you can only have one `BUILD` or `TWEAK` per class.
A quick test showed to me that it doesn't matter if I use BUILD or TWEAK. Besides this, I tried this construct before without the TWEAK or BUILD submethods and the assignment generated the same errors.

To answer your question below I just want to have an assignment to another typed variable which works for all variables but not the CStruct variable. Like below;

my cairo_path_data_point_t $p1 = $another-path-data-point;

I must also mention that it becomes awkward when such a variable is provided as an argument to some method. Binding will generate also such message.

In the mean time I made a small program which didn't generate errors, see below
---
use v6;
use NativeCall;

class cairo_path_data_point_t is repr('CStruct') is export {
  has num64 $.x;
  has num64 $.y;
}

my cairo_path_data_point_t $d1 .= new( :x(1e0), :y(2e0));
my cairo_path_data_point_t $d2 = $d1;

note "d2: ", $d2.perl;            # d2: cairo_path_data_point_t.new(x => 1e0, y => 2e0)
---

and this is what I want.... I now need a Crystal Ball to find me the answer of the problems I ran into :-\

Well, at least the error is weird to warn me that I cannot assign a variable to another of the same type.

Regards,
Marcel

---

Or maybe you want a multi method `new` instead?

    multi method new ( :$native-object! ) {
        samewith( x => $native-object.x, y => $native-object.y )
    }

Assuming that `x` and `y` are actually public attributes you could pull them out of the object in the signature.

    multi method new ( :$native-object! (:$x, :$y) ) {
        samewith( :$x, :$y )
    }

If you need to throw out other public attributes, add `*%`

    multi method new ( :$native-object! (:$x, :$y, *%) ) {
        samewith( :$x, :$y )
    }

---

Honestly I do not understand why you are passing in an already constructed object, and I don't know anything about it either.

If you gave us answers for those two questions, we may be able to help you better.


On Sat, Jun 27, 2020 at 10:56 AM Marcel Timmerman <mt1...@gmail.com <mailto:mt1...@gmail.com>> wrote:

    Hi,

    I am getting an error and don't know why it happens, it might even
    be a
    bug. It is about an assignment to a CStruct variable.

    The structure is defined like;

    class cairo_path_data_point_t is repr('CStruct') is export {
       has num64 $.x;
       has num64 $.y;

       submethod TWEAK ( :$native-object ) {
         $!x = $native-object.x;
         $!y = $native-object.y;
       }
    }


    The error is generated when typed variables are used (below, $x is
    also
    a cairo_path_data_point_t);

    my cairo_path_data_point_t $p1 = $x;

    or

    my cairo_path_data_point_t $p1 =
    cairo_path_data_point_t.new(:native-object($x));

    but not with

    my cairo_path_data_point_t $p1 .= new(:native-object($x));

    or

    my $p1 = $x;

    After which all fields in the structure are happely accessable
    using $p1!


    The error is

    Type check failed in assignment to $p1; expected
    cairo_path_data_point_t
    but got cairo_path_data_point_t.new(x => 0e0, y => 0e0)


    Raku version: 2020.06-7-gf1960baa9 built on MoarVM version
    2020.06-6-gbf6af07de
    implementing Raku 6.d.

    The content of the structure does not matter, I've seen it with other
    structures too.

    Regards,
    Marcel


Reply via email to