Should we handle usize
, uint16
, etc? Would it be
wise to annotate number literals with a subtype indicator?
Yes. I think for many applications bit size is important. Iâm personally a fan of this style:
uint
int
u32
i32
f32
etc.
i dont think usize would be useful on scrapscript. scrapscript clearly desires some form of machine-independence, and usize is only relevant for machine-specific things (pointer size). considering that there are no pointers in scrapscript, thereâs no pointer-size dependent math to be done in applications themselves, no?
While generally I agree, pointers and int
/uint
are different types in most languages and different semantics and usage patterns. I typically use int
/uint
when I donât particularly care about the binary representation of the variable and want to let the system decide.
Thereâs 2 paths IMO: int
/uint
are always 64-bits and you keep the bit-specific types (ex. i32
), or you make int
/uint
variable length integers and support a full numeric tower (ie. with âbig integerâ support) and drop the specific width types entirely.
i feel like sized types are definitely too useful to not have them. having sized ints lets us deal with stuff like Protobuf and other binary schemas and parsing things.
another approach is to have a generic ânumeralâ type and allow the system to infer what specific type it should be based on suffix and size. have it always infer the smallest possible size for that expression. this is the approach stuff like zig and odin takes, i believe?
i think it would be best to avoid Javascriptâs âeverything is a floatâ or pythonâs âeverything is a bigintâ, because both of these have undesirable consequences: âeverything is a bigintâ in particular leads to things slowing to a crawl with numerics, which we may want. i believe common lisp provides transparent runtime promotion to bigints from sized integers, but that might be excessively âdynamicâ.
of course, iâm biased towards lower level design, so it might be that these concerns simply arenât relevant to scrapscriptâs aims.
You donât need native support for sized types to support formats like Protobuf, see how Python handles it in the struct
module: you just offload converting from the dynamically sized native types to the intended sized binary representation.
Really not a fan of the JavaScript approach, floats and integers must be separate. On the other hand, I like Lisp/Schemeâs approach to integer/floats: automatic promotion to âBigIntâ/âBigFloatâ as necessary. Itâs a bit more complicated to implement, but makes coding simpler.
do you have links to the handling? not quite understanding âoffload convertingâ
autopromote can be quite simple in certain contexts, if you can detect overflow at runtime - x86 has an âoverflowâ flag, for instance.
Basically you pass in the dynamically sized variable (int/float/whatever) into a function along with some way of representing the desired bitwidth + type + endianness and it outputs the bytes with the corresponding representation.
Iâm generally in favor of autopromotion, regardless of complexity.
hmm, that sounds pretty good as a feature to have for scrapscript anyway, regardless of the choice for integer sizedness
autopromotion and sized types are definitely at odds with each other. sized types with autopromotion would mean that the type of any arithmetic expresson would be, at the very least, a mess.
int8 + int8
âs type would be int8|int16
. in the case of int16+int8
, itâd be int16|int32
, and soforth - each addition would potentially be a promotion.
If you donât support autopromotion, you end up with a bunch of ugly type casts like in Go. If youâre going to encode those types of promotions into the type system just have the type system be naive to the bitwidth of the value and be dynamic
yes! thatâs my point exactly. you can have one, or the other.
no, actually. thereâs another way!
we could have both, a Number type and Integer types.
Number can be a general, promotable, signed, âcomplexâ numeric type. the representation for Number could be immensely flexible! it could include fractions, it could include floats, etc. and then, where precision, and, importantly, performance, is desired, you can âopt inâ to lower, more precise typing - i32, and such. this is the approach that TypedArray in javascript effectively represents. TypedArrays are opt-in performance, but are more annoying to handle than the generic number type. its a tradeoff that you make yourself.
Sure, but if the goal is parser/compiler/language simplicity, having a mega type and more precise types goes against that, no?