goatlang / goat

Extended flavor of the Go programming language, aiming for increased value safety and maintainability
MIT License
63 stars 1 forks source link

simpler type declarations + safe system for sub-types #14

Open PaperPrototype opened 1 year ago

PaperPrototype commented 1 year ago

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.

avivcarmis commented 1 year ago

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?

PaperPrototype commented 1 year ago

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

avivcarmis commented 1 year ago

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)
}
PaperPrototype commented 9 months ago

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