

% add the PATH to retrieve cls and sty beamer styles files
\makeatletter
\providecommand*{\input@path}{}
\edef\input@path{{../../support/beamer/}\input@path}% prepend
\makeatother

% \documentclass{pharoslides}
% \documentclass[aspectratio=169]{pharoslides} % format 16/9
\documentclass[withvideo]{pharoslides} % format 16/9 + incrustation video

\graphicspath{{./figures/}{../}{../../support/beamer/imgs/}}

%Information to be included in the title page:
\title{Understanding Messages}
\subtitle{}
\author{Damien Cassou, Stéphane Ducasse and Luc Fabresse}
\institute{http://stephane.ducasse.free.fr}
\date{}
\slidesid{W2S01}

\begin{document}

\frame[plain]{\titlepage}



\begin{frame}[fragile]

\frametitle{Objects, Messages and Closures}

\begin{itemize}
\item We only manipulate \textbf{objects} (mouse, booleans, arrays, numbers, strings, ...)
\item We only send them \textbf{messages} (@, +, not, getPng:, ifTrue:ifFalse:, new, ...)
\item and we use \textbf{closures}
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Syntax}

\begin{itemize}
\item Originally invented for kids 
\item Programs look like little sentences
\item Try to minimize the number of parentheses
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Example}

\begin{listing}{}
(ZnEasy getPng: 'http://a.tile.openstreetmap.org/8/12/8.png') 
    asMorph openInWindow
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Three Kinds of Messages to Minimize Parentheses}

\begin{itemize}
\item Unary message: \code{receiver selector}
\begin{itemize}
\item \code{9 squared}, \code{Date today}
\end{itemize}

\item Binary message: \code{receiver selector argument}
\begin{itemize}
\item \code{1+2}
\item \code{3@4}
\end{itemize}

\item Keyword message: \code{receiver key1: arg1 key2: arg2}
\begin{itemize}
\item \code{2 between: 10 and: 20}
\end{itemize}

\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Message Precedence}

(Msg) \textgreater{} Unary \textgreater{} Binary \textgreater{} Keywords

\begin{itemize}
\item First we execute \code{()}
\item Then unary, then binary and finally keyword messages
\end{itemize}

This order minimizes \code{()} needs

But let us start with messages
\end{frame}



\begin{frame}[fragile]

\frametitle{Unary Message Examples}

\begin{listing}{}
anObject aSelector
\end{listing}

\begin{listing}{}
1 class
> SmallInteger
\end{listing}

\begin{listing}{}
false not
> true
\end{listing}

\begin{listing}{}
Date today
> 24 May 2009
\end{listing}

\begin{listing}{}
Float pi
> 3.141592653589793
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Did you notice?}

\begin{itemize}
\item We sent messages to any objects, including classes!
\item There is no difference between sending a message to an object or to a class
\end{itemize}

\begin{listing}{}
1 class
> SmallInteger
\end{listing}

\begin{listing}{}
Date today
> 27 June 2015
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{A Bit of Introspection}

\begin{listing}{}
Point selectors
> #(#x #theta #quadrantOf: #onLineFrom:to:within: #bitShiftPoint: #< #scaleFrom:to: #sideOf: #'\\' #scaleTo: #grid: #'//' #asIntegerPoint #directionToLineFrom:to: ...) 
\end{listing}

\begin{itemize}
\item Returns all the messages the class understands
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{A Little Query}

\begin{itemize}
\item Let us query the system and only filter the unary messages:
\end{itemize}

\begin{listing}{}
    Point selectors select: #isUnary  
    > #(#x #theta #asIntegerPoint #r #negated #normalized #sign #degrees #isIntegerPoint #guarded #fourNeighbors #eightNeighbors #min #max #ceiling #normal #asPoint #y #abs #isPoint #angle #transposed #reciprocal #asFloatPoint #asNonFractionalPoint #rounded #leftRotated #floor #truncated #hash #deepCopy #fourDirections #rightRotated #isSelfEvaluating #asMargin #isZero)
\end{listing}

\begin{itemize}
\item \code{select:} is an iterator (see Iterator lecture)
\item Easy :-)
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Binary Messages}

\begin{listing}{}
anObject aBinarySelector anArgument
\end{listing}

\begin{itemize}
\item Used for arithmetic, comparison and logical operations
\item One, two or three characters taken from:
\begin{itemize}
\item + - / \textbackslash{} * \textasciitilde{} \textless{} \textgreater{} = @ \% \textbar{} \& ! ? ,
\end{itemize}

\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Binary Message Examples}

\begin{listing}{}
1 + 2
> 3
\end{listing}

\begin{listing}{}
2 > 3
> false
\end{listing}

\begin{listing}{}
10@200 
> 10@200
\end{listing}

\begin{listing}{}
'Black chocolate' , ' is good'
> 'Black chocolate is good'
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Keyword Messages}

\begin{listing}{}
anObject keyword1: arg1 keyword2: arg2
\end{listing}

equivalent to:

\begin{listing}{}
receiver.keyword1keyword2(arg1, arg2)
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Test Yourself!}

\begin{itemize}
\item \code{1 log}
\item \code{Browser open} 
\item \code{2 raisedTo: 5}
\item \code{'hello', 'world'}
\item \code{10@20}
\item \code{point1 x}
\item \code{point1 distanceFrom: point2}
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Test Yourself!}

\begin{itemize}
\item \code{1 log} (unary)
\item \code{Browser open} (unary)
\item \code{2 raisedTo: 5} (keyword)
\item \code{'hello', 'world'} (binary)
\item \code{10@20} (binary)
\item \code{point1 x} (unary)
\item \code{point1 distanceFrom: point2} (keyword)
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Example: Message setX:}

\begin{listing}{}
10@20 setX: 2
> 2@20
\end{listing}

\begin{itemize}
\item We change the x value of the receiver (a point)
\item No parentheses required
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Example: Message at:put:}

\begin{listing}{}
#('Calvin' 'hates' 'Suzie') at: 2 put: 'loves'
> #('Calvin' 'loves' 'Suzie')
\end{listing}

\begin{itemize}
\item \code{\#(...)} creates an array
\item \code{at:put:} changes the value of the array element.
\item arrays start at 1 in Pharo (i.e., first element is at index 1)
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Example: Message between:and:}

\begin{listing}{}
12 between: 10 and: 20
> true
\end{listing}

\begin{itemize}
\item The message \code{between:and:} is sent to an integer
\item Takes two arguments \code{10} and \code{20}
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Summary}

Three kinds of messages: unary, binary and keywords

\begin{itemize}
\item Unary
\begin{itemize}
\item \code{5 factorial}
\end{itemize}

\item Binary
\begin{itemize}
\item \code{2 + 3}
\end{itemize}

\item Keywords-based messages
\begin{itemize}
\item \code{2 between: 0 and: 10}
\end{itemize}

\end{itemize}
\end{frame}




% \setlength{\textwidth}{\originaltextwidth}
\setbeamertemplate{background canvas}{\insertBorderForHandout}

\begin{frame}[plain]

\begin{minipage}{\originaltextwidth}
\centering
\large


\vspace{2em}

A course by

\vspace{1em}

\hspace{-20px}\includegraphics[height=32px]{inriaLogo}\hspace{3em}\raisebox{7px}{and}\hspace{3em}\raisebox{-6px}{\includegraphics[height=30px]{logoutop}}

\vspace{2em}

in collaboration with

\vspace{1em}

\includegraphics[height=40px]{unisciellogo}\hskip 1.5em%
\raisebox{5px}{\includegraphics[height=30px]{unitlogo}}\hskip 2em%
\raisebox{8px}{\includegraphics[height=24px]{univLilleLogo}}\hskip 2em%
\raisebox{3px}{\includegraphics[height=29px]{logo-IMT}}\hskip 2em%
\raisebox{1px}{\includegraphics[height=32px]{minesDouaiLogo}}

\vspace{2.5em}

\includegraphics[width=50px]{by-nc-nd} \tiny \hskip 0.5em \raisebox{5px}{Inria 2016}

\medskip

\tiny
Except where otherwise noted, this work is licensed under CC BY-NC-ND 3.0 France

\url{https://creativecommons.org/licenses/by-nc-nd/3.0/fr/} 

\end{minipage}
\end{frame}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
