On 07/27/2015 12:42 AM, Stefan Scott Alexander wrote:
While attempting to extend the Crud demos, I need to do destructuring
on a record of records, eg on a value 'theCols' of type 'colsMeta(int,
string)' (slightly abbreviated below):
[...]
Using just the string "bar" and the value 'theCols', I want to use
pattern-matching and destructuring, to do a kind of lookup on "bar"
(to find the field whose 'Nam' field = "bar"), and then assign two values:
(a) myCol
(b) myColNam
What you are asking for should be possible, using variant types. See the
declaration of [variant], and the operations right underneath it, in
lib/ur/basis.urs. It's an easy task for someone familiar with the Ur
type system, but it's probably impossible without taking the time to
actually learn how that type system works, instead of just applying a
few "design patterns" by pattern matching on code examples. As far as I
know, no one has yet implemented this kind of string-based indexing.
Just extracting the name field is easier and doesn't require variants.
I think the two functions below should do the trick:
fun getCol aNam someCols =
case someCols of
{ colNam = {
Nam = aNam,
Show = sh,
Widget = wg },
otherOuterFields } =>
[ colNam = [
Nam = aNam,
Show = sh,
Widget = wg ] ]
| _ => error
I see two kinds of wishful thinking in this code:
1) Patterns in ML and Haskell, and thus Ur, are /linear/, so you aren't
allowed to mention a preexisting variable name to implement an equality
test, as you do above with [aNam].
2) You've invented a form of pattern matching on records that matches
one field, with an unknown name, and then binds a variable to stand for
all other field values. Ur supports nothing along those lines.
This operation is impossible without using a [folder] to do iteration.
(2) In function getCol, am I using { } and [ ] correctly - ie:
- { } for record *patterns*, and
- [ ] for record *expressions* (values) ?
No. Brackets are only for type-level records. Your code just above
only uses value-level records, which are always written with curly braces.
_______________________________________________
Ur mailing list
[email protected]
http://www.impredicative.com/cgi-bin/mailman/listinfo/ur