FAQ
I recently read up on Microsofts Typescript and there was one simple feature that really intrigued me:
Interfaces in Typescript do not only support functions but also values.

You can actually declare an interface like this:

interface point {
      number x;
      number y;
}

This is very much like an abstract class and feels rather natural.

I think that interfaces like these could be a neat way to implement the functionality of generics.

Is there a reason go interfaces only support methods ? Could this feature be added in the future ?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Dave Cheney at Dec 10, 2013 at 1:26 am

    On Tue, Dec 10, 2013 at 9:15 AM, wrote:
    I recently read up on Microsofts Typescript and there was one simple feature that really intrigued me:
    Interfaces in Typescript do not only support functions but also values.

    You can actually declare an interface like this:

    interface point {
    number x;
    number y;
    }

    This is very much like an abstract class and feels rather natural.
    How is that different to

    type point struct {
            x, y int
    }
    I think that interfaces like these could be a neat way to implement the functionality of generics.

    Is there a reason go interfaces only support methods ?
    Interfaces in Go define behaviour, they are a contract between the
    caller and the implementation that specify nothing about the
    implementation.
    Could this feature be added in the future ?
    No, not in the foreseeable future.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Kevin Gillette at Dec 10, 2013 at 3:08 am
    What you're describing there is data, not behavior -- interfaces are about
    behaviors. Part of the benefit dynamic languages (such as JavaScript or
    JavaScript-based languages) can get from allowing properties to be
    specified in interfaces is that in such languages, method/function calls
    can be disguised as simple property accesses. Go doesn't have
    implicit/disguised calls, and most likely never will (the Go designers
    would likely consider it to be an anti-feature).

    If you really want a data-as-interface notion, just have your functions
    accept a Point, where Point is:

    type Point struct { X, Y float64 }

    Callers don't have to "be" a Point, they only need to construct one, which
    is trivial. It also eliminates any potential semantic mismatches between
    Point, and, say:

    type LetterFrequencies struct {
        A float64
        // ...
        X float64
        Y float64
        Z float64
    }

    Here, LetterFrequencies would "implement" your hypothetical point
    interface, but certainly is not a point!
    On Monday, December 9, 2013 3:15:53 PM UTC-7, privatl...@gmail.com wrote:

    I recently read up on Microsofts Typescript and there was one simple
    feature that really intrigued me:
    Interfaces in Typescript do not only support functions but also values.

    You can actually declare an interface like this:

    interface point {
    number x;
    number y;
    }

    This is very much like an abstract class and feels rather natural.

    I think that interfaces like these could be a neat way to implement the
    functionality of generics.

    Is there a reason go interfaces only support methods ? Could this feature
    be added in the future ?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Jesse McNelis at Dec 10, 2013 at 3:09 am

    On Tue, Dec 10, 2013 at 9:15 AM, wrote:
    I recently read up on Microsofts Typescript and there was one simple feature that really intrigued me:
    Interfaces in Typescript do not only support functions but also values.

    You can actually declare an interface like this:

    interface point {
    number x;
    number y;
    }
    The equivalent in Go is.

    type point interface {
         x() int
         y() int
    }

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 10, '13 at 1:22a
activeDec 10, '13 at 3:09a
posts4
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase