Giter Site home page Giter Site logo

go-plus-deprecated's Introduction

Go Plus

THIS IS A WORKING IN PROGRESS DRAFT SPECIFICATION

Go Plus is designed to be a strict super-set of the Go programming language, which means:

  • All valid Go code is valid Go Plus code

Go Plus works by translating Go Plus code into Go code, then compile it with standard Go compiler.

Ternary operator

func Choose(c bool, v1, v2 int) int {
    return c ? v1 : v2
}

Explicit access specifiers (not implemented for now)

type SomeStruct struct {
    private ValueA      int // will be translated to `valueA`
    private valueB      int // will be kept as is
    public  someValue   int // will be translated to `SomeValue`
    public  PublicValue int // will be kept as is

    // public SomeValue int // illegal: duplicated with `someValue`
    // private valueA   int // illegal: duplicated with `ValueA`

    // Golang style access specifications are also supported
    OldStylePublicValue     int 
    oldStylePrivateValue    int
}

// the rules also apply to methods
public func (self SomeStruct) publicMethod() {}
private func (self SomeStruct) privateMethod() {}

// and types
private type InternalObject struct {
    // ...
}

// and global variables and consts
public var exportedValue int = 100
private const INTERNAL_CONST int = 10000

Interface implementation validation (not implemented for now)

There is no way to enfore explicit interface implementation, cause that breaks existing code.

The best thing we can do is to provide an optional mechanism to ensure a certain struct implements certain interfaces.

type ISomething interface {
    DoSomething()
}

// compiler ensures that `Something` must implement `ISomething`
type Something struct implements ISomething {
    // ...
}

func (Something) DoSomething() {
    println("hello, world")
}

// you can specify multiple interfaces at once
// the order is irrelevant
type OtherThing struct implements ISomething, error {
    // ...
}

func (OtherThing) DoSomething() {}
func (OtherThing) Error() string { return "" }

// illegal: `OtherThing` implements unspecified interface `io.Closer`
// func (OtherThing) Close() error { 
//     return nil
// }

// illegal: `CustomError` does not implement `error` interface
// type CustomError struct implements error {
//     // ...
// }

Exception

type SomeError struct {
    msg string
}

func (self SomeError) Error() string {
    return self.msg
}

func SomeFunction() {
    try {
        throw SomeError("whatever implements the error interface")
    } except (e error) {
        println("catch every error")
    } finally {
        println("do your cleanup")
    }
}

C++ style template class

template <T type>
type Stack struct {
    private sp    int
    private stack []T
}

public func (self Stack) push(val T) {
    // whatever pushes
}

// template placeholder can be primitive types
template <maxSize int, T type>
type LRUCache struct {
    private cache [maxSize]T
}

C++ style template function

template <T type>
func GetMax(v1, v2 T) T {
    return v1 > v2 ? v1 : v2
}

C++ style polymorphism

It is very hard to implement multi-inheritance properly, so we decide not to implement it. Single inheritance is good enough for most cases.

Java doesn't support multiple inheritance as well, yet it's still one of the world's most successful programming languages.

type BaseAction class {
    // will generate a virtual table when a class
    // have at least 1 method that marked as `virtual`
}

// a non-virtual method, cannot be overridden
public func (self BaseAction) whatever() {
    // ...
}

// a virtual method that can be overridden
public virtual func (self BaseAction) init() {
    // ...
}

// a pure-virtual method without method body
public virtual func (self BaseAction) perform()

type SayHelloAction class extends BaseAction {
    // ...
}

// implements `BaseAction.perform`
public override func (self SayHelloAction) perform() {
    println("hello, world")
    self.BaseAction.init()       // call super class methods
    // self.BaseAction.perform() // illegal: pure-virtual method call 
}

// this throws a warning because it hides the method in super class
public func (self SayHelloAction) whatever() {
    // ...
}

// illegal: method `whatever` overrides nothing
// public override func (self SayHelloAction) whatever() {
//     // ...
// }

Python-style function decorators

func LoginRequired(handler func()) func() {
    return func() {
        // do login check, etc
        handler()
    }
}

@LoginRequired
public func RequestHandler() {
    // ...
}

// illegal: invalid function signature
// @LoginRequired
// public func InvalidFunc(a int) {
//     // ...
// }

go-plus-deprecated's People

Contributors

chenzhuoyu avatar

Stargazers

zou shengfu avatar zhao cheng avatar yanwei avatar AN Long avatar 尹吉峰 avatar  avatar zhjpaul avatar  avatar Cl avatar  avatar Dennis Xie avatar  avatar Latham Zhao avatar lsq avatar  avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

p79n6a chenrae

go-plus-deprecated's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.