albrow / fo

An experimental language which adds functional programming features to Go.
Other
1.24k stars 34 forks source link

Certain recursive types causing stack overflow errors #15

Closed albrow closed 6 years ago

albrow commented 6 years ago

This is copied over from #3 since it is a separate issue.

Here's a similar recursive type which panics when I try to compile.

package main

type A[T] struct {
  Next *A[T]
}

func main() {
  var a A[int]
  a.Next = &a
}
mandolyte commented 6 years ago

I think I have a similar example. Hope it helps. Here is the code:

package main

func main() {
    lli := &LinkedList[int]
    lli.InsertFirst[int](1)
}

type Node[T] struct {
    data T
    next *Node[T]
    prev *Node[T]
}

type LinkedList[T] struct {
    head *Node[T]
    tail *Node[T]
}

func (list *LinkedList[T]) InsertFirst(i T) {
    data := &Node[T]{data: i}
    if list.head != nil {
        list.head.prev = data
        data.next = list.head
    }
    list.head = data
}
albrow commented 6 years ago

This is fixed in 02a4bdb70ec696af52a10d8bc71cb8ad6fa45dd3. I also added a linked list implementation which uses recursive generic types to the examples/ directory.