[go-nuts] Re: cgo Compiling C files housed in different directory than Go files

2023-04-15 Thread Jason E. Aten
perhaps take a look at how the Go bindings for Lua 
work. https://github.com/aarzilli/golua

On Friday, April 14, 2023 at 6:14:05 PM UTC+1 Palash B wrote:

> Hello folks.
>
> I am working on writing an Interpreter (for my own programming language) 
> which is written in C and I want to build a Go API for my interpreter.
>
> My current directory structure is something like this
> ...
>  cpank/*.c 
> cpank/stdlib/*.c 
> cpank/ext/*.c 
> cpank/ext/*.h 
> cpank/include/*.h 
> goapi/api.go 
> ... 
>
> cpank/*.c contains the core files, stdlib contains source for standard 
> library, ext curently contains 2 files xxhash.c and xxhash.h but can and 
> will contain more files later.
>
> I have tried putting the go file in main cpank directory but it fails. If 
> I directly include the c files, cgo throws duplicate errors. Only thing 
> that works is copying all c files from cpank, stdlib, ext and the include 
> directory into the goapi directory.
>
> Is there any way to tell cgo to compile this, this and that files like we 
> use with normal c projects.
>
> How do I make this work?
>
> Any help or suggestions will be appreciated.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/4575c8e0-a198-4521-aa02-625999204016n%40googlegroups.com.


[go-nuts] Re: cgo Compiling C files housed in different directory than Go files

2023-04-15 Thread Jason E. Aten
and https://github.com/stevedonovan/luar

On Saturday, April 15, 2023 at 9:13:03 AM UTC+1 Jason E. Aten wrote:

> perhaps take a look at how the Go bindings for Lua work. 
> https://github.com/aarzilli/golua
>
> On Friday, April 14, 2023 at 6:14:05 PM UTC+1 Palash B wrote:
>
>> Hello folks.
>>
>> I am working on writing an Interpreter (for my own programming language) 
>> which is written in C and I want to build a Go API for my interpreter.
>>
>> My current directory structure is something like this
>> ...
>>  cpank/*.c 
>> cpank/stdlib/*.c 
>> cpank/ext/*.c 
>> cpank/ext/*.h 
>> cpank/include/*.h 
>> goapi/api.go 
>> ... 
>>
>> cpank/*.c contains the core files, stdlib contains source for standard 
>> library, ext curently contains 2 files xxhash.c and xxhash.h but can and 
>> will contain more files later.
>>
>> I have tried putting the go file in main cpank directory but it fails. 
>> If I directly include the c files, cgo throws duplicate errors. Only thing 
>> that works is copying all c files from cpank, stdlib, ext and the 
>> include directory into the goapi directory.
>>
>> Is there any way to tell cgo to compile this, this and that files like we 
>> use with normal c projects.
>>
>> How do I make this work?
>>
>> Any help or suggestions will be appreciated.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/08e88126-965c-4211-a976-2edb64533510n%40googlegroups.com.


[go-nuts] Pointer constraint to generics

2023-04-15 Thread Jan
hi,

This is a variation for a previous topic 
, but 
since there isn't a clear solution, I thought I would ask if anyone can 
think of a work around.

I've been interacting a lot with C++ libraries from Go, and one of the 
commonly returned types is an abls::StatusOr 
, for which I created a simple C 
wrapper that casts the error and value to a `char *` and `void *` 
respectively (dropping the type information in between C++ and Go).

In Go I want to return the type information, so I defined a small generic 
function:

// PointerOrError converts a StatusOr structure to either a pointer to T 
with the data
// or the Status converted to an error message and then freed.
func PointerOrError[T any](s C.StatusOr) (*T, error) {
ptr, err := UnsafePointerOrError(s) // returns unsafe.Pointer, error
if err != nil {
return nil, err
}
return (*T)(ptr), nil
}

Now this doesn't work for my forward declared C++ types (most of them are 
just aliases to C++ objects) -- Go complaints with: `cannot use incomplete 
(or unallocatable) type as a type argument`, because `T` is incomplete 
indeed.

But ... I will never instantiate `T`, I only care about `*T`, which is not 
incomplete.

But there isn't a way to say a generics attribute is a pointer. So if I use 
the following:

func PointerOrError2[T any](s C.StatusOr) (t T, err error) {
var ptr unsafe.Pointer
ptr, err = UnsafePointerOrError(s) // <-- unsafe.Pointer, error
if err != nil {
return
}
t = (T)(ptr)
return
}

And instantiate it with a `PointerOrError2[*MyType](statusOr)` for 
instance, I get, as expected:

cannot convert ptr (variable of type unsafe.Pointer) to type T

Any suggestions to make this work ? 

I could probably craft something using the `reflect` package, but I was 
hoping for a smart (and likely faster?) generics solution.

cheers
Jan






-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/bd1e1a06-e10b-460e-b04a-3ea06b3be06fn%40googlegroups.com.


Re: [go-nuts] Avoid forcing my module users' to carry dependencies from code that live under the same repo just as examples/ debugging purposes.

2023-04-15 Thread Jim Idle
Indeed. Some parts of it are fine though, given that the ask was for a
starting point. I agree completely with the pkg stuff being wrong. I use
just some of the ideas here, but I don’t see what the point of pkg is.

On Fri, Apr 14, 2023 at 16:04 'Dan Kortschak' via golang-nuts <
golang-nuts@googlegroups.com> wrote:

> On Fri, 2023-04-14 at 14:01 +0800, Jim Idle wrote:
> > You might start with this repo:
> >
> > https://github.com/golang-standards/project-layout
> >
> > This is not an 'official' standard, though it does encapsulate the
> > things that are standard go such as the internal directory.
> >
> > Personally I avoid its recommendation to use a directory 'pkg' to
> > store your module code as it makes the import path quite strange. But
> > for a main you can look at the cmd directory or the internal
> > directory. You will not go too far wrong by following this guide.
> >
> > Jim
>
> It's worth noting this issue in that repo:
> https://github.com/golang-standards/project-layout/issues/117
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/c66f416360824816fa1c9415126c78969beaad9b.camel%40kortschak.io
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAGPPfg9s6UNPvQAJ4mz_kH4i6t%3Dp3W8Eb4G-573a-_CnE4F7uA%40mail.gmail.com.


[go-nuts] Re: Pointer constraint to generics

2023-04-15 Thread jake...@gmail.com
What About:

func PointerOrError[T *Q, Q any](s C.StatusOr ) (t T, err error) 

Seems to compile: https://go.dev/play/p/n4I-XkONj-O?v=gotip

On Saturday, April 15, 2023 at 6:28:39 AM UTC-4 Jan wrote:

> hi,
>
> This is a variation for a previous topic 
> , 
> but since there isn't a clear solution, I thought I would ask if anyone can 
> think of a work around.
>
> I've been interacting a lot with C++ libraries from Go, and one of the 
> commonly returned types is an abls::StatusOr 
> , for which I created a simple 
> C wrapper that casts the error and value to a `char *` and `void *` 
> respectively (dropping the type information in between C++ and Go).
>
> In Go I want to return the type information, so I defined a small generic 
> function:
>
> // PointerOrError converts a StatusOr structure to either a pointer to T 
> with the data
> // or the Status converted to an error message and then freed.
> func PointerOrError[T any](s C.StatusOr) (*T, error) {
> ptr, err := UnsafePointerOrError(s) // returns unsafe.Pointer, error
> if err != nil {
> return nil, err
> }
> return (*T)(ptr), nil
> }
>
> Now this doesn't work for my forward declared C++ types (most of them are 
> just aliases to C++ objects) -- Go complaints with: `cannot use 
> incomplete (or unallocatable) type as a type argument`, because `T` is 
> incomplete indeed.
>
> But ... I will never instantiate `T`, I only care about `*T`, which is not 
> incomplete.
>
> But there isn't a way to say a generics attribute is a pointer. So if I 
> use the following:
>
> func PointerOrError2[T any](s C.StatusOr) (t T, err error) {
> var ptr unsafe.Pointer
> ptr, err = UnsafePointerOrError(s) // <-- unsafe.Pointer, error
> if err != nil {
> return
> }
> t = (T)(ptr)
> return
> }
>
> And instantiate it with a `PointerOrError2[*MyType](statusOr)` for 
> instance, I get, as expected:
>
> cannot convert ptr (variable of type unsafe.Pointer) to type T
>
> Any suggestions to make this work ? 
>
> I could probably craft something using the `reflect` package, but I was 
> hoping for a smart (and likely faster?) generics solution.
>
> cheers
> Jan
>
>
>
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/b9e377dd-4ef8-4cdf-8c36-80cb086910b1n%40googlegroups.com.


[go-nuts] Re: Pointer constraint to generics

2023-04-15 Thread Jan
Thanks! I hadn't realized that one could constraint T to be a pointer type 
by using a second type paramater Q, this in nice.

But alas, it doesn't work. When I copy&pasted your code to mine, and used 
an undefined C type ... it complained of the same thing:

```
cannot use incomplete (or unallocatable) type as a type argument: 
gomlx/xla._Ctype_struct_StableHLOHolder
```

I'm using it in the following snipped of code:

```
func (comp *Computation) ToStableHLO() (*StableHLO, error) {
if comp.IsNil() || comp.firstError != nil {
return nil, errors.Errorf("Computation graph is nil!?")
}
statusOr := C.ConvertComputationToStableHLO(comp.cCompPtr)
cPtr, err := PointerOrError[*C.StableHLOHolder](statusOr)
if err != nil {
return nil, errors.Wrapf(err, "failed conversion in Computation.ToStableHLO"
)
}
return NewStableHLO(cPtr), nil
}
```

I suspect it doesn't allow matching Q to an incomplete type 
(`C.StableHLOHolder` in this example), same way as my original version :(

I think your example in playground doesn't capture that -- the playground 
doesn't seem to allow CGO code (i tried this 
, but it didn't 
even try to compile).

I mean it's not the end of the world, I can always cast it in the next line 
... it's just one of those little things that would be "ergonomically" very 
nice if it worked :)



On Saturday, April 15, 2023 at 3:02:14 PM UTC+2 jake...@gmail.com wrote:

> What About:
>
> func PointerOrError[T *Q, Q any](s C.StatusOr ) (t T, err error) 
>
> Seems to compile: https://go.dev/play/p/n4I-XkONj-O?v=gotip
>
> On Saturday, April 15, 2023 at 6:28:39 AM UTC-4 Jan wrote:
>
>> hi,
>>
>> This is a variation for a previous topic 
>> , 
>> but since there isn't a clear solution, I thought I would ask if anyone can 
>> think of a work around.
>>
>> I've been interacting a lot with C++ libraries from Go, and one of the 
>> commonly returned types is an abls::StatusOr 
>> , for which I created a simple 
>> C wrapper that casts the error and value to a `char *` and `void *` 
>> respectively (dropping the type information in between C++ and Go).
>>
>> In Go I want to return the type information, so I defined a small generic 
>> function:
>>
>> // PointerOrError converts a StatusOr structure to either a pointer to T 
>> with the data
>> // or the Status converted to an error message and then freed.
>> func PointerOrError[T any](s C.StatusOr) (*T, error) {
>> ptr, err := UnsafePointerOrError(s) // returns unsafe.Pointer, error
>> if err != nil {
>> return nil, err
>> }
>> return (*T)(ptr), nil
>> }
>>
>> Now this doesn't work for my forward declared C++ types (most of them are 
>> just aliases to C++ objects) -- Go complaints with: `cannot use 
>> incomplete (or unallocatable) type as a type argument`, because `T` is 
>> incomplete indeed.
>>
>> But ... I will never instantiate `T`, I only care about `*T`, which is 
>> not incomplete.
>>
>> But there isn't a way to say a generics attribute is a pointer. So if I 
>> use the following:
>>
>> func PointerOrError2[T any](s C.StatusOr) (t T, err error) {
>> var ptr unsafe.Pointer
>> ptr, err = UnsafePointerOrError(s) // <-- unsafe.Pointer, error
>> if err != nil {
>> return
>> }
>> t = (T)(ptr)
>> return
>> }
>>
>> And instantiate it with a `PointerOrError2[*MyType](statusOr)` for 
>> instance, I get, as expected:
>>
>> cannot convert ptr (variable of type unsafe.Pointer) to type T
>>
>> Any suggestions to make this work ? 
>>
>> I could probably craft something using the `reflect` package, but I was 
>> hoping for a smart (and likely faster?) generics solution.
>>
>> cheers
>> Jan
>>
>>
>>
>>
>>
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/d5c7ac8c-5db7-463e-b87b-c8d2ad6c727en%40googlegroups.com.


[go-nuts] Re: Pointer constraint to generics

2023-04-15 Thread Jan
Re-factoring your example to use CGO, in a small main.go file:

```
$ go run .
./main.go:28:10: cannot use incomplete (or unallocatable) type as a type 
argument: main._Ctype_struct_MyThing
$ cat main.go 
package main

// struct MyThing;
// typedef struct MyThing MyThing;
import "C"
import (
"fmt"
"unsafe"
)
import "flag"

func PointerOrError[T *Q, Q any](s int) (t T, err error) {
var ptr unsafe.Pointer
ptr, err = UnsafePointerOrError(s) // <-- unsafe.Pointer, error
if err != nil {
return
}
t = (T)(ptr)
return
}

func UnsafePointerOrError(v int) (unsafe.Pointer, error) {
return unsafe.Pointer(&v), nil
}

func main() {
flag.Parse()
t, _ := PointerOrError[*C.MyThing](1)
fmt.Println(t)
}
```


On Saturday, April 15, 2023 at 8:41:28 PM UTC+2 Jan wrote:

> Thanks! I hadn't realized that one could constraint T to be a pointer type 
> by using a second type paramater Q, this in nice.
>
> But alas, it doesn't work. When I copy&pasted your code to mine, and used 
> an undefined C type ... it complained of the same thing:
>
> ```
> cannot use incomplete (or unallocatable) type as a type argument: 
> gomlx/xla._Ctype_struct_StableHLOHolder
> ```
>
> I'm using it in the following snipped of code:
>
> ```
> func (comp *Computation) ToStableHLO() (*StableHLO, error) {
> if comp.IsNil() || comp.firstError != nil {
> return nil, errors.Errorf("Computation graph is nil!?")
> }
> statusOr := C.ConvertComputationToStableHLO(comp.cCompPtr)
> cPtr, err := PointerOrError[*C.StableHLOHolder](statusOr)
> if err != nil {
> return nil, errors.Wrapf(err, "failed conversion in 
> Computation.ToStableHLO")
> }
> return NewStableHLO(cPtr), nil
> }
> ```
>
> I suspect it doesn't allow matching Q to an incomplete type 
> (`C.StableHLOHolder` in this example), same way as my original version :(
>
> I think your example in playground doesn't capture that -- the playground 
> doesn't seem to allow CGO code (i tried this 
> , but it 
> didn't even try to compile).
>
> I mean it's not the end of the world, I can always cast it in the next 
> line ... it's just one of those little things that would be "ergonomically" 
> very nice if it worked :)
>
>
>
> On Saturday, April 15, 2023 at 3:02:14 PM UTC+2 jake...@gmail.com wrote:
>
>> What About:
>>
>> func PointerOrError[T *Q, Q any](s C.StatusOr ) (t T, err error) 
>>
>> Seems to compile: https://go.dev/play/p/n4I-XkONj-O?v=gotip
>>
>> On Saturday, April 15, 2023 at 6:28:39 AM UTC-4 Jan wrote:
>>
>>> hi,
>>>
>>> This is a variation for a previous topic 
>>> , 
>>> but since there isn't a clear solution, I thought I would ask if anyone can 
>>> think of a work around.
>>>
>>> I've been interacting a lot with C++ libraries from Go, and one of the 
>>> commonly returned types is an abls::StatusOr 
>>> , for which I created a 
>>> simple C wrapper that casts the error and value to a `char *` and `void *` 
>>> respectively (dropping the type information in between C++ and Go).
>>>
>>> In Go I want to return the type information, so I defined a small 
>>> generic function:
>>>
>>> // PointerOrError converts a StatusOr structure to either a pointer to 
>>> T with the data
>>> // or the Status converted to an error message and then freed.
>>> func PointerOrError[T any](s C.StatusOr) (*T, error) {
>>> ptr, err := UnsafePointerOrError(s) // returns unsafe.Pointer, error
>>> if err != nil {
>>> return nil, err
>>> }
>>> return (*T)(ptr), nil
>>> }
>>>
>>> Now this doesn't work for my forward declared C++ types (most of them 
>>> are just aliases to C++ objects) -- Go complaints with: `cannot use 
>>> incomplete (or unallocatable) type as a type argument`, because `T` is 
>>> incomplete indeed.
>>>
>>> But ... I will never instantiate `T`, I only care about `*T`, which is 
>>> not incomplete.
>>>
>>> But there isn't a way to say a generics attribute is a pointer. So if I 
>>> use the following:
>>>
>>> func PointerOrError2[T any](s C.StatusOr) (t T, err error) {
>>> var ptr unsafe.Pointer
>>> ptr, err = UnsafePointerOrError(s) // <-- unsafe.Pointer, error
>>> if err != nil {
>>> return
>>> }
>>> t = (T)(ptr)
>>> return
>>> }
>>>
>>> And instantiate it with a `PointerOrError2[*MyType](statusOr)` for 
>>> instance, I get, as expected:
>>>
>>> cannot convert ptr (variable of type unsafe.Pointer) to type T
>>>
>>> Any suggestions to make this work ? 
>>>
>>> I could probably craft something using the `reflect` package, but I was 
>>> hoping for a smart (and likely faster?) generics solution.
>>>
>>> cheers
>>> Jan
>>>
>>>
>>>
>>>
>>>
>>>
>>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nut

[go-nuts] Importing a package declared in the root directory of a module

2023-04-15 Thread Victor Giordano
Hi there! 

I was playing a little bit with modules and packages, regarding making 
projects.
And I'm struggling to use a package (non-main) declared in the root 
directory.. allow me to show an example:

This scenario,  I have project somewhere on my file system
.
├── cmd
│   └── main.go
├── go.mod
└── util.go

*go.mod*
module a_module_path

go 1.19


*util.go*
package util

func Abs(x int) int {
if x < 0 {
return -x
} else {
return x
}
}

And the problem appears when I try to use the package util (in the root 
directory of the module) within another package of the module...

*cmd/main.go*
package main

import (
"fmt"
"a_module_path/util" // this doesn't works
"a_module_path/../util" // this attemp neither (and as relative import 
paths are not supported in module mode i guess is a no-go)
)

func main() {
fmt.Println(util.Abs(-2))
}

The question, as you may predict, is *¿If there any way to make this work?*
*I do not want to create a folder named util (or whatever) and place there 
the util package.*

Thanks for the reading and the patience!
Keep rocking code! 

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/05b29995-e451-4235-a613-8193d995efcdn%40googlegroups.com.


Re: [go-nuts] Importing a package declared in the root directory of a module

2023-04-15 Thread 'Sean Liao' via golang-nuts
import "a_module_path"

optionally rename it to make it clearer

import util "a_module_path"

- sean

On Sat, Apr 15, 2023, 20:31 Victor Giordano  wrote:

> Hi there!
>
> I was playing a little bit with modules and packages, regarding making
> projects.
> And I'm struggling to use a package (non-main) declared in the root
> directory.. allow me to show an example:
>
> This scenario,  I have project somewhere on my file system
> .
> ├── cmd
> │   └── main.go
> ├── go.mod
> └── util.go
>
> *go.mod*
> module a_module_path
>
> go 1.19
>
>
> *util.go*
> package util
>
> func Abs(x int) int {
> if x < 0 {
> return -x
> } else {
> return x
> }
> }
>
> And the problem appears when I try to use the package util (in the root
> directory of the module) within another package of the module...
>
> *cmd/main.go*
> package main
>
> import (
> "fmt"
> "a_module_path/util" // this doesn't works
> "a_module_path/../util" // this attemp neither (and as relative import
> paths are not supported in module mode i guess is a no-go)
> )
>
> func main() {
> fmt.Println(util.Abs(-2))
> }
>
> The question, as you may predict, is *¿If there any way to make this
> work?*
> *I do not want to create a folder named util (or whatever) and place there
> the util package.*
>
> Thanks for the reading and the patience!
> Keep rocking code!
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/05b29995-e451-4235-a613-8193d995efcdn%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAGabyPqDhNox%3DLCOSkbLZaVpQF74ubzroL%3DWOkDuYMdb0zvoVA%40mail.gmail.com.


Re: [go-nuts] Importing a package declared in the root directory of a module

2023-04-15 Thread Victor Giordano
Thanks *Sean!!*
That makes sense for me!

But i guess I must import with an alias as import without an alias doesn't 
works, right?

@:/cmd$ go build .
# a_module_path/cmd
./main.go:4:2: imported and not used: "a_module_path" as util
./main.go:9:14: undefined: a_module_path

main.go
package main

import (
"a_module_path"
"fmt"
)

func main() {
fmt.Println(a_module_path.Abs(-2))
}

I mean... i'm actually importing it without utill.. so the message kind of 
confuse me.

Thanks again.
El sábado, 15 de abril de 2023 a las 16:34:01 UTC-3, Sean Liao escribió:

> import "a_module_path"
>
> optionally rename it to make it clearer
>
> import util "a_module_path"
>
> - sean
>
> On Sat, Apr 15, 2023, 20:31 Victor Giordano  wrote:
>
>> Hi there! 
>>
>> I was playing a little bit with modules and packages, regarding making 
>> projects.
>> And I'm struggling to use a package (non-main) declared in the root 
>> directory.. allow me to show an example:
>>
>> This scenario,  I have project somewhere on my file system
>> .
>> ├── cmd
>> │   └── main.go
>> ├── go.mod
>> └── util.go
>>
>> *go.mod*
>> module a_module_path
>>
>> go 1.19
>>
>>
>> *util.go*
>> package util
>>
>> func Abs(x int) int {
>> if x < 0 {
>> return -x
>> } else {
>> return x
>> }
>> }
>>
>> And the problem appears when I try to use the package util (in the root 
>> directory of the module) within another package of the module...
>>
>> *cmd/main.go*
>> package main
>>
>> import (
>> "fmt"
>> "a_module_path/util" // this doesn't works
>> "a_module_path/../util" // this attemp neither (and as relative import 
>> paths are not supported in module mode i guess is a no-go)
>> )
>>
>> func main() {
>> fmt.Println(util.Abs(-2))
>> }
>>
>> The question, as you may predict, is *¿If there any way to make this 
>> work?*
>> *I do not want to create a folder named util (or whatever) and place 
>> there the util package.*
>>
>> Thanks for the reading and the patience!
>> Keep rocking code! 
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "golang-nuts" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to golang-nuts...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/golang-nuts/05b29995-e451-4235-a613-8193d995efcdn%40googlegroups.com
>>  
>> 
>> .
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/f5bd0bd6-dc15-4160-89a0-34ae4d89d70bn%40googlegroups.com.


Re: [go-nuts] Importing a package declared in the root directory of a module

2023-04-15 Thread 'Sean Liao' via golang-nuts
the import path specifies the location to find a package, but the actual
identifier used is the one in the package declaration, so

import "a_module_path"

var foo = util.Foo

- sean

On Sat, Apr 15, 2023, 20:40 Victor Giordano  wrote:

> Thanks *Sean!!*
> That makes sense for me!
>
> But i guess I must import with an alias as import without an alias
> doesn't works, right?
>
> @:/cmd$ go build .
> # a_module_path/cmd
> ./main.go:4:2: imported and not used: "a_module_path" as util
> ./main.go:9:14: undefined: a_module_path
>
> main.go
> package main
>
> import (
> "a_module_path"
> "fmt"
> )
>
> func main() {
> fmt.Println(a_module_path.Abs(-2))
> }
>
> I mean... i'm actually importing it without utill.. so the message kind of
> confuse me.
>
> Thanks again.
> El sábado, 15 de abril de 2023 a las 16:34:01 UTC-3, Sean Liao escribió:
>
>> import "a_module_path"
>>
>> optionally rename it to make it clearer
>>
>> import util "a_module_path"
>>
>> - sean
>>
>> On Sat, Apr 15, 2023, 20:31 Victor Giordano  wrote:
>>
>>> Hi there!
>>>
>>> I was playing a little bit with modules and packages, regarding making
>>> projects.
>>> And I'm struggling to use a package (non-main) declared in the root
>>> directory.. allow me to show an example:
>>>
>>> This scenario,  I have project somewhere on my file system
>>> .
>>> ├── cmd
>>> │   └── main.go
>>> ├── go.mod
>>> └── util.go
>>>
>>> *go.mod*
>>> module a_module_path
>>>
>>> go 1.19
>>>
>>>
>>> *util.go*
>>> package util
>>>
>>> func Abs(x int) int {
>>> if x < 0 {
>>> return -x
>>> } else {
>>> return x
>>> }
>>> }
>>>
>>> And the problem appears when I try to use the package util (in the root
>>> directory of the module) within another package of the module...
>>>
>>> *cmd/main.go*
>>> package main
>>>
>>> import (
>>> "fmt"
>>> "a_module_path/util" // this doesn't works
>>> "a_module_path/../util" // this attemp neither (and as relative import
>>> paths are not supported in module mode i guess is a no-go)
>>> )
>>>
>>> func main() {
>>> fmt.Println(util.Abs(-2))
>>> }
>>>
>>> The question, as you may predict, is *¿If there any way to make this
>>> work?*
>>> *I do not want to create a folder named util (or whatever) and place
>>> there the util package.*
>>>
>>> Thanks for the reading and the patience!
>>> Keep rocking code!
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "golang-nuts" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to golang-nuts...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/golang-nuts/05b29995-e451-4235-a613-8193d995efcdn%40googlegroups.com
>>> 
>>> .
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/f5bd0bd6-dc15-4160-89a0-34ae4d89d70bn%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAGabyPqs%2BHUse780M-CdYaAYR%3D8qSdWotYxE5QSqJZwfSD65SA%40mail.gmail.com.


Re: [go-nuts] Importing a package declared in the root directory of a module

2023-04-15 Thread Victor Giordano
*Thanks again!*

El sáb, 15 abr 2023 a las 16:50, 'Sean Liao' via golang-nuts (<
golang-nuts@googlegroups.com>) escribió:

> the import path specifies the location to find a package, but the actual
> identifier used is the one in the package declaration, so
>
> import "a_module_path"
>
> var foo = util.Foo
>
> - sean
>
> On Sat, Apr 15, 2023, 20:40 Victor Giordano  wrote:
>
>> Thanks *Sean!!*
>> That makes sense for me!
>>
>> But i guess I must import with an alias as import without an alias
>> doesn't works, right?
>>
>> @:/cmd$ go build .
>> # a_module_path/cmd
>> ./main.go:4:2: imported and not used: "a_module_path" as util
>> ./main.go:9:14: undefined: a_module_path
>>
>> main.go
>> package main
>>
>> import (
>> "a_module_path"
>> "fmt"
>> )
>>
>> func main() {
>> fmt.Println(a_module_path.Abs(-2))
>> }
>>
>> I mean... i'm actually importing it without utill.. so the message kind
>> of confuse me.
>>
>> Thanks again.
>> El sábado, 15 de abril de 2023 a las 16:34:01 UTC-3, Sean Liao escribió:
>>
>>> import "a_module_path"
>>>
>>> optionally rename it to make it clearer
>>>
>>> import util "a_module_path"
>>>
>>> - sean
>>>
>>> On Sat, Apr 15, 2023, 20:31 Victor Giordano  wrote:
>>>
 Hi there!

 I was playing a little bit with modules and packages, regarding making
 projects.
 And I'm struggling to use a package (non-main) declared in the root
 directory.. allow me to show an example:

 This scenario,  I have project somewhere on my file system
 .
 ├── cmd
 │   └── main.go
 ├── go.mod
 └── util.go

 *go.mod*
 module a_module_path

 go 1.19


 *util.go*
 package util

 func Abs(x int) int {
 if x < 0 {
 return -x
 } else {
 return x
 }
 }

 And the problem appears when I try to use the package util (in the root
 directory of the module) within another package of the module...

 *cmd/main.go*
 package main

 import (
 "fmt"
 "a_module_path/util" // this doesn't works
 "a_module_path/../util" // this attemp neither (and as relative import
 paths are not supported in module mode i guess is a no-go)
 )

 func main() {
 fmt.Println(util.Abs(-2))
 }

 The question, as you may predict, is *¿If there any way to make this
 work?*
 *I do not want to create a folder named util (or whatever) and place
 there the util package.*

 Thanks for the reading and the patience!
 Keep rocking code!

 --
 You received this message because you are subscribed to the Google
 Groups "golang-nuts" group.
 To unsubscribe from this group and stop receiving emails from it, send
 an email to golang-nuts...@googlegroups.com.
 To view this discussion on the web visit
 https://groups.google.com/d/msgid/golang-nuts/05b29995-e451-4235-a613-8193d995efcdn%40googlegroups.com
 
 .

>>> --
>> You received this message because you are subscribed to the Google Groups
>> "golang-nuts" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to golang-nuts+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/golang-nuts/f5bd0bd6-dc15-4160-89a0-34ae4d89d70bn%40googlegroups.com
>> 
>> .
>>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "golang-nuts" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/golang-nuts/9LtHHkjGsGQ/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/CAGabyPqs%2BHUse780M-CdYaAYR%3D8qSdWotYxE5QSqJZwfSD65SA%40mail.gmail.com
> 
> .
>


-- 
V

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAPUu9suCZPSJkRsrSyc%3DopmCBUjK8hYe3RimaLsGRHjh%3DUyuFQ%40mail.gmail.com.


Re: [go-nuts] Re: Pointer constraint to generics

2023-04-15 Thread 'Axel Wagner' via golang-nuts
You should be able to instantiate the function using a Pointer. That is,
you can write

func PointerOrError[T any](s C.StatusOr) (t T, err error) {
var ptr unsafe.Pointer
ptr, err = UnsafePointerOrError(s) // <-- unsafe.Pointer, error
if err != nil {
return
}
return *(*T)(unsafe.Pointer(&ptr))
}

func main() {
var s C.StatusOr
p := PointerOrError[*C.mystruct](s)
_ = p
}

It's unfortunate, of course, that this would allow you to instantiate the
function using a non-pointer as well, but it seems that's impossible to
prevent statically? You can catch it dynamically by doing something akin to

if k := reflect.TypeOf(*new(T)).Kind(); k != reflect.Pointer && k !=
reflect.UnsafePointer {
panic("PointerOrError must be instantiated with pointer type")
}

Obviously, none of this is ideal, but maybe it's the best you can do -
apart from generating code.

On Sat, Apr 15, 2023 at 8:48 PM Jan  wrote:

> Re-factoring your example to use CGO, in a small main.go file:
>
> ```
> $ go run .
> ./main.go:28:10: cannot use incomplete (or unallocatable) type as a type
> argument: main._Ctype_struct_MyThing
> $ cat main.go
> package main
>
> // struct MyThing;
> // typedef struct MyThing MyThing;
> import "C"
> import (
> "fmt"
> "unsafe"
> )
> import "flag"
>
> func PointerOrError[T *Q, Q any](s int) (t T, err error) {
> var ptr unsafe.Pointer
> ptr, err = UnsafePointerOrError(s) // <-- unsafe.Pointer, error
> if err != nil {
> return
> }
> t = (T)(ptr)
> return
> }
>
> func UnsafePointerOrError(v int) (unsafe.Pointer, error) {
> return unsafe.Pointer(&v), nil
> }
>
> func main() {
> flag.Parse()
> t, _ := PointerOrError[*C.MyThing](1)
> fmt.Println(t)
> }
> ```
>
>
> On Saturday, April 15, 2023 at 8:41:28 PM UTC+2 Jan wrote:
>
>> Thanks! I hadn't realized that one could constraint T to be a pointer
>> type by using a second type paramater Q, this in nice.
>>
>> But alas, it doesn't work. When I copy&pasted your code to mine, and used
>> an undefined C type ... it complained of the same thing:
>>
>> ```
>> cannot use incomplete (or unallocatable) type as a type argument:
>> gomlx/xla._Ctype_struct_StableHLOHolder
>> ```
>>
>> I'm using it in the following snipped of code:
>>
>> ```
>> func (comp *Computation) ToStableHLO() (*StableHLO, error) {
>> if comp.IsNil() || comp.firstError != nil {
>> return nil, errors.Errorf("Computation graph is nil!?")
>> }
>> statusOr := C.ConvertComputationToStableHLO(comp.cCompPtr)
>> cPtr, err := PointerOrError[*C.StableHLOHolder](statusOr)
>> if err != nil {
>> return nil, errors.Wrapf(err, "failed conversion in
>> Computation.ToStableHLO")
>> }
>> return NewStableHLO(cPtr), nil
>> }
>> ```
>>
>> I suspect it doesn't allow matching Q to an incomplete type
>> (`C.StableHLOHolder` in this example), same way as my original version :(
>>
>> I think your example in playground doesn't capture that -- the playground
>> doesn't seem to allow CGO code (i tried this
>> , but it
>> didn't even try to compile).
>>
>> I mean it's not the end of the world, I can always cast it in the next
>> line ... it's just one of those little things that would be "ergonomically"
>> very nice if it worked :)
>>
>>
>>
>> On Saturday, April 15, 2023 at 3:02:14 PM UTC+2 jake...@gmail.com wrote:
>>
>>> What About:
>>>
>>> func PointerOrError[T *Q, Q any](s C.StatusOr ) (t T, err error)
>>>
>>> Seems to compile: https://go.dev/play/p/n4I-XkONj-O?v=gotip
>>>
>>> On Saturday, April 15, 2023 at 6:28:39 AM UTC-4 Jan wrote:
>>>
 hi,

 This is a variation for a previous topic
 ,
 but since there isn't a clear solution, I thought I would ask if anyone can
 think of a work around.

 I've been interacting a lot with C++ libraries from Go, and one of the
 commonly returned types is an abls::StatusOr
 , for which I created a
 simple C wrapper that casts the error and value to a `char *` and `void *`
 respectively (dropping the type information in between C++ and Go).

 In Go I want to return the type information, so I defined a small
 generic function:

 // PointerOrError converts a StatusOr structure to either a pointer to
 T with the data
 // or the Status converted to an error message and then freed.
 func PointerOrError[T any](s C.StatusOr) (*T, error) {
 ptr, err := UnsafePointerOrError(s) // returns unsafe.Pointer, error
 if err != nil {
 return nil, err
 }
 return (*T)(ptr), nil
 }

 Now this doesn't work for my forward declared C++ types (most of them
 are just aliases to C++ objects) -- Go complaints with: `cannot use
 incomplete (or unallocatable) type as a typ

Re: [go-nuts] Re: Pointer constraint to generics

2023-04-15 Thread 'Axel Wagner' via golang-nuts
I guess thinking about some more, I don't really understand the point of
what you're doing. It seems you *can* actually get the static guarantee you
want - you just have to spell the call
`(*C.mystruct)(UnsafePointerOrError(s))` instead of
`PointerOrError[C.mystruct](s)`. If you *could* restrict the type parameter
to be "any pointer", the two would actually be entirely equivalent, giving
you exactly the same guarantees.

I think in reality you *don't* want to allow "any pointer type". I think in
reality you want the type to be dependent on the C++ function you are
calling - which returns the Status. I don't think you can use templated C++
types, otherwise you would probably want to do something like
func Call[T any](f func(…) C.StatusOr) (T, error)

In the absence of that, you might try listing the types which are valid:
type CPointer interface{
*C.MyStruct | *C.MyOtherStruct | *C.YourStruct
}
func PointerOrError[T CPointer](s C.StatusOr) (T, error) { … }



On Sat, Apr 15, 2023 at 10:24 PM Axel Wagner 
wrote:

> You should be able to instantiate the function using a Pointer. That is,
> you can write
>
> func PointerOrError[T any](s C.StatusOr) (t T, err error) {
> var ptr unsafe.Pointer
> ptr, err = UnsafePointerOrError(s) // <-- unsafe.Pointer, error
> if err != nil {
> return
> }
> return *(*T)(unsafe.Pointer(&ptr))
> }
>
> func main() {
> var s C.StatusOr
> p := PointerOrError[*C.mystruct](s)
> _ = p
> }
>
> It's unfortunate, of course, that this would allow you to instantiate the
> function using a non-pointer as well, but it seems that's impossible to
> prevent statically? You can catch it dynamically by doing something akin to
>
> if k := reflect.TypeOf(*new(T)).Kind(); k != reflect.Pointer && k !=
> reflect.UnsafePointer {
> panic("PointerOrError must be instantiated with pointer type")
> }
>
> Obviously, none of this is ideal, but maybe it's the best you can do -
> apart from generating code.
>
> On Sat, Apr 15, 2023 at 8:48 PM Jan  wrote:
>
>> Re-factoring your example to use CGO, in a small main.go file:
>>
>> ```
>> $ go run .
>> ./main.go:28:10: cannot use incomplete (or unallocatable) type as a type
>> argument: main._Ctype_struct_MyThing
>> $ cat main.go
>> package main
>>
>> // struct MyThing;
>> // typedef struct MyThing MyThing;
>> import "C"
>> import (
>> "fmt"
>> "unsafe"
>> )
>> import "flag"
>>
>> func PointerOrError[T *Q, Q any](s int) (t T, err error) {
>> var ptr unsafe.Pointer
>> ptr, err = UnsafePointerOrError(s) // <-- unsafe.Pointer, error
>> if err != nil {
>> return
>> }
>> t = (T)(ptr)
>> return
>> }
>>
>> func UnsafePointerOrError(v int) (unsafe.Pointer, error) {
>> return unsafe.Pointer(&v), nil
>> }
>>
>> func main() {
>> flag.Parse()
>> t, _ := PointerOrError[*C.MyThing](1)
>> fmt.Println(t)
>> }
>> ```
>>
>>
>> On Saturday, April 15, 2023 at 8:41:28 PM UTC+2 Jan wrote:
>>
>>> Thanks! I hadn't realized that one could constraint T to be a pointer
>>> type by using a second type paramater Q, this in nice.
>>>
>>> But alas, it doesn't work. When I copy&pasted your code to mine, and
>>> used an undefined C type ... it complained of the same thing:
>>>
>>> ```
>>> cannot use incomplete (or unallocatable) type as a type argument:
>>> gomlx/xla._Ctype_struct_StableHLOHolder
>>> ```
>>>
>>> I'm using it in the following snipped of code:
>>>
>>> ```
>>> func (comp *Computation) ToStableHLO() (*StableHLO, error) {
>>> if comp.IsNil() || comp.firstError != nil {
>>> return nil, errors.Errorf("Computation graph is nil!?")
>>> }
>>> statusOr := C.ConvertComputationToStableHLO(comp.cCompPtr)
>>> cPtr, err := PointerOrError[*C.StableHLOHolder](statusOr)
>>> if err != nil {
>>> return nil, errors.Wrapf(err, "failed conversion in
>>> Computation.ToStableHLO")
>>> }
>>> return NewStableHLO(cPtr), nil
>>> }
>>> ```
>>>
>>> I suspect it doesn't allow matching Q to an incomplete type
>>> (`C.StableHLOHolder` in this example), same way as my original version :(
>>>
>>> I think your example in playground doesn't capture that -- the
>>> playground doesn't seem to allow CGO code (i tried this
>>> , but it
>>> didn't even try to compile).
>>>
>>> I mean it's not the end of the world, I can always cast it in the next
>>> line ... it's just one of those little things that would be "ergonomically"
>>> very nice if it worked :)
>>>
>>>
>>>
>>> On Saturday, April 15, 2023 at 3:02:14 PM UTC+2 jake...@gmail.com wrote:
>>>
 What About:

 func PointerOrError[T *Q, Q any](s C.StatusOr ) (t T, err error)

 Seems to compile: https://go.dev/play/p/n4I-XkONj-O?v=gotip

 On Saturday, April 15, 2023 at 6:28:39 AM UTC-4 Jan wrote:

> hi,
>
> This is a variation for a previous topic
> 

Re: [go-nuts] Re: Pointer constraint to generics

2023-04-15 Thread Jan
Thanks for the suggestions Alex. Interesting that enumerating the pointers 
in a constraint would work ... but it makes sense.

I think I disagree with the statement about not wanting to allow any 
pointer type. From my project perspective there will be indeed a limited 
number of those (~10).  But from the `PointerOrError` library (my StatusOr 
library) perspective, I think it shouldn't know which pointers it's being 
used for (or need to include every ".h" file I have). Ideally it would 
support arbitrary pointer type that a client of the library might want to 
use. 

Again ... not a big issue I can simply use the UnsafePointerOrError 
version, and cast to the desired pointer at the next line. It's just would 
feel clearner with one fewer line with a floating unsafe pointer around :)

cheers 

On Saturday, April 15, 2023 at 10:54:47 PM UTC+2 Axel Wagner wrote:

> I guess thinking about some more, I don't really understand the point of 
> what you're doing. It seems you *can* actually get the static guarantee you 
> want - you just have to spell the call 
> `(*C.mystruct)(UnsafePointerOrError(s))` instead of 
> `PointerOrError[C.mystruct](s)`. If you *could* restrict the type parameter 
> to be "any pointer", the two would actually be entirely equivalent, giving 
> you exactly the same guarantees.
>
> I think in reality you *don't* want to allow "any pointer type". I think 
> in reality you want the type to be dependent on the C++ function you are 
> calling - which returns the Status. I don't think you can use templated C++ 
> types, otherwise you would probably want to do something like
> func Call[T any](f func(…) C.StatusOr) (T, error)
>
> In the absence of that, you might try listing the types which are valid:
> type CPointer interface{
> *C.MyStruct | *C.MyOtherStruct | *C.YourStruct
> }
> func PointerOrError[T CPointer](s C.StatusOr) (T, error) { … }
>
>
>
> On Sat, Apr 15, 2023 at 10:24 PM Axel Wagner  
> wrote:
>
>> You should be able to instantiate the function using a Pointer. That is, 
>> you can write
>>
>> func PointerOrError[T any](s C.StatusOr) (t T, err error) {
>> var ptr unsafe.Pointer
>> ptr, err = UnsafePointerOrError(s) // <-- unsafe.Pointer, error
>> if err != nil {
>> return
>> }
>> return *(*T)(unsafe.Pointer(&ptr))
>> }
>>
>> func main() {
>> var s C.StatusOr
>> p := PointerOrError[*C.mystruct](s)
>> _ = p
>> }
>>
>> It's unfortunate, of course, that this would allow you to instantiate the 
>> function using a non-pointer as well, but it seems that's impossible to 
>> prevent statically? You can catch it dynamically by doing something akin to
>>
>> if k := reflect.TypeOf(*new(T)).Kind(); k != reflect.Pointer && k != 
>> reflect.UnsafePointer {
>> panic("PointerOrError must be instantiated with pointer type")
>> }
>>
>> Obviously, none of this is ideal, but maybe it's the best you can do - 
>> apart from generating code.
>>
>> On Sat, Apr 15, 2023 at 8:48 PM Jan  wrote:
>>
>>> Re-factoring your example to use CGO, in a small main.go file:
>>>
>>> ```
>>> $ go run .
>>> ./main.go:28:10: cannot use incomplete (or unallocatable) type as a type 
>>> argument: main._Ctype_struct_MyThing
>>> $ cat main.go 
>>> package main
>>>
>>> // struct MyThing;
>>> // typedef struct MyThing MyThing;
>>> import "C"
>>> import (
>>> "fmt"
>>> "unsafe"
>>> )
>>> import "flag"
>>>
>>> func PointerOrError[T *Q, Q any](s int) (t T, err error) {
>>> var ptr unsafe.Pointer
>>> ptr, err = UnsafePointerOrError(s) // <-- unsafe.Pointer, error
>>> if err != nil {
>>> return
>>> }
>>> t = (T)(ptr)
>>> return
>>> }
>>>
>>> func UnsafePointerOrError(v int) (unsafe.Pointer, error) {
>>> return unsafe.Pointer(&v), nil
>>> }
>>>
>>> func main() {
>>> flag.Parse()
>>> t, _ := PointerOrError[*C.MyThing](1)
>>> fmt.Println(t)
>>> }
>>> ```
>>>
>>>
>>> On Saturday, April 15, 2023 at 8:41:28 PM UTC+2 Jan wrote:
>>>
 Thanks! I hadn't realized that one could constraint T to be a pointer 
 type by using a second type paramater Q, this in nice.

 But alas, it doesn't work. When I copy&pasted your code to mine, and 
 used an undefined C type ... it complained of the same thing:

 ```
 cannot use incomplete (or unallocatable) type as a type argument: 
 gomlx/xla._Ctype_struct_StableHLOHolder
 ```

 I'm using it in the following snipped of code:

 ```
 func (comp *Computation) ToStableHLO() (*StableHLO, error) {
 if comp.IsNil() || comp.firstError != nil {
 return nil, errors.Errorf("Computation graph is nil!?")
 }
 statusOr := C.ConvertComputationToStableHLO(comp.cCompPtr)
 cPtr, err := PointerOrError[*C.StableHLOHolder](statusOr)
 if err != nil {
 return nil, errors.Wrapf(err, "failed conversion in 
 Computation.ToStableHLO")
 }
 return NewSta