POLA fetch
From Erights
(Difference between revisions)
Kevin Reid (Talk | contribs) (→Proposals: note need to provide default-value behavior) |
m (whoops, inconsistent links) |
||
(One intermediate revision not shown) | |||
Line 10: | Line 10: | ||
* Make the second argument usually an ejector. Ejectors do not return values and are robust against being invoked multiple times; if some other sort of object is being used, its author is more likely to consider the multiple invocation issue. If we do this, then utilities should be provided for default-value behavior. --[[User:Kevin Reid|Kevin Reid]] 11:25, 12 April 2008 (CDT) | * Make the second argument usually an ejector. Ejectors do not return values and are robust against being invoked multiple times; if some other sort of object is being used, its author is more likely to consider the multiple invocation issue. If we do this, then utilities should be provided for default-value behavior. --[[User:Kevin Reid|Kevin Reid]] 11:25, 12 April 2008 (CDT) | ||
+ | |||
+ | == Scribbles == | ||
+ | |||
+ | # Current example (ensuring [[FlexMap]] entry, has the POLA problem discussed here) | ||
+ | def value := map.[[:Category:Message fetch/2|fetch]](key, fn { | ||
+ | map[key] := makeValue() | ||
+ | }) | ||
+ | |||
+ | # works with existing fetch implementations | ||
+ | def value := [[escape]] e { | ||
+ | map.[[:Category:Message fetch/2|fetch]](key, e) | ||
+ | } catch _ { | ||
+ | map[key] := makeValue() | ||
+ | } | ||
+ | |||
+ | # return-value-distinguishing #1 | ||
+ | def value := switch (map.fetch(key)) { | ||
+ | match [value] { value } | ||
+ | match [] { map[key] := makeValue() } | ||
+ | } | ||
+ | |||
+ | # return-value-distinguishing #2, using call-patterns and Haskell-style Maybe | ||
+ | def value := switch (map.fetch(key)) { | ||
+ | match just(value) { value } | ||
+ | match nothing() { map[key] := makeValue() } | ||
+ | } | ||
[[Category:Unresolved design issues]] | [[Category:Unresolved design issues]] | ||
+ | [[Category:Message fetch/2]] | ||
+ | [[Category:Message fetch/1]] |
Latest revision as of 17:15, 11 August 2008
to fetch(key, absentThunk)
The fetch/2
protocol for collections does not obey POLA:
- The collection gets access to the value of absentThunk.
- The collection may invoke absentThunk and yet return something else.
- The collection may invoke absentThunk more than once, or after fetch returns.
Proposals
- Make the second argument usually an ejector. Ejectors do not return values and are robust against being invoked multiple times; if some other sort of object is being used, its author is more likely to consider the multiple invocation issue. If we do this, then utilities should be provided for default-value behavior. --Kevin Reid 11:25, 12 April 2008 (CDT)
Scribbles
# Current example (ensuring FlexMap entry, has the POLA problem discussed here) def value := map.fetch(key, fn { map[key] := makeValue() }) # works with existing fetch implementations def value := escape e { map.fetch(key, e) } catch _ { map[key] := makeValue() } # return-value-distinguishing #1 def value := switch (map.fetch(key)) { match [value] { value } match [] { map[key] := makeValue() } } # return-value-distinguishing #2, using call-patterns and Haskell-style Maybe def value := switch (map.fetch(key)) { match just(value) { value } match nothing() { map[key] := makeValue() } }