From: Oren Ben-K. <or...@ri...> - 2001-06-05 07:20:17
|
Brian Ingerson [mailto:briani@ActiveState.com] wrote: > I think of 'real' as more of a special type. What about Foo? > > If we have: > > key : !Foo a fooish string > > That means I need to generate a Foo object which is a > subclass of a YAML > object. If 'Foo' is a recognized class, you just build a Foo object. 'Foo' doesn't have to derive from any YAML class. If 'Foo' is an *unrecognized* class, then you build a YAML-object object. The 'YAML-object' class (or whatever we'll call it) behaves like both a string an a map. In an "ideal" language (Sather, anyone? :-) all you'd need to do is write: class YAML-OBJECT: public MAP, public STRING { some tricks to make it work }; Again, there's no new class created; the above class is used only once, throughout the system. > All nodes will be objects which are subclasses of YAML. > That's because of this substitutability thing. Nope. Let's enumerate: map: % key: value Is de-serialized to a normal, built-in hash. list: @ value value Is de-serialized to a normal, built-in list. key : value Is de-serialized into a normal string. map: % = : value And: key : !class value Are de-serialized into a YAML-object. That is, *Only* if you are actually *using* substitutability, you pay for it (The good old C++ notion of "don't use, don't pay"). That's why I don't like Clark's idea of a list having a default value of the first element; it forces one to always pay this price, when for most lists it isn't required. I also don't think it is useful to evolve a scalar into a list; that's not part of the "color" idiom, anyway. Clark? > If we have: > key : !Foo a fooish string > > ... my Perl might look like: > > $y = YAML->parse('doc1.yaml'); > print $y->getValue('key')->getValue; Ideally, if a proper YAML-object class can be written in Perl, it would look like; # Access to "default" value of 'key' - 'fooish string' print $y->{key}; # Access to value of 'key' as a map, to the class # member - 'Foo'; print $y->{key}->{'!'}; # Access to value of key as a map, to the value # member - 'fooish string'. print $y->{key}->{'='}; The question is: is this possible in Perl? I suspect it can... > Even this isn't simpler: > > key : a fooish string > > You would still need the exact same code as above [i.e., the same code as accessing a YAML-object] On that we agree; the question is, how simple can we make this code be, and how efficient *in the normal case*. I'd rule out any implementation where the above isn't de-serialized into a built-in Perl string. > Now Perl has a wonderful "tie" facility that may allow me to > expose the > simpler syntax. I'm not sure how well it will work out yet. But the > underlying implementation will be sloooow. I've no problem with an implementation where an *unknown*-class-annotated value is working slower. Makes sense? > So I'm willing to try this, but I'm skeptimistic about the usability > from Perl. This will never become a good alternative to Data::Denter. > (Long live Data::Denter :) I think that YAML *must* be usable from Perl. Let's make it so! > Perhaps a better alternative is to only provide > substitutability within > classes. Therefore all classes will be first class YAML classes > (round-tripping, substitutable, etc), but regular data will never get > upgraded into a class. That way, maps are just maps like > before. Hmm. I really like that. I'm not certain exactly what you mean... > Maybe that's what you guys were already thinking? I'll have to go back > and check. It does sound a bit like what I described above... The only problem with the above scheme is that given a normal Perl map, suppose I "innocently" add the key '=' to it. Ideally, it should polymorph into a YAML-object. Deleting that key should cause it to polymorph back into a normal map. It seems to me that either of these operations is momentous enough to warrant an explicit call, such as: $yaml_object = make_yaml_object_from_normal_map($normal_map, $value); ($value, $normal_map) = make_normal_map_from_yaml_object($yaml_object); Acceptable? > > (I'm using '!' for class so we can use '#' for comments - I > think that's a > > more intuitive use). > > That makes sense. OK, I'm going to do an update to the draft (nothing about the substitutability yet, just the syntactical issues we agreed on) - I'll throw that in as well. Have fun, Oren Ben-Kiki |