Re: [go-nuts] Generic arguments in recursive functions

2022-04-10 Thread Aidan Hahn
Hello Ian,

I think some of your confusion may be because the function is poorly named. 
In some cases the Node generic may store another Node in the generic Inner 
field.
In this case I want my preorder traversal to also traverse the list in the 
Inner field. "TreePreTraverse" would be a better name.

func (n *Node[any]) TreePreTraverse(f func(*Node[any])) {
tmpN := interface{}(n)
if tmpN, ok := tmpN.(*Node[*Node[any]]); ok {
tmpN.Inner.TreePreTraverse(f)
} else {
f(n)
}

if n.Next != nil {
n.Next.TreePreTraverse(f)
}
}

Additionally, I notice that you set the "Next" field to *Node[T] mirroring 
the type of the parent Node, but part of my goal with this exercise is to 
be able to have a list of elements of varying type. If I set the generic 
type for the Next field to 'T' then the list has to be homogeneous.

Ignoring the weird corner case, I am just wondering why f, unchanged, 
cannot be passed into a recursion of the function that it is an argument to.

Thanks,
Ava
On Saturday, April 9, 2022 at 9:13:31 PM UTC-7 Ian Lance Taylor wrote:

> On Sat, Apr 9, 2022 at 8:28 PM Aidan Hahn  wrote:
> >
> > I am playing with the generics that were added in go 1.18 and noticed an 
> odd discrepancy that I have not been able to find an authoritative answer 
> to. Consider the following data type, as well as the function defined for 
> it:
> >
> > type Node[T any] struct {
> > Inner T
> > Next *Node[any]
> > }
> >
> > func (n *Node[any]) ListPreTraverse(f func(*Node[any])) {
> > tmpN := interface{}(n)
> > if tmpN, ok := tmpN.(*Node[*Node[any]]); ok {
> > tmpN.Inner.ListPreTraverse(f)
> > } else {
> > f(n)
> > }
> > n.Next.ListPreTraverse(f)
> > }
> >
> > This function is a simple attempt to iterate across a generic linked 
> list and call a function on each node in the list (although in this case it 
> may behave more like a tree if inner stores another node). When I went to 
> compile this code I encountered the following error:
> >
> > test.go:64:25: cannot use f (variable of type func(*Node[any])) as type 
> func(*Node[any]) in argument to n.Next.ListPreTraverse
> >
> > To me at least it seems that all the information to validate that the 
> argument fits the type specified in the function call is there, defined in 
> the function header. What am I missing?
>
> The error message is confusing because the "any" used in
> ListPreTraverse is shadowing the "any" used in Node. See
> https://go.dev/doc/faq#types_in_method_declaration. You want to write
>
> func (n *Node[T]) ListPreTraverse(f func(*Node[T])) {
>
> But then you will see that your code has an instantiation cycle,
> violating the restriction described at
>
> https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md#generic-types
> :
> "A generic type can refer to itself in cases where a type can
> ordinarily refer to itself, but when it does so the type arguments
> must be the type parameters, listed in the same order. This
> restriction prevents infinite recursion of type instantiation."
>
> I don't really understand what you are trying to do here. The type
> assertion doesn't make sense to me. Perhaps this is the code you
> want:
>
> type Node[T any] struct {
> Inner T
> Next *Node[T]
> }
>
> func (n *Node[T]) ListPreTraverse(f func(*Node[T])) {
> f(n)
> if n.Next != nil {
> n.Next.ListPreTraverse(f)
> }
> }
>
> Ian
>

-- 
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/019beabf-a1f5-4aeb-96d4-c121ae3c2252n%40googlegroups.com.


[go-nuts] Generic arguments in recursive functions

2022-04-09 Thread Aidan Hahn
Hello Go users!

I am playing with the generics that were added in go 1.18 and noticed an 
odd discrepancy that I have not been able to find an authoritative answer 
to. Consider the following data type, as well as the function defined for 
it:

type Node[T any] struct {
Inner T
Next  *Node[any]
}

func (n *Node[any]) ListPreTraverse(f func(*Node[any])) {
tmpN := interface{}(n)
if tmpN, ok := tmpN.(*Node[*Node[any]]); ok {
tmpN.Inner.ListPreTraverse(f)
} else {
f(n)
}
n.Next.ListPreTraverse(f)
}

This function is a simple attempt to iterate across a generic linked list 
and call a function on each node in the list (although in this case it may 
behave more like a tree if inner stores another node). When I went to 
compile this code I encountered the following error:

test.go:64:25: cannot use f (variable of type func(*Node[any])) as type 
func(*Node[any]) in argument to n.Next.ListPreTraverse

To me at least it seems that all the information to validate that the 
argument fits the type specified in the function call is there, defined in 
the function header. What am I missing?

Thanks,
Ava

-- 
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/951bc5d6-6194-4cfa-905d-66f7f0059a65n%40googlegroups.com.


[go-nuts] Import a submodule from its parent package?

2020-12-27 Thread Aidan Hahn
Hello,
I am writing a modification to the os package in which I would like to take 
advantage of code from the os/signal package. Ive written the patch 
(modified os/exec_posix.go) and as I expected none of the symbols from 
os/signal are defined in the os package. My question is how I can import 
them?

I have tried the following: 
- import "os/signal": caused the build to hang, presumably due to circular 
imports
- import "./signal": the import path included the vendor folder
- no import: none of the symbols were defined (Notify, Reset, and SIGTTOU)

Sorry if I have overlooked something obvious here, but I do not see a 
solution to importing code from a subpackage into the parent package.

Thanks,
Aidan

-- 
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/342bc175-4d5b-4888-8e68-b2ffbbe39f9en%40googlegroups.com.


[go-nuts] Making syscalls in the syscall package

2020-11-17 Thread Aidan Hahn
Good Morning/Afternoon/Evening,

I am currently patching my local go runtime to fix a bug it has. I would 
like to make the signal() syscall, or at least the sigaction() syscall from 
inside the syscall package but it looks like those functions are defined as 
private within the runtime package. 

I see that both SYS_SIGACTION and SYS_SIGNAL are defined in the 
zsysnum_{os}_{arch}.go files, and it looks like these enumerations are 
meant to be used with the RawSyscall, RawSyscall6, or rawSyscallNoError 
functions.

My question is: how does a call to RawSyscall(SYS_SIGNAL, .) translate 
to a call to signal(signum, handler)? Do I add the usual arguments to 
signal to RawSyscall and then ignore the rest?

Thanks,
Aidan

-- 
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/7c8acd74-a4fb-4e79-ac3a-c6a09421b8cdn%40googlegroups.com.