Eero cocoaheadssf-talk

442 views
381 views

Published on

Slides from the talk I gave at Cocoaheads-SF on Aug 9, 2012.

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
442
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Eero cocoaheadssf-talk

  1. 1. eeroobjective-c, evolved Andy Arvanitis
  2. 2. What is Eero? • A programming language • It is a dialect of Objective-C • It has a streamlined syntax • It is binary- and header-compatible with Objective-C • Its toolchain is implemented using a continuously updated fork of LLVM/Clang (trunk)Cocoaheads-SF Aug. 9, 2012
  3. 3. Why did I do this? • My background: I’ve mostly worked for large companies, large organizations, often large teams • Whether an architect or individual contributor, I end up reading lots and lots of other people’s code • Even reading your own code after a few months (or years) is like reading someone else’s codeCocoaheads-SF Aug. 9, 2012
  4. 4. Code readability • Code is read much more often than it’s written • Human “parsability” of code is critical • Readability matters A LOT! And when it comes to a programming language, it’s not “just syntactic sugar” • Eero is an unrepentant project about syntactic sugarCocoaheads-SF Aug. 9, 2012
  5. 5. Eero goals • To be a practical tool, not just an experiment • To reuse ALL Objective-C frameworks out there, as seamlessly as possible • To keep Objective-C strengths: named parameters, type checking, performance, interoperability with C libraries, toolsCocoaheads-SF Aug. 9, 2012
  6. 6. Eero principles • DRY • WYSIWYG (POLS) • Make the compiler, not the human, do the workCocoaheads-SF Aug. 9, 2012
  7. 7. Eero techniques • Remove clutter without introducing ambiguity • Avoid “syntactic saccharin” • Employ sane defaultsCocoaheads-SF Aug. 9, 2012
  8. 8. General code-structure changes
  9. 9. Significant whitespace • Offside-rule, à la Python • Lots of controversy — strong opinions on both sides • Personally, I wasn’t so sure about it at first, but grew to appreciate/love itCocoaheads-SF Aug. 9, 2012
  10. 10. Significant whitespace — why? • WYSIWYG / DRY: • most agree that blocks should be indented • Why should I have to indent and use braces? • Readability! — less visual clutterCocoaheads-SF Aug. 9, 2012
  11. 11. Optional semicolons • Readability (less visual clutter) • DRY • Good compilers generally know where they should go. Why should I have to do it? (works best when newline is significant) • Clang very good for this, made it easy to implementCocoaheads-SF Aug. 9, 2012
  12. 12. Local type inference • The := operator specifies a variable definition and assignment, with the type inferred from the value • Same behavior as C++11’s “auto” counter := 0 // infers an int const title := ”hello, world”Cocoaheads-SF Aug. 9, 2012
  13. 13. Local type inference • Gains some of the conciseness advantages of Ruby/Python, but a bit safer • Compiler can still catch typos in subsequent assignments • Normal = assignment operator remains unchangedCocoaheads-SF Aug. 9, 2012
  14. 14. Namespace-like prefixes • Eero does not introduce a true namespace facility, but instead works with existing Objective-C prefix conventions • Prefixes can be “registered” within a scope • If a symbol (class, type, function, etc.) is not found by the compiler, it tries again, with prefix • The “NS” prefix is built-in, e.g., “String” resolves to “NSString”Cocoaheads-SF Aug. 9, 2012
  15. 15. Namespace-like prefixes, cont. • User-defined prefixes • Eero introduces keyword using (borrowed from C++), and context keyword prefix • To register prefix: using prefix XX • Declared (and valid) within an enclosing scope: file, method, function, conditional block, etc.Cocoaheads-SF Aug. 9, 2012
  16. 16. Example using prefix UI // file scope int myFunction() using prefix AB // only in function scope AddressBook addressBook = nil Log(...) // resolves to NSLog() return 0Cocoaheads-SF Aug. 9, 2012
  17. 17. Objective-C–specific changes
  18. 18. Objective-C keywords • Objective-C keywords “promoted” to first-class keywords • @ no longer needed in front of them interface MyClass : Object ... endCocoaheads-SF Aug. 9, 2012
  19. 19. NSString literals • No @, enclosed in single quotes • C string literals remain in double-quotes title := ’hello, world’Cocoaheads-SF Aug. 9, 2012
  20. 20. Array and dictionary literals • Neither type of literal needs @ myArray := [’a’, ’b’, ’c’] myDict := {’A’ : ’a’, ’B’ : ’b’}Cocoaheads-SF Aug. 9, 2012
  21. 21. Array and dictionary literals • Since we’re on the topic: empty object literals are mutable myMutableArray := [] myMutableDict := {} myMutableString := ’’Cocoaheads-SF Aug. 9, 2012
  22. 22. NSRange literals • Alternative to NSMakeRange() or C99’s (NSRange){ start, length } • Accepts any integer values, not just numeric literals range := 1 .. 10 otherRange := kFirst .. kLastCocoaheads-SF Aug. 9, 2012
  23. 23. Objects are always pointers • In Objective-C, we never have objects on the stack, so it is never valid to declare a non- pointer object variable • Eero assumes you mean a pointer; nothing else is valid anyway • Consistent with message-passing syntax String title = ’Hello, World!’Cocoaheads-SF Aug. 9, 2012
  24. 24. Methods andmessage-passing
  25. 25. Message-passing sans brackets • Like Smalltalk, message passing is done without square brackets • However, commas are used to help humans (and the compiler) disambiguate selectors/ parameters • Message-passing expressions are just like any other expression; they can be put in parentheses, if neededCocoaheads-SF Aug. 9, 2012
  26. 26. Examples names := [] names addObject: ’Saarinen’ names insertObject: ‘Eero’, atIndex: 0 names addObject: (otherNames objectAtIndex: 0)Cocoaheads-SF Aug. 9, 2012
  27. 27. Method declarations • An observation: argument variable names are not really needed in interfaces • The users of the method don’t care about them (it’s an implementation detail) • The implementor of the method doesn’t need to make variable naming decisions when designing just the interfaceCocoaheads-SF Aug. 9, 2012
  28. 28. Method declarations, cont. • Another observation: argument variable names tend to be very similar to (or derived from) their selector names • Can we use sane defaults? • When we do need to specify arg variable names, can we get something a little more consistent with normal variable declarations (maybe without those parentheses)?Cocoaheads-SF Aug. 9, 2012
  29. 29. Method declarations in Eero • Argument variable names default to last camel-case word in selector (see site for more details) • Similar to Apple’s approach to properties/ setters • Override with variable name following type, just like a normal declarationCocoaheads-SF Aug. 9, 2012
  30. 30. Example @interface MyNotificationCenterClass -(void)addObserver:(id)observer selector:(SEL)selector name:(NSString *)notificationName object:(id)object; @endCocoaheads-SF Aug. 9, 2012
  31. 31. Example interface MyNotificationCenterClass addObserver: id, selector: SEL, name: String, object: SEL endCocoaheads-SF Aug. 9, 2012
  32. 32. Example implementation MyNotificationCenterClass addObserver: id, selector: SEL, name: String, object: SEL if observer == nil ... endCocoaheads-SF Aug. 9, 2012
  33. 33. Example implementation MyNotificationCenterClass addObserver: id, selector: SEL, name: String notificationName, object: SEL if observer == nil ... endCocoaheads-SF Aug. 9, 2012
  34. 34. Method declarations in Eero • Note: there are a couple of (subtle) departures from Objective-C • Parameter types are no longer optional (they do not default to id) • When absent, return type no longer defaults to id, it now means no return value at all, i.e., void — WYSIWYGCocoaheads-SF Aug. 9, 2012
  35. 35. Optional and default parameters • Eero’s method prototype syntax allows specification of optional parameters (selector pieces) and default values • Optional parameters are enclosed in square brackets in the interface (mirroring common software/tech-doc convention) • Defaults are assigned in the implementation using “= value” (they are, in fact, an implementation detail)Cocoaheads-SF Aug. 9, 2012
  36. 36. Example interface MyNotificationCenterClass addObserver: id, selector: SEL, name: String, object: SEL end implementation MyNotificationCenterClass addObserver: id, selector: SEL, name: String, object: SEL if name == nil ... endCocoaheads-SF Aug. 9, 2012
  37. 37. Example interface MyNotificationCenterClass addObserver: id, selector: SEL, [name: String], object: SEL end implementation MyNotificationCenterClass addObserver: id, selector: SEL, name: String, object: SEL if name == nil ... endCocoaheads-SF Aug. 9, 2012
  38. 38. Example interface MyNotificationCenterClass addObserver: id, selector: SEL, [name: String], object: SEL end implementation MyNotificationCenterClass addObserver: id, selector: SEL, name: String = nil, object: SEL if name == nil ... endCocoaheads-SF Aug. 9, 2012
  39. 39. Optional and default parameters • This is all just sugar, and doesn’t introduce any semantic changes • The compiler simply generates the appropriate method interfaces and implementations • You can mix/match these with existing Objective-C classesCocoaheads-SF Aug. 9, 2012
  40. 40. Object operators
  41. 41. Object subscript operators • Same support recently added to Objective-C (although Eero had them first ;-) • Integers imply array operations; objects imply dictionary operations • Eero introduces support for NSRange subscripts • If a string, slice via substringWithRange • Otherwise, slice via subarrayWithRangeCocoaheads-SF Aug. 9, 2012
  42. 42. Example implementation MyClass + truncateString: String, return String if string.length > 10 truncatedString := string[0 .. 9] return truncatedString return string endCocoaheads-SF Aug. 9, 2012
  43. 43. Example implementation MyClass + truncateString: String, return String if string.length > 10 truncatedString := string[0 .. 9] return truncatedString return string + truncateArray: Array, return Array if array.length > 100 truncatedArray := array[0 .. 99] return truncatedArray return array endCocoaheads-SF Aug. 9, 2012
  44. 44. Overloadable binary operators • Supported operators are + - * / < > • Includes implicit support for operator/equal variants (+=, -=, <=, etc.) • Each of these map to method selectors plus, minus, multipliedBy, isLess, etc. • Overloaded by implementing any of these methodsCocoaheads-SF Aug. 9, 2012
  45. 45. Example // Defining some string comparison operators using // a category implementation String (operators) isLess: String string, return BOOL const comparison := self compare: string return (comparison == OrderedAscending) isGreater: String string, return BOOL const comparison := self compare: string return (comparison == OrderedDescending) endCocoaheads-SF Aug. 9, 2012
  46. 46. Example // Using the string comparison operator ‘<‘ from // the category name := ‘Eames’ previousName := ‘Saarinen’ if name < previousName ...Cocoaheads-SF Aug. 9, 2012
  47. 47. Overloadable operators, cont. • There are some built-in operators • == and != via isEqual for all object types • If object is a String, + is concatenation via stringWithString • If object is a MutableString, << is concatenation via appendStringCocoaheads-SF Aug. 9, 2012
  48. 48. Example hello := ’hello’ world := ’world’ title := hello + ’, ’ + world // title is now ‘hello, world’ itemName := ’’ itemName << ‘tulip’ itemName << ’ ’ itemName << ‘chair’ // itemName is now ‘tulip chair’Cocoaheads-SF Aug. 9, 2012
  49. 49. Other features • Optional parentheses around conditions • Concise boxing/unboxing • Enhanced blocks (including compact form) • Stricter enum type checking • Enhanced switch/case • And more!Cocoaheads-SF Aug. 9, 2012
  50. 50. Questions?Cocoaheads-SF Aug. 9, 2012
  51. 51. Thank you! • More information: • http://eerolanguage.org • https://github.com/eerolanguage • My twitter: @andyarvanitisCocoaheads-SF Aug. 9, 2012

×