goat icon indicating copy to clipboard operation
goat copied to clipboard

simpler type declarations + safe system for sub-types

Open PaperPrototype opened this issue 3 years ago • 4 comments

Image the following code:

type Person {
    public string Name
}

type Zombie extends Person {
    public int32 InfectionAmount
}

interface IName {
    public func GetName()
}

With C you can implement a very simple form of plymorphism by embedding a struct Position atop say another struct called Transform. With this you can now cast the Transform type directly to the Position type, and get the position type out of it.

It would allow a form of single inheritance, and is fast and very type safe.

Interface's can still be implemented as before, although I'm not sure if structs store a pointer to their member functions? I know a C interface is a struct of function pointers. Not sure how Golang does this.

PaperPrototype avatar Nov 06 '22 22:11 PaperPrototype

Can you explain the idea again? I got lost between the two examples 😵 A full code example will be best I think.

As a side note I'd say....I'm not sure i'm ready to go into inheritance. Go diverted from it on purpose, as much as i miss it at points, dealing with all the edge cases that arise from inheritance might be a bit too big to handle at this point. WDYT?

avivcarmis avatar Nov 08 '22 18:11 avivcarmis

Can you explain the idea again? I got lost between the two examples 😵

type Person {
    public string Name
}

type Zombie extends Person {
    public int32 InfectionAmount
}

interface IName {
    public func GetName()
}

if @typeof(Person) == @typeof(Zombie) {
    mut zombie = Zombie{
       Name: "Abdiel"
       infectionAmount: 12
    }
    
    mut person = Person(Zombie)
}

This is also using the new const and mut syntax from issue #3

As a side note I'd say....I'm not sure i'm ready to go into inheritance. Go diverted from it on purpose, as much as i miss it at points, dealing with all the edge cases that arise from inheritance might be a bit too big to handle at this point. WDYT?

By "inheritance" I mean literally the 0s and 1s layout of the structs would let us do a hard cast from one type to the other (as seen in above example code).

To put it visually

zombie
[Name, infectionAmount]

person = zombie
[Name] ~~~infectionAmount~~~

Although this would only be possible if goat ensured no field reordering could happen as mentioned in the C ABI issue #12

PaperPrototype avatar Dec 09 '22 01:12 PaperPrototype

Just so I understand, if goat is to automatically translate goat files to go files - can you provide an example go file produced from the following goat file:

type Person {
    public string Name
}

type Zombie extends Person {
    public int32 InfectionAmount
}

interface IName {
    public func GetName()
}

if @typeof(Person) == @typeof(Zombie) {
    mut zombie = Zombie{
       Name: "Abdiel"
       infectionAmount: 12
    }
    
    mut person = Person(Zombie)
}

avivcarmis avatar Dec 10 '22 09:12 avivcarmis

This reply is long overdo

package main

import (
    "fmt"
    "reflect"
)

type Person {
    public string Name
}

type Zombie {
    public Person Person
    public int32 InfectionAmount
}

interface IName {
    public func GetName()
}

// I this would be possible, but for now I'd say it reduces to compile time
/* if @typeof(Person) == @typeof(Zombie) */
var zombie = Zombie{
    Person: Person {
        Name: "Abdiel"
    }
    infectionAmount: 12
}

var person = zombie.person

PaperPrototype avatar Nov 02 '23 17:11 PaperPrototype