3 ms·
That depends on the language, I love flatMap, but I never want to see functions attached to the integer type. Just stop with that :) Principle of least astoni
by jbooth 10y ago
That depends on the language, I love flatMap, but I never want to see functions attached to the integer type. Just stop with that :) Principle of least astonishment and all.
- gnaritas 10y agoAn integer is an object, objects have methods, such methods are standard fare; if it's astonishing, try more languages. This is the idiomatic way to do this for example in Smalltalk. 10 timesRepeat: [ self doStuff ] What's relevant isn't whether you're astonished or not, but whether it's idiomatic or not. If you can't see an integer as an object with methods, then you haven't fully grokked OO.
- jbooth 10y agoI've tried lots of languages, thanks. It's still astonishing and will always be astonishing. WTF does "timesRepeat" have to do with what an integer represents? Nothing, that's what. Integer is a noun, conventionally a 32-bit value type, while "timesRepeat" is a control structure. It's absolutely crazy for "timesRepeat" to be a member of integer. http://thecodelesscode.com/case/223 http://thecodelesscode.com/case/223
- gnaritas 10y agoYou're completely ignoring what I said, idomatic; this is how it's normally done in Smalltalk, and it's absolutely idiomatic to hang control structures off objects since that's how the entire language is implemented from top to bottom, it's not crazy at all, it's good design that keeps the language itself minimal. As for what #timesRepeat: has to do with Integer, I'd think that's pretty obvious, an object should be able to enumerate itself, to write a loop where you inc a counter is procedural, not object oriented and I'm very much talking about OO here, not procedural programming. Same thing applies to ranges. 1 to: 10 do: [:num | Transcript show: num ] #to:do: is a control structure on Number of which Integer is a subclass. Anonymous methods even have methods, consider the basic while loop. [ aList isEmpty ] whileFalse: [ aList removeFirst ] #whileFalse: is a control structure on BlockContext, the class represented by [ ]. No, there's nothing weird at all about hanging control structures off of system objects, that's one of the best ways to design a minimal language and avoid the plague of a hundred reserved words that you encounter in C variants.