SlideShare a Scribd company logo
Value Objects
Brendan Eich
brendan@mozilla.com
@BrendanEich
!

Ecma TC39 January 2014
Caveats & Pleas

• A review & update from July 2013 TC39 meeting	

• I’m looking for big picture and detailed feedback	

• The big picture matters most at this stage	

• There will be some TypeScript syntax/semantics!	

• Please hold your fire, illustrative & concrete but
could be changed based on other ES7 work	


• These slides are dense, please feel free to ask Qs	

• Not done yet, open issues & imperfections below
Value Objects

• int32, uint32!
• int64, uint64!
• int32x4, int32x8 (SIMD)	

• float32 (to/from Float32Array today)	

• float32x4, float32x8 (SIMD)	

• bignum	

• decimal (long-time TC39 goal: self-hosted extension)	

• rational	

• complex
Overloadable Operators

• | ^ &!
• ==!
• < <=!
• << >> >>>!
• + -!
• * / %!
• unary- unary+

boolean-test!! ~
Preserving Boolean Algebra

•

!= and ! are not overloadable, to preserve

identities including	


•
•
•
•

X ? A : B

<=>

!X ? B : A	


!(X && Y)

<=>

!X || !Y!

!(X || Y)

<=>

!X && !Y	


X != Y

<=>

!(X == Y)
Preserving Relational Relations

•

> and >= are derived from < and <= as

follows:	


•
•

A > B

<=>

B < A	


A >= B

<=>

B <= A	


• We provide <= in addition to < rather than

derive A <= B from !(B < A) in order to
allow the <= overloading to match the same
value object’s == semantics -- and for special
cases, e.g., unordered values (NaNs)
Strict Equality Operators

• The strict equality operators, === and !==,
cannot be overloaded	


• They work on frozen-by-definition value
objects via a structural recursive strict
equality test (beware, NaN !== NaN)	


• Same-object-reference remains a fast-path
optimization
Why Not Double Dispatch?

• Left-first asymmetry (v value, n number):	

•
•

v + n

==>

v.add(n)	


n + v

==>

v.radd(n)!

• Anti-modular: exhaustive other-operand type
enumeration required in operator method
bodies	


• Consequent loss of compositionality:

complex and rational cannot be
composed to make ratplex without

modifying source or wrapping in proxies
Cacheable Multimethods

• Proposed in 2009 by Christian Plesner Hansen
(Google) in es-discuss	


• Avoids double-dispatch drawbacks from last slide:
binary operators implemented by 2-ary functions
for each pair of types	


• Supports Polymorphic Inline Cache (PIC)

optimizations (Christian was on the V8 team)	


• Background reading: [Chambers 1992]
Binary Operator Example

• For v + u with either a value object: !
• Let p = v.[[Get]](@@ADD)	

• If p is not a Set, throw a TypeError	

• Let q = u.[[Get]](@@ADD_R)	

• If q is not a Set, throw a TypeError	

• Let r = p intersect q	

• If r.size != 1 throw a TypeError	

• Let f = r[0]; if f is not a function, throw	

• Evaluate f(v, u) and return the result
API Idea from CPH 2009
// NOTE: NOT PROPOSED FOR ES7	

!
function addPointAndNumber(a, b) {	
return Point(a.x + b, a.y + b);	
}	

!
Function.defineOperator('+', addPointAndNumber, Point, Number);	

!
function addNumberAndPoint(a, b) {	
return Point(a + b.x, a + b.y);	
}	

!
Function.defineOperator('+', addNumberAndPoint, Number, Point);	

!
function addPoints(a, b) {	
return Point(a.x + b.x, a.y + b.y);	
}	

!
Function.defineOperator('+', addPoints, Point, Point);
Literal Syntax

•
•
•
•
•

int64(0)

==>

0L // as in C#!

uint64(0)

==> 0UL // as in C#!

float32(0) ==>

0f // as in C#!

bignum(0)

0n // avoid i/I!

==>

decimal(0) ==>

0m // or M, C/F#!

• We want a syntax extension mechanism, with
declarative not runtime API	


• This means new syntax for operator function
and suffix definition
Value Class Declaration
value class point2d {	
// no suffix	
// default typeof “point2d” (no overriding/spoofing)	

!
constructor(private x: int32, private y: int32) {	
// implicit Object.freeze(this) on return	
}	

!
// binary operators (note arrow shorthand for { return … })	
point2d + number (a, b) => point2d(a.x + b, a.y);	
number + point2d (a, b) => point2d(a + b.x, b.y);	
point2d + point2d (a, b) => point2d(a.x + b.x, a.y + b.y);	

!
point2d - number (a, b) => point2d(a.x - b, a.y);	
number - point2d (a, b) => point2d(a - b.x, b.y);	
point2d - point2d (a, b) => point2d(a.x - b.x, a.y - b.y);	

!
// more operators with private access elided	
}
Value Class Declaration, cont.
value class pixel {	 // CSS unit, 1/96th of an inch	
	
suffix “px”;	
typeof “CSS:pixel”;	// we allow a lot, but overriding throws	

!
constructor pixel(public twips: int32) {	
// implicit Object.freeze(this) on return	
}	

!
// unary operators (note arrow shorthand for { return … })	
+() => pixel(this.twips);	
-() => pixel(-this.twips);	
!!() => !!this.twips;	
~() => pixel(~this.twips);	
}	

!
value class point {	 // CSS unit, not Cartesian plane point!	
	
suffix “pt”;	
typeof “CSS:point”;	
// constructor and unary operators not shown…	
}
Binary Operator Declaration
value operators {	
// Here number, string, boolean are in scope, and new operator	
// syntax common to value class works.	

!
pixel + number (a, b)
number + pixel (a, b)
pixel + pixel (a, b)
pixel + point (a, b)
point + pixel (a, b)

=>
=>
=>
=>
=>

pixel(a.twips + b * 15);	
pixel(a * 15 + b.twips);	
pixel(a.twips + b.twips);	
pixel(a.twips + b.twips * 20);	
pixel(a.twips * 20 + b.twips);	

pixel - number (a, b)
number - pixel (a, b)
pixel - pixel (a, b)
pixel - point (a, b)
point - pixel (a, b)

=>
=>
=>
=>
=>

pixel(a.twips - b * 15);	
pixel(a * 15 - b.twips);	
pixel(a.twips - b.twips);	
pixel(a.twips - b.twips * 20);	
pixel(a.twips * 20 - b.twips);	

!

!
// etc… (note only public class members)	
}
Value Subclasses
value class point2d {	
constructor point2d(public x: int32, public y: int32) {	
// implicit Object.freeze(this) on return EXCEPT via super	
}	
// call this function f:	
point2d + point2d (a, b) => point2d(a.x + b.x, a.y + b.y);	
}	

!
value class point3d extends point2d {	
constructor point3d(x: int32, y: int32, public z: int32) {	
super(x, y);	
// implicit Object.freeze(this) on return once, here	
}	
// call this function g:	
point3d + point3d (a, b) => point3d(a.x+b.x, a.y+b.y, a.z+b.z);	
}	

!
// What does point3d(1, 0, 0) + point3d(0, 1, 0) do?	
{f, g} intersect {f, g} => {f, g}, ambiguity error!
Class Precedence via Prototype Depth
// When point2d’s declaration is evaluated:	
let f = point2d + point2d (a, b) => point2d(a.x + b.x, a.y + b.y);	
point2d.prototype.@@ADD = Set([[1, f]]);	
point2d.prototype.@@ADD_R = Set([[1, f]]);	

!
// When point3d’s declaration is evaluated:	
let g = point3d+point3d (a,b) => point3d(a.x+b.x,a.y+b.y,a.z+b.z);	
point3d.prototype.@@ADD = Set([[1, f], [2, g]]);	
point3d.prototype.@@ADD_R = Set([[1, f], [2, g]]);	

!
// Set notation, please! Here’s what we have at this moment:	
point2d.prototype.@@ADD: {[1, f]}	
point2d.prototype.@@ADD_R: {[1, f]}	
point3d.prototype.@@ADD: {[1, f], [2, g]}	
point3d.prototype.@@ADD_R: {[1, f], [2, g]}	

!
// What does point3d(1, 0, 0) + point3d(0, 1, 0) do?	
{[1,f], [2,g]} intersect {[1,f], [2,g]} => g, more specific wins
The (Other) Frame Problem
// P1: Primitives, e.g. strings, are wrapped via the corresponding	
// scoped constructor/converter function, e.g. String.	
String.prototype.len = function () { return this.length; }	

!
// Suppose otherFrame.str is “LOL”, the primitive string value:	
alert(otherFrame.str.len());	 	 	 	 // 3	

!
// Value objects are objects, that’s the price of extensibility.	
int64.prototype.digits = function () { … }	

!
alert(otherFrame.int64(42).digits())	 // throws, method missing	

!
// P2: As noted last time, cross-frame/realm binary ops fail:	
let three = 1L + otherFrame.int64(2);	 // throws, no such method	

!
//
//
//
//

Possible solutions:	
1. Live with it, frames/realms (should be) more isolated FTW	
2. Proxy as if across a membrane, isolation with mediation FTW	
3. Memoize aggressively (hash-cons); solves P2, not P1 in full
Healing the Old Wounds
// Primitives: built-in magic, not extensible, auto-wrap via scope	
// Reference Objects: user-extensible, conversions not operators	
// Value Objects: by-value semantics; multimethod dyadic operator,	
//
unary operator, suffix, and typeof extensibility	

!
// Idea: enable the JS hacker to bless primitives as value objects	
// and thereby opt into value object operator semantics.	

!
value
value
value
value

class
class
class
class

null;	 	
boolean;	
number;	
	
string;	
	

//
//
//
//

typeof null == “null”	
false != "", true != 1	
42 != "42", 1 != true	
"" != false, “42" != 42, [] + “" throws	

!
// ‘value class string;’ will require explicit .toString() calls!	

!
use value sanity;	 	
	 	 	 	 	 	 	 	

// all of the above, a shorthand; no way to	
// “undeclare”; upgrade your Realm!
Thanks / Q&A
Brendan Eich
brendan@mozilla.com
@BrendanEich
!

Ecma TC39 January 2014

More Related Content

What's hot

Aspdot
AspdotAspdot
Dynamic Type Inference for Gradual Hindley–Milner Typing
Dynamic Type Inference for Gradual Hindley–Milner TypingDynamic Type Inference for Gradual Hindley–Milner Typing
Dynamic Type Inference for Gradual Hindley–Milner Typing
Yusuke Miyazaki
 
Mining Functional Patterns
Mining Functional PatternsMining Functional Patterns
Mining Functional Patterns
Debasish Ghosh
 
Monte Carlo C++
Monte Carlo C++Monte Carlo C++
Monte Carlo C++
Dmitri Nesteruk
 
Optionals by Matt Faluotico
Optionals by Matt FaluoticoOptionals by Matt Faluotico
Optionals by Matt Faluotico
WithTheBest
 
Algebraic Thinking for Evolution of Pure Functional Domain Models
Algebraic Thinking for Evolution of Pure Functional Domain ModelsAlgebraic Thinking for Evolution of Pure Functional Domain Models
Algebraic Thinking for Evolution of Pure Functional Domain Models
Debasish Ghosh
 
Templates
TemplatesTemplates
Templates
Nilesh Dalvi
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
Keyur Vadodariya
 
Introducing: A Complete Algebra of Data
Introducing: A Complete Algebra of DataIntroducing: A Complete Algebra of Data
Introducing: A Complete Algebra of Data
Inside Analysis
 
Towards hasktorch 1.0
Towards hasktorch 1.0Towards hasktorch 1.0
Towards hasktorch 1.0
Junji Hashimoto
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
Shubham Vishwambhar
 
Types by Adform Research
Types by Adform ResearchTypes by Adform Research
Types by Adform ResearchVasil Remeniuk
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
Mayank Bhatt
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
Dr Sukhpal Singh Gill
 
Introduction to programming with dependent types in Scala
Introduction to programming with dependent types in ScalaIntroduction to programming with dependent types in Scala
Introduction to programming with dependent types in Scala
Dmytro Mitin
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
gourav kottawar
 

What's hot (19)

Aspdot
AspdotAspdot
Aspdot
 
Dynamic Type Inference for Gradual Hindley–Milner Typing
Dynamic Type Inference for Gradual Hindley–Milner TypingDynamic Type Inference for Gradual Hindley–Milner Typing
Dynamic Type Inference for Gradual Hindley–Milner Typing
 
Mining Functional Patterns
Mining Functional PatternsMining Functional Patterns
Mining Functional Patterns
 
Monte Carlo C++
Monte Carlo C++Monte Carlo C++
Monte Carlo C++
 
Optionals by Matt Faluotico
Optionals by Matt FaluoticoOptionals by Matt Faluotico
Optionals by Matt Faluotico
 
Algebraic Thinking for Evolution of Pure Functional Domain Models
Algebraic Thinking for Evolution of Pure Functional Domain ModelsAlgebraic Thinking for Evolution of Pure Functional Domain Models
Algebraic Thinking for Evolution of Pure Functional Domain Models
 
Templates
TemplatesTemplates
Templates
 
Clojure intro
Clojure introClojure intro
Clojure intro
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Overview of c (2)
Overview of c (2)Overview of c (2)
Overview of c (2)
 
Introducing: A Complete Algebra of Data
Introducing: A Complete Algebra of DataIntroducing: A Complete Algebra of Data
Introducing: A Complete Algebra of Data
 
Towards hasktorch 1.0
Towards hasktorch 1.0Towards hasktorch 1.0
Towards hasktorch 1.0
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 
Types by Adform Research
Types by Adform ResearchTypes by Adform Research
Types by Adform Research
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Introduction to programming with dependent types in Scala
Introduction to programming with dependent types in ScalaIntroduction to programming with dependent types in Scala
Introduction to programming with dependent types in Scala
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
 

Viewers also liked

Mozilla Research Party Talk
Mozilla Research Party TalkMozilla Research Party Talk
Mozilla Research Party Talk
Brendan Eich
 
Containers in a File
Containers in a FileContainers in a File
Containers in a File
OpenVZ
 
PFcache - LinuxCon 2015
PFcache - LinuxCon 2015PFcache - LinuxCon 2015
PFcache - LinuxCon 2015
OpenVZ
 
Value Objects
Value ObjectsValue Objects
Value Objects
barryosull
 
Values
ValuesValues
Values
BenEddy
 
Capitol js
Capitol jsCapitol js
Capitol js
Brendan Eich
 
Fluent15
Fluent15Fluent15
Fluent15
Brendan Eich
 
My dotJS Talk
My dotJS TalkMy dotJS Talk
My dotJS Talk
Brendan Eich
 
JSLOL
JSLOLJSLOL
Paren free
Paren freeParen free
Paren free
Brendan Eich
 
Mozilla's NodeConf talk
Mozilla's NodeConf talkMozilla's NodeConf talk
Mozilla's NodeConf talk
Brendan Eich
 
dotJS 2015
dotJS 2015dotJS 2015
dotJS 2015
Brendan Eich
 
Always bet on JS - Finjs.io NYC 2016
Always bet on JS - Finjs.io NYC 2016Always bet on JS - Finjs.io NYC 2016
Always bet on JS - Finjs.io NYC 2016
Brendan Eich
 
Serializing Value Objects-Ara Hacopian
Serializing Value Objects-Ara HacopianSerializing Value Objects-Ara Hacopian
Serializing Value Objects-Ara Hacopian
SmartLogic
 
Splash
SplashSplash
Splash
Brendan Eich
 
#pugMi - DDD - Value objects
#pugMi - DDD - Value objects#pugMi - DDD - Value objects
#pugMi - DDD - Value objects
Simone Gentili
 
Persisting Value Objects
Persisting Value ObjectsPersisting Value Objects
Persisting Value Objects
The Software House
 
Txjs talk
Txjs talkTxjs talk
Txjs talk
Brendan Eich
 
A System Is Not a Tree
A System Is Not a TreeA System Is Not a Tree
A System Is Not a Tree
Kevlin Henney
 

Viewers also liked (20)

Mozilla Research Party Talk
Mozilla Research Party TalkMozilla Research Party Talk
Mozilla Research Party Talk
 
Containers in a File
Containers in a FileContainers in a File
Containers in a File
 
PFcache - LinuxCon 2015
PFcache - LinuxCon 2015PFcache - LinuxCon 2015
PFcache - LinuxCon 2015
 
Value Objects
Value ObjectsValue Objects
Value Objects
 
Values
ValuesValues
Values
 
Capitol js
Capitol jsCapitol js
Capitol js
 
Fluent15
Fluent15Fluent15
Fluent15
 
My dotJS Talk
My dotJS TalkMy dotJS Talk
My dotJS Talk
 
JSLOL
JSLOLJSLOL
JSLOL
 
Paren free
Paren freeParen free
Paren free
 
Taysom seminar
Taysom seminarTaysom seminar
Taysom seminar
 
Mozilla's NodeConf talk
Mozilla's NodeConf talkMozilla's NodeConf talk
Mozilla's NodeConf talk
 
dotJS 2015
dotJS 2015dotJS 2015
dotJS 2015
 
Always bet on JS - Finjs.io NYC 2016
Always bet on JS - Finjs.io NYC 2016Always bet on JS - Finjs.io NYC 2016
Always bet on JS - Finjs.io NYC 2016
 
Serializing Value Objects-Ara Hacopian
Serializing Value Objects-Ara HacopianSerializing Value Objects-Ara Hacopian
Serializing Value Objects-Ara Hacopian
 
Splash
SplashSplash
Splash
 
#pugMi - DDD - Value objects
#pugMi - DDD - Value objects#pugMi - DDD - Value objects
#pugMi - DDD - Value objects
 
Persisting Value Objects
Persisting Value ObjectsPersisting Value Objects
Persisting Value Objects
 
Txjs talk
Txjs talkTxjs talk
Txjs talk
 
A System Is Not a Tree
A System Is Not a TreeA System Is Not a Tree
A System Is Not a Tree
 

Similar to Value Objects, Full Throttle (to be updated for spring TC39 meetings)

JavaScript in 2016
JavaScript in 2016JavaScript in 2016
JavaScript in 2016
Codemotion
 
JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)
Eduard Tomàs
 
C++ process new
C++ process newC++ process new
C++ process new
敬倫 林
 
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
GeeksLab Odessa
 
Return of c++
Return of c++Return of c++
Return of c++
Yongwei Wu
 
Memory Management with Java and C++
Memory Management with Java and C++Memory Management with Java and C++
Memory Management with Java and C++Mohammad Shaker
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
ANURAG SINGH
 
C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Languagemspline
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++
Manzoor ALam
 
The Present and Future of the Web Platform
The Present and Future of the Web PlatformThe Present and Future of the Web Platform
The Present and Future of the Web Platform
C4Media
 
Replace OutputIterator and Extend Range
Replace OutputIterator and Extend RangeReplace OutputIterator and Extend Range
Replace OutputIterator and Extend Range
Akira Takahashi
 
Angular2 for Beginners
Angular2 for BeginnersAngular2 for Beginners
Angular2 for Beginners
Oswald Campesato
 
Denis Lebedev, Swift
Denis  Lebedev, SwiftDenis  Lebedev, Swift
Denis Lebedev, SwiftYandex
 
Object Oriented Programming using C++ - Part 4
Object Oriented Programming using C++ - Part 4Object Oriented Programming using C++ - Part 4
Object Oriented Programming using C++ - Part 4
University College of Engineering Kakinada, JNTUK - Kakinada, India
 
Modern C++ Lunch and Learn
Modern C++ Lunch and LearnModern C++ Lunch and Learn
Modern C++ Lunch and Learn
Paul Irwin
 
The Present and The Future of Functional Programming in C++
The Present and The Future of Functional Programming in C++The Present and The Future of Functional Programming in C++
The Present and The Future of Functional Programming in C++
Alexander Granin
 
L10
L10L10
L10
lksoo
 
Friend this-new&delete
Friend this-new&deleteFriend this-new&delete
Friend this-new&delete
Shehzad Rizwan
 
Debugging and Profiling C++ Template Metaprograms
Debugging and Profiling C++ Template MetaprogramsDebugging and Profiling C++ Template Metaprograms
Debugging and Profiling C++ Template Metaprograms
Platonov Sergey
 
Writing DSL with Applicative Functors
Writing DSL with Applicative FunctorsWriting DSL with Applicative Functors
Writing DSL with Applicative Functors
David Galichet
 

Similar to Value Objects, Full Throttle (to be updated for spring TC39 meetings) (20)

JavaScript in 2016
JavaScript in 2016JavaScript in 2016
JavaScript in 2016
 
JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)
 
C++ process new
C++ process newC++ process new
C++ process new
 
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
 
Return of c++
Return of c++Return of c++
Return of c++
 
Memory Management with Java and C++
Memory Management with Java and C++Memory Management with Java and C++
Memory Management with Java and C++
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
 
C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Language
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++
 
The Present and Future of the Web Platform
The Present and Future of the Web PlatformThe Present and Future of the Web Platform
The Present and Future of the Web Platform
 
Replace OutputIterator and Extend Range
Replace OutputIterator and Extend RangeReplace OutputIterator and Extend Range
Replace OutputIterator and Extend Range
 
Angular2 for Beginners
Angular2 for BeginnersAngular2 for Beginners
Angular2 for Beginners
 
Denis Lebedev, Swift
Denis  Lebedev, SwiftDenis  Lebedev, Swift
Denis Lebedev, Swift
 
Object Oriented Programming using C++ - Part 4
Object Oriented Programming using C++ - Part 4Object Oriented Programming using C++ - Part 4
Object Oriented Programming using C++ - Part 4
 
Modern C++ Lunch and Learn
Modern C++ Lunch and LearnModern C++ Lunch and Learn
Modern C++ Lunch and Learn
 
The Present and The Future of Functional Programming in C++
The Present and The Future of Functional Programming in C++The Present and The Future of Functional Programming in C++
The Present and The Future of Functional Programming in C++
 
L10
L10L10
L10
 
Friend this-new&delete
Friend this-new&deleteFriend this-new&delete
Friend this-new&delete
 
Debugging and Profiling C++ Template Metaprograms
Debugging and Profiling C++ Template MetaprogramsDebugging and Profiling C++ Template Metaprograms
Debugging and Profiling C++ Template Metaprograms
 
Writing DSL with Applicative Functors
Writing DSL with Applicative FunctorsWriting DSL with Applicative Functors
Writing DSL with Applicative Functors
 

Recently uploaded

Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 

Recently uploaded (20)

Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 

Value Objects, Full Throttle (to be updated for spring TC39 meetings)

  • 2. Caveats & Pleas • A review & update from July 2013 TC39 meeting • I’m looking for big picture and detailed feedback • The big picture matters most at this stage • There will be some TypeScript syntax/semantics! • Please hold your fire, illustrative & concrete but could be changed based on other ES7 work • These slides are dense, please feel free to ask Qs • Not done yet, open issues & imperfections below
  • 3. Value Objects • int32, uint32! • int64, uint64! • int32x4, int32x8 (SIMD) • float32 (to/from Float32Array today) • float32x4, float32x8 (SIMD) • bignum • decimal (long-time TC39 goal: self-hosted extension) • rational • complex
  • 4. Overloadable Operators • | ^ &! • ==! • < <=! • << >> >>>! • + -! • * / %! • unary- unary+ boolean-test!! ~
  • 5. Preserving Boolean Algebra • != and ! are not overloadable, to preserve identities including • • • • X ? A : B <=> !X ? B : A !(X && Y) <=> !X || !Y! !(X || Y) <=> !X && !Y X != Y <=> !(X == Y)
  • 6. Preserving Relational Relations • > and >= are derived from < and <= as follows: • • A > B <=> B < A A >= B <=> B <= A • We provide <= in addition to < rather than derive A <= B from !(B < A) in order to allow the <= overloading to match the same value object’s == semantics -- and for special cases, e.g., unordered values (NaNs)
  • 7. Strict Equality Operators • The strict equality operators, === and !==, cannot be overloaded • They work on frozen-by-definition value objects via a structural recursive strict equality test (beware, NaN !== NaN) • Same-object-reference remains a fast-path optimization
  • 8. Why Not Double Dispatch? • Left-first asymmetry (v value, n number): • • v + n ==> v.add(n) n + v ==> v.radd(n)! • Anti-modular: exhaustive other-operand type enumeration required in operator method bodies • Consequent loss of compositionality: complex and rational cannot be composed to make ratplex without modifying source or wrapping in proxies
  • 9. Cacheable Multimethods • Proposed in 2009 by Christian Plesner Hansen (Google) in es-discuss • Avoids double-dispatch drawbacks from last slide: binary operators implemented by 2-ary functions for each pair of types • Supports Polymorphic Inline Cache (PIC) optimizations (Christian was on the V8 team) • Background reading: [Chambers 1992]
  • 10. Binary Operator Example • For v + u with either a value object: ! • Let p = v.[[Get]](@@ADD) • If p is not a Set, throw a TypeError • Let q = u.[[Get]](@@ADD_R) • If q is not a Set, throw a TypeError • Let r = p intersect q • If r.size != 1 throw a TypeError • Let f = r[0]; if f is not a function, throw • Evaluate f(v, u) and return the result
  • 11. API Idea from CPH 2009 // NOTE: NOT PROPOSED FOR ES7 ! function addPointAndNumber(a, b) { return Point(a.x + b, a.y + b); } ! Function.defineOperator('+', addPointAndNumber, Point, Number); ! function addNumberAndPoint(a, b) { return Point(a + b.x, a + b.y); } ! Function.defineOperator('+', addNumberAndPoint, Number, Point); ! function addPoints(a, b) { return Point(a.x + b.x, a.y + b.y); } ! Function.defineOperator('+', addPoints, Point, Point);
  • 12. Literal Syntax • • • • • int64(0) ==> 0L // as in C#! uint64(0) ==> 0UL // as in C#! float32(0) ==> 0f // as in C#! bignum(0) 0n // avoid i/I! ==> decimal(0) ==> 0m // or M, C/F#! • We want a syntax extension mechanism, with declarative not runtime API • This means new syntax for operator function and suffix definition
  • 13. Value Class Declaration value class point2d { // no suffix // default typeof “point2d” (no overriding/spoofing) ! constructor(private x: int32, private y: int32) { // implicit Object.freeze(this) on return } ! // binary operators (note arrow shorthand for { return … }) point2d + number (a, b) => point2d(a.x + b, a.y); number + point2d (a, b) => point2d(a + b.x, b.y); point2d + point2d (a, b) => point2d(a.x + b.x, a.y + b.y); ! point2d - number (a, b) => point2d(a.x - b, a.y); number - point2d (a, b) => point2d(a - b.x, b.y); point2d - point2d (a, b) => point2d(a.x - b.x, a.y - b.y); ! // more operators with private access elided }
  • 14. Value Class Declaration, cont. value class pixel { // CSS unit, 1/96th of an inch suffix “px”; typeof “CSS:pixel”; // we allow a lot, but overriding throws ! constructor pixel(public twips: int32) { // implicit Object.freeze(this) on return } ! // unary operators (note arrow shorthand for { return … }) +() => pixel(this.twips); -() => pixel(-this.twips); !!() => !!this.twips; ~() => pixel(~this.twips); } ! value class point { // CSS unit, not Cartesian plane point! suffix “pt”; typeof “CSS:point”; // constructor and unary operators not shown… }
  • 15. Binary Operator Declaration value operators { // Here number, string, boolean are in scope, and new operator // syntax common to value class works. ! pixel + number (a, b) number + pixel (a, b) pixel + pixel (a, b) pixel + point (a, b) point + pixel (a, b) => => => => => pixel(a.twips + b * 15); pixel(a * 15 + b.twips); pixel(a.twips + b.twips); pixel(a.twips + b.twips * 20); pixel(a.twips * 20 + b.twips); pixel - number (a, b) number - pixel (a, b) pixel - pixel (a, b) pixel - point (a, b) point - pixel (a, b) => => => => => pixel(a.twips - b * 15); pixel(a * 15 - b.twips); pixel(a.twips - b.twips); pixel(a.twips - b.twips * 20); pixel(a.twips * 20 - b.twips); ! ! // etc… (note only public class members) }
  • 16. Value Subclasses value class point2d { constructor point2d(public x: int32, public y: int32) { // implicit Object.freeze(this) on return EXCEPT via super } // call this function f: point2d + point2d (a, b) => point2d(a.x + b.x, a.y + b.y); } ! value class point3d extends point2d { constructor point3d(x: int32, y: int32, public z: int32) { super(x, y); // implicit Object.freeze(this) on return once, here } // call this function g: point3d + point3d (a, b) => point3d(a.x+b.x, a.y+b.y, a.z+b.z); } ! // What does point3d(1, 0, 0) + point3d(0, 1, 0) do? {f, g} intersect {f, g} => {f, g}, ambiguity error!
  • 17. Class Precedence via Prototype Depth // When point2d’s declaration is evaluated: let f = point2d + point2d (a, b) => point2d(a.x + b.x, a.y + b.y); point2d.prototype.@@ADD = Set([[1, f]]); point2d.prototype.@@ADD_R = Set([[1, f]]); ! // When point3d’s declaration is evaluated: let g = point3d+point3d (a,b) => point3d(a.x+b.x,a.y+b.y,a.z+b.z); point3d.prototype.@@ADD = Set([[1, f], [2, g]]); point3d.prototype.@@ADD_R = Set([[1, f], [2, g]]); ! // Set notation, please! Here’s what we have at this moment: point2d.prototype.@@ADD: {[1, f]} point2d.prototype.@@ADD_R: {[1, f]} point3d.prototype.@@ADD: {[1, f], [2, g]} point3d.prototype.@@ADD_R: {[1, f], [2, g]} ! // What does point3d(1, 0, 0) + point3d(0, 1, 0) do? {[1,f], [2,g]} intersect {[1,f], [2,g]} => g, more specific wins
  • 18. The (Other) Frame Problem // P1: Primitives, e.g. strings, are wrapped via the corresponding // scoped constructor/converter function, e.g. String. String.prototype.len = function () { return this.length; } ! // Suppose otherFrame.str is “LOL”, the primitive string value: alert(otherFrame.str.len()); // 3 ! // Value objects are objects, that’s the price of extensibility. int64.prototype.digits = function () { … } ! alert(otherFrame.int64(42).digits()) // throws, method missing ! // P2: As noted last time, cross-frame/realm binary ops fail: let three = 1L + otherFrame.int64(2); // throws, no such method ! // // // // Possible solutions: 1. Live with it, frames/realms (should be) more isolated FTW 2. Proxy as if across a membrane, isolation with mediation FTW 3. Memoize aggressively (hash-cons); solves P2, not P1 in full
  • 19. Healing the Old Wounds // Primitives: built-in magic, not extensible, auto-wrap via scope // Reference Objects: user-extensible, conversions not operators // Value Objects: by-value semantics; multimethod dyadic operator, // unary operator, suffix, and typeof extensibility ! // Idea: enable the JS hacker to bless primitives as value objects // and thereby opt into value object operator semantics. ! value value value value class class class class null; boolean; number; string; // // // // typeof null == “null” false != "", true != 1 42 != "42", 1 != true "" != false, “42" != 42, [] + “" throws ! // ‘value class string;’ will require explicit .toString() calls! ! use value sanity; // all of the above, a shorthand; no way to // “undeclare”; upgrade your Realm!
  • 20. Thanks / Q&A Brendan Eich brendan@mozilla.com @BrendanEich ! Ecma TC39 January 2014