Hacker News new | ask | show | jobs
In Go, I'm going to avoid using 'any' as an actual type (utcc.utoronto.ca)
2 points by teichmann 868 days ago
2 comments

Don't "go" that far!

Jokes aside tho, I agree—the "anything goes" (pun intended) approach in dynamic programming languages is so annoying. Using "any" in a typed language is like "go"ing back to dynamic style, which many already are trying to escape.

Enough puns for today!

Is there a difference in how or whether 'any' and 'interface{}' preserve static type information?

i.e., if I implement `identity(a) = a` in a generic way, can I still call identity(duck).quack()?

yes

  // You can edit this code!
  // Click here and start typing.
  package main

  import "fmt"

  func main() {
   duck := Duck{}

   identity(duck).quack()
  }

  func identity[T any](t T) T {
   return t
  }

  type Duck struct{}

  func (d Duck) quack() {
   fmt.Println("quack")
  }

  /*
  quack

  Program exited.
  */