Higher Order Messaging (HOM) Marcel Weiher www.metaobject.com - - PowerPoint PPT Presentation

higher order messaging hom
SMART_READER_LITE
LIVE PREVIEW

Higher Order Messaging (HOM) Marcel Weiher www.metaobject.com - - PowerPoint PPT Presentation

Higher Order Messaging (HOM) Marcel Weiher www.metaobject.com Overview Intro Motivation/Development Usage and Implementation Outlook HOM in a Nutshell Message taking a Message as an Argument Example [[stringArray


slide-1
SLIDE 1

Higher Order Messaging (HOM)

Marcel Weiher www.metaobject.com

slide-2
SLIDE 2

Overview

  • Intro
  • Motivation/Development
  • Usage and Implementation
  • Outlook
slide-3
SLIDE 3

HOM in a Nutshell

  • Message taking a Message as

an Argument

  • Example

[[stringArray collect] stringByAppendingString:@"suffix"];

(this returns the result of sending the message "stringByAppendingString" with the argument @"suffix" to each

  • f theobjects in stringArray)
slide-4
SLIDE 4

Motivation

  • Pattern avoidance (laziness)
  • The "loop" pattern

id nextObject; id enumerator=[array objectEnumerator]; id result=[NSArray array]; while ( nil!=(nextObject=[enumerator nextObject] ) { [result addObject:[nextObject stringByAppendingString:@"suffix"]]; } (most of this code is unchanging, it gets repeated in every 'invocation' of the pattern. We should be able to write it once and then 'call' it with the arguments given in bold)

slide-5
SLIDE 5

Inspiration

  • Iteration in Smalltalk, ST-72
  • forall in Postscript
  • Functors in FP (HOF)
  • Unix Pipes and Filters
  • NSEnumerator
slide-6
SLIDE 6

Perspiration

  • makeObjectsPerformSelector:
  • Filter-Enumerators: enumerator-

based, stackable, practical

result=[array filterWithSelector: @selector(stringByAppendingString:) withObject:@"suffix" withObjet:nil];

slide-7
SLIDE 7

Problems

  • proliferation of filter-methods
  • dispersed message-send

result=[array filterWithSelector: @selector(stringByAppendingString:) withObject:@"suffix" withObjet:nil]; (the parts in bold together form a message send)

  • order obscured

result=[array filterArgumentWithSelector: @selector(stringByAppendingString:) withTarget:@"prefix" withObjet:nil]; (@"prefix" is the receiver of the dispersed message send, array is the argument, but the order is reversed in code)

slide-8
SLIDE 8

Insight

  • use existing syntax

result=[object stringByAppendingString:@"suffix"];

  • generalize message send
  • use runtime as 'parser'
slide-9
SLIDE 9

HOM

  • prefix message (the HOM)

[[array collect] stringByAppendingString:@"suffix"];

  • argument-message

[[array collect] stringByAppendingString:@"suffix"];

slide-10
SLIDE 10

Usage

  • HOMs:

do,collect,select,selectWhere:,reduce

  • Arbitrary multiple iterations

[[prefixes collect] stringByAppendingString:@"suffix"]; (iterates through receiver array, single argument) [[@"prefix" collect] stringByAppendingString:[suffixes each]]; (single receiver, iterates through argument array) [[prefixes collect] stringByAppendingString:[suffixes each]]; (iterates through both receiver array and argument array in lock-step)

slide-11
SLIDE 11

Advantages

  • Saves code
  • Declarative, intentional style
  • Chunk more, do more
  • Easy conceptual model
  • Works across bridges
slide-12
SLIDE 12

Implementation

  • Trampolines
  • Filter Enumerators
  • Optimization
slide-13
SLIDE 13

Trampolines

  • NSInvocation
  • forwardInvocation:
  • Target + selector to bounce
slide-14
SLIDE 14

Filter-Enumerators

  • Setup
  • nextObject
  • Results Processing
  • Running the Iteration
slide-15
SLIDE 15

Optimization

  • Aggressive IMP-caching
  • Encapsulated/Localized
  • Compensate Overhead
slide-16
SLIDE 16

Issues

  • Overhead
  • Obfuscation
  • Portability
  • select and calling conventions
slide-17
SLIDE 17

Outlook

  • Expose stackability
  • Optimize implementation
  • Eliminate more patterns
  • DBs, Threads, Futures,
  • Idea of message transport
slide-18
SLIDE 18

Availability

  • Part of MPWFoundation
  • unit tests, heavy use, > 4 years

(PS/PDF Interpreter, Workflow-Engine, etc.)

  • Free for the asking

www.metaobject.com

slide-19
SLIDE 19

Summary

  • EnumFilters eliminate loops
  • Obvious interface via HOM
  • Very general technique
  • Tested, free:

www.metaobject.com