LHS inference for type assertions (Pre proposal discussion)

105 views
Skip to first unread message

Dexter Plameras

unread,
May 20, 2023, 4:17:50 PM5/20/23
to golang-nuts
Greetings,

As per the requirement before putting together a proposal, a discussion on LHS inference for type assertions

Currently, type assertions take the form of:

//
// Pre assertion
//
var original string = "Hello Type Assertions"
var data interface{} = original // or "var data any"

//
// Assertions
// 

// fails compiler checks
destInt := data.(int)

// passes compiler checks
destString := data.(string)

Proposal:
Instead of a RHS type assertion, I'd like to propose a language change that would take the type assertion as an inference from the LHS, potentially using a new assignment operator.

//
// Pre assertion
//
original := "Hello Type Assertions"
var data interface{} = original  // or var data any

// LHS inference of the int type. Compiler checks fail. (notice the ".=" assignment operator, it could be variation of the assignment operator that makes it clearer)
var destInt int .= data

// LHS inference of the string type.
var destString string .= data
// or
var destString string
destString .= data

This could potentially work with multiple RHS returns too.
var data string
var err error
data, err .= function()

I'm certainly no programming language expert but does this hold any value?

Ian Lance Taylor

unread,
May 20, 2023, 5:23:22 PM5/20/23
to Dexter Plameras, golang-nuts
You didn't explain why the new syntax is better. It doesn't seem any
clearer to me, so I don't see what we gain. Thanks.

Ian

Dexter Plameras

unread,
May 20, 2023, 11:51:55 PM5/20/23
to golang-nuts
My apologies,

So as to not waste your time or anybody else's, I'll follow the proposal headings in the (proposal/.../TEMPLATE.md), and post here.
Reply all
Reply to author
Forward
0 new messages