Go’s Type System Is An Embarrassment

Go is one of the best tools out there today for heavy lifting and backend code. It’s my go to language when it’s time to bring out the big guns and I enjoy working with it immensely. That being said, its type system is an embarrassment. It’s meant to be clear and comprehensible yet it’s full of awkward surprises.

Lack Of Generics

Most modern type systems have some notion of generics. Generics allow types to interact with other types in a type-safe manner. Go has no support for generics. It’s possible to live without generics by using type assertions (casting) and empty interfaces but these are not checked at compile time. Some built-in functions of Go work with pseudo-generic types (e.g. len(), append()) so the language designers are clearly aware of the problem, and yet this pseudo-generic functionality is not exposed to regular users of the language. The compiler and the type system are there to help the developer; having to cast types back and forth in a statically typed language is just embarrassing.

Confusing Semantics Of Exported Identifiers

Many modern languages provide some mechanism to hide methods and data internal to various compilation units or data structures. This is a good thing; it makes programs more reliable by limiting interfaces exposed by modules and by abstracting away implementation details.

Most programming languages allow this by having a mechanism to expose or hide functions, data fields, and types. Go does this with a twist: it exports the lexical identifier, not program element (e.g. type or function) the identifier represents. As a consequence, if there are no identifiers involved, unexported program elements can leak into other packages. What does this mean? Consider the following example:

package p1

type hidden struct {
    Field int
}

func NewHidden() hidden {
    return hidden{Field:1337}
} 

package main

import (
    "fmt"
    "p1"
)

func main () {
    myHidden := p1.NewHidden()
    fmt.Println(myHidden.Field)
    myHidden.Field = 9000
    fmt.Println(myHidden.Field)
}

This example will compile and work. Go will have absolutely no problem with this as it’s not the “hidden” type that is unexported, only its identifier. Since the:=operator infers the type of the variable on its left side from the expression on the its right, there are no unexported identifiers involved. People on the golang-nuts mailing list seem to think that this makes the type system “easier to understand and explain” but in my opinion these sort of unintended consequences only make things worse.

In addition, the reflect package does not allow reading or writing struct fields with unexported identifiers (even if the field was reached without using any identifiers) which seems to completely contradict the philosophy of exporting being a strictly lexical concept.

The Verdict

If we were still living in the ’90s or Golang was a hobbyist or experimental programming language, these shortcomings would be acceptable. The year is 2014 however and Golang is heavily backed by Google itself so there is simply no other way to say it: Go’s type system is an embarrassment.

I don’t believe that the lexical exporting issue will ever be resolved, partly because it appears to be an inherent part of the language and partly because I don’t think the designers of the language will ever admit it’s an issue. On the other hand, I’m almost certain that generics will be added at some point, hopefully sooner rather than later. That will make things better.

时间: 2025-01-13 16:25:54

Go’s Type System Is An Embarrassment的相关文章

Beginning Scala study note(8) Scala Type System

1. Unified Type System Scala has a unified type system, enclosed by the type Any at the top of the hierarchy and the type Nothing at the bottom of the hierarchy. All Scala types inherit from Any. # Using Any, Book extends AnyRef, and x is an Int that

Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' 问题描述: 再windows server 2008 r2 环境下搭建.net网站 ,运行是IIS7.0提示以上问题 解决方案: 这里需要注册一下ASP.NET 4.

Could not load type 'System.Web.Mvc.ViewPage<dynamic>' in asp.net mvc2 after publishing the website

在WebConfig里 找到 <pages></pages> <pages pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" pageBaseType="System.Web.Mvc.ViewPage, System

解决:Could not load type &#39;System.ServiceModel.Activation.HttpModule&#39; from assemb

解决:Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.ServiceMode 今天我发布项目,获取运行出错如下: Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.ServiceMode 解决:Could not load type 'System.Se

使用Ef查询出现的问题The cast to value type &#39;System.Boolean&#39; failed because the materialized value is null.的解决方法

把值类型的系统.布尔的失败是因为物化值是null.结果类型的泛型参数或查询必须使用可空类型. 解决方法: 请确保你查询中的字段值不为空或者做为空判断 使用Ef查询出现的问题The cast to value type 'System.Boolean' failed because the materialized value is null.的解决方法

Could not load type ‘System.ServiceModel.Activation.HttpModule’ from&amp;

1. 部署网站到IIS7.5,Window 2008的时候出现这个错误 2. 错误信息 Server Error in ‘/’ Application. Could not load type ‘System.ServiceModel.Activation.HttpModule’ from assembly ‘System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′. 3. 解决

(C# Debug)A first chance exception of type &#39;System.ArgumentException&#39; occurred in System.Data.dll

Debug 模式下运行程序的时候,Output 窗口出来个错误“A first chance exception of type 'System.ArgumentException' occurred in System.Data.dll”. 但是并没有直接throw错误.无法知道具体在哪一步发生了这个错误. 如果想知道具体的内容,需要enable 这个debug If you do want to know, in Visual Studio -> Debug (main menu) ->

JsonException: Max allowed object depth reached while trying to export from type System.Single

在进行类转json字符串时,报错JsonException: Max allowed object depth reached while trying to export from type System.Single. ok,实际上是类的属性中有json不能识别的数据类型,我这里就脑残的float.去除掉之后就ok了. (备注:用的是LitJson) 那一般什么类型是允许的呢? 我在JsonData类中找到了答案. public JsonData(bool boolean); public

[Hive-Tutorial] Type System 数据类型

数据类型Type System Hive supports primitive and complex data types, as described below. See Hive Data Types for additional information. Hive支持原生和复杂数据类型. Primitive Types 原生数据类型 Types are associated with the columns in the tables. The following Primitive t