Typed properties beside Typed Objects
Andrea Giammarchi wrote:
Backward Compatible
Having an implicit default to
Any
means that thistype
descriptor property could be simply ignored or implemented as always Any in ES5 or ES6 compatible engines but a simple library that wrapsObject.create
,
Still not making sense.
If the types can be ignored, then they have no effect when evaluating new code as well as old. Anything else is not backward compatible.
Please slow down and show what you want to happen in new code that differs because of a .type member of a property descriptor. Show a use-case.
Here an example based on partially typed prototype generating normal dynamic instances with typed properties.
var int32 = 0, string = '';
function UniqueIDGenerator(prefix, suffix) {
this.i = 0;
this.prefix = prefix || '';
this.suffix = suffix || '';
}
Object.defineProperties(
UniqueIDGenerator.prototype,
{
i: {
writable: true,
type: int32
},
prefix: {
writable: true,
type: string
},
suffix: {
writable: true,
type: string
},
new: {
value: function () {
this.i++;
return this.toString();
}
},
toString: {
value: function () {
return ''.concat(
this.prefix,
this.i,
this.suffix
);
}
}
}
);
var uid = new UniqueIDGenerator('pre', 'suf');
'' + uid; // pre0suf
'' + uid.new(); // pre1suf
// dynamic shape
uid.whatever = 123; // OK
// guarded properties
uid.pre = 123; // throws new Error, pre is not string
Does this make any sense? It was a quick example but basically any prototype you know in JS could have some property pre defined upfront with a type ... imagine a Point2D "class" with also methods and not just x and y.
Last thing, for backward compatible it means that these properties will work regardless in ES5 and ES6 but without guarded types ... guard that has been already implemented by my shim/experiment with these kind of descriptors.
It's like adding withCredentials
to XHR when it won't affect older
implementations (or anything new that is backward compatible, syntactically
speaking, and could be polyfilled somehow)
Hope I've explained better myself.
As quick recap: this is about typed properties for generic JS objects, not only StructType/statically shaped one, if this makes any sense.
Opening a new thread since the topic chosen for the enriched descriptors was misleading and I didn't manage to explain what I was trying to achieve.
Typed Objects Using
StructType
, as described here we can define static "classes" or "types" so thatconst Point2D = new StructType({x: int32, y: int32});
This means that every
Point2D({x: 1, y: 2})
object will have some sort of special descriptor for those two properties, at least behind the exposed JS scene.Accordingly, I wonder if it makes sense to introduce another descriptor property named
type
where such type could be any type accepted byStructType
properties.These types are described here and it could be used as descriptor property so that:
const Point2D = new StructType({x: int32, y: int32}); let p0 = Point2D({x: 1, y: 2}); Object.getOwnPropertyDescriptor(p0, 'x'); /* { writable: true, enumerable: true, configurable: false, type: int32 // <=== this ! } */
In a world where optional JS types are introduced, if specified, implicitly defaulting to
Any
if missing, it would ideally be possible to define typed properties in dynamic shaped objects too, granting type consistency for the developer, and giving the ability to the JS engine to optionally optimize operations per most common types such string, boolean, or any sort of number or statically shaped entity so that not onlyStructType
would benefit from a theoretical performance boost.Backward Compatible
Having an implicit default to
Any
means that thistype
descriptor property could be simply ignored or implemented as always Any in ES5 or ES6 compatible engines but a simple library that wrapsObject.create
,Object.defineProperty
, andObject.defineProperties
upfront and can be simply not used in production could bring already these types in all ES5+ engines, as this library does already.As Summary I hope I've better explained what I meant in the other thread when I was talking about extra properties ... not because I wanted my own custom properties going all over the place with or without Proxies, simply to bring these extra properties to the plate.
type
is only the first of those proposed in there, but it's also the most important one.Thanks for your thoughts, questions, or possible improvements on this.
Best