3 ms·
Pretty interesting. It is scary to make your "learn a new language" task to port 14,000 lines of code, but with that in mind, this all seems to have gone well.
by jrockway 7y ago
Pretty interesting. It is scary to make your "learn a new language" task to port 14,000 lines of code, but with that in mind, this all seems to have gone well. Some random thoughts:
> I had to write my own set-of-int and
set-of-string classes
map[int]struct{}, map[string]struct{}
ints[42] = struct{}{} // insert
delete(ints, 42) // delete
for i := range ints { ... } // iterate
if _, ok := ints[42]; ok { ... } // exists?
> Catchable exceptions require silly contortions
I am not sure why go has panic/recover, but it's not something to use. panic means "programming error", recover means "well, the code is broken but I'm just a humble generic web framework and I guess maybe the next request won't be broken, so let's keep running". It is absolutely not for things like "timeout waiting for webserver" or "no rows in the database" as other languages use exceptions for. For those, you return an error and either wrap it with fmt.Errorf("waiting for webserver: %w", err) or check it with errors.Is and move on. Yup, you have to remember to do that or your program will run weirdly. It's just how it is. There is not something better that maybe with some experimentation you will figure out. You have to just do the tedious, boring, and simple thing.
I have used recover exactly once in my career. I wrote a program that accepted mini-programs (more like rules) via a config file that could be reloaded at runtime. We tried to prove them safe, but recover was there so that we could disable the faulty rule and keep running in the event some sort of null pointer snuck in. (I don't think one ever did!)
> Pass-by-reference vs. pass-by-value
I feel like the author wants []*Type instead of []Type here.
> Absence of sum/discriminated-union types
True. Depending on what your goals are, there are many possibilities:
type IntOrString struct { i int; s string; iok, sok bool }
func (i IntOrString) String() (string, error) { if i.sok { return i.s, nil } else { return "", errors.New("is not a string") }}
func NewInt(x int) IntOrString { return IntOrString{i: x, iok: true} }
...
This uses more memory than interface{}, but it's also very clear what you intend for this thing to be.
I will also point out that switch can bind the value for you:
switch x := foo.(type) {
case int:
return x + 1
case string:
i, err := strconv.Atoi(x)
return i + 1
}
And that you need not name types merely to call methods on them:
if x, ok := foo.(interface { IntValue() int }); ok { return x.IntValue() }
You can also go crazy like the proto compiler does for implementations of "oneof" and have infinite flexibility. It is not very ergonomic, but it is reliable.
> Keyword arguments
type Point struct { X, Y float64 }
func EuclideanDistance(a, b Point) float64 { ... }
EuclideanDistance(Point{X: 1, Y: 2}, Point{3, 4})
> No map over slices
This one is like returning errors. You will press a lot of buttons on your keyboard. It is how it is.
I personally hate typing the average "simple" for loop:
func fooToInterface(foos []foo) []interface{} {
var result []interface{}
for _, f := range foos {
result = append(result, f)
}
return result
}
But it's also not that hard. I used to be a Python readability reviewer at Google. I always had the hardest time reading people's very aggressive list comprehensions. It was like they HAD to get their entire program into one line of code, or people wouldn't think they were smart. The result was that the line became a black box; nobody would read it, it was just assumed to work.
I really like seeing the word "for" twice when you're iterating over two things.
- pdonis 7y ago> I really like seeing the word "for" twice when you're iterating over two things. You will if you write a Python list or generator comprehension that iterates over two things, won't you?
- dboreham 7y agoMuch of what passes for wisdom in this field is just people's way of feeling good about themselves because they're convinced they're smarter than others. The list comprehension or whatever it is is just a construct to enable that thought pattern.
- zbentley 7y agoI feel about list comprehensions the way I feel about regular expressions. Below a certain point of complexity, both are vastly superior ways of expressing what's going on. Above that point, comprehensibility drops off fast, and they immediately become inferior tools. For example, something like: [some_func(y) for x, y in some_dict.items() if some_condition(x)] ...is, at least in my mind, eminently more readable than the imperative equivalent, and less PEBCAK-risky (i.e. what if you have to do two similar iterations and forget to use a different accumulator between the two?). However, I totally agree with you re: "don't get too clever". Pretty much the instant you have nested comprehensions, or try to get clever iterating over multiple data structures in a single expression, it immediately becomes much worse than the imperative form, and you should feel a little bashful and bust out some loops, functions, and accumulators. Same is true for regex. Something like: ($match) =~ qr/\A(?:foo|bar)[.]com (\d+)-baz$/ ...while it does require you to know regex, is way simpler and more robust than writing the equivalent stack of many string slicing conditions, and less error prone. However, once the "too clever" rubicon is crossed (subjective, but my personal rule of thumb is: more than 50chrs of non-literals or more than one lookaround expression), like list comprehensions, it rapidly becomes much harder to understand than the equivalent long, explicit, string-slicing form. As with many things, I think that skill in these areas is a matter of knowing when to stop.
- strictusername 7y ago> It was like they HAD to get their entire program into one line of code, or people wouldn't think they were smart. Lol. Call sign of a junior developer...