News for hackers. Stuff that matters.
Re:Option and null in dynamic languages by NateDad on Tuesday December 09, 2014 @01:12PM (#8724492)
Go doesn't have tuples because they aren't very compatible with compile-time type safety. You could using []interface{} as a tuple, but why would you?

Go does have anonymous structs, they're just only of moderate use in a statically typed language (given that you need to specify the types for function arguments and return types)

You can do something like this:

    // make a list of anonymous structs 
    // often useful for table driven tests
    tests := []struct{ Name string, count int }{
        {
            Name: "First test", Count: 5
        },
        {
            Name: "Second test", Count: 7
        }
    }
    
    for _, test := range tests {
        if test.Count > 5 {
            t.Errorf("%s: expected <= 5, got %d", test.Name, test.Count)
        }
    }
Re:Option and null in dynamic languages by dragonwriter on Wednesday December 10, 2014 @03:34PM (#8731513)
> Go doesn't have tuples because they aren't very compatible with compile-time type safety.

That's...amusing. ML-family languages use tuples extensively (Haskell, because of its preference for currying, somewhat less than others), and yet are light years ahead of Go when it comes to compile-time type safety.

Re:Option and null in dynamic languages by NateDad on Friday December 12, 2014 @10:22AM (#8740460)
Sorry, you're right. I was thinking of tuples without defined types for the slots, like "this function returns a tuple of two objects", not "this function returns a tuple of (string, int)".
Re:Option and null in dynamic languages by Dewie on Tuesday December 09, 2014 @04:31PM (#8725916)
> Go doesn't have tuples because they aren't very compatible with compile-time type safety.

What's the problem? Couldn't you have something like:

    (string, int)
For example in the return type of a function? I.e., this function returns a tuple of string and int.
Re:Option and null in dynamic languages by NateDad on Wednesday December 10, 2014 @03:20PM (#8731428)
What's the difference between that and multiple returns?

You can have

func foo() (string, int)

func bar(a string, b int)

And do bar(foo())

The only thing you couldn't do is make a slice of tuples... But you can make a slice of structs that have just a string and an int.

I guess I just don't see a huge amount of utility that isn't otherwise already covered.

Re:Option and null in dynamic languages by Dewie on Wednesday December 10, 2014 @04:36PM (#8731859)
> What's the difference between that and multiple returns?

It's a more general concept than multiple returns. Why special-case something, needlessly?

Re:Option and null in dynamic languages by NateDad on Friday December 12, 2014 @10:18AM (#8740430)
A tuple appears to be a special case of a struct. Not sure why you need (int, bool) when you can have struct{i int, b bool}... the tuple you have to access via index, and the struct you access via name... and by name is much more clear in your code.
Re:Option and null in dynamic languages by Dewie on Friday December 12, 2014 @01:56PM (#8741975)
> Not sure why you need (int, bool) when you can have struct{i int, b bool}...

You tell me: you for some reason do need (or want) multiple returns. Why, when you can just use a struct? ;)

> the tuple you have to access via index, and the struct you access via name... and by name is much more clear in your code.

I guess Go doesn't have destructing (like - `(x, y) := some_fun()`). In that case, I can see how it could be more annoying than the multiple return thing. I do not see, however, how tuples necessitate indexing - in the languages I've used with tuples, the arity is known at compile time, and you have to use certain functions to access the first, second, etc. But if the choice is between multiple returns (which I guess is just two values?) and tuples, then I don't see why the common case (2-arity) can't be easily supported.