Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Make Float extensible #7348

Closed
loldrup opened this issue Apr 7, 2018 · 7 comments
Closed

Make Float extensible #7348

loldrup opened this issue Apr 7, 2018 · 7 comments

Comments

@loldrup
Copy link

loldrup commented Apr 7, 2018

If the Float type was extensible, a language user could easily create distinct types for eg. temperatures, distances, weights.
As of current, such values all have to be merely Floats, giving us no type safety against mixing them up when doing computations.

PS. I know that it's possible to define one's own Floaty type, but I also know that it is quite cumbersome. Simply extending Float would give me all that Floatyness for free.
PPS. F# has ability to mingle Floats of various units. Eg. if I divide a value that has a 'lengthy' unit (eg. meters) by a value that has a 'timy unit' (eg. seconds), I get a length per time type. If I multiply a length by a length, I get a 'length squared' type. If I divide a length by a length, I get a scalar value. That's all quite cool. Could Ceylon support something similar, and would that feature be in good harmony with the ability to extend the Float type?

@gavinking
Copy link
Contributor

If by "extending", you mean "subclassing", that would mean we can't erase the type Float to float in the JVM.

@loldrup
Copy link
Author

loldrup commented Apr 7, 2018

Yes I meant subclassing.

An, most likely, ignorant question: Why would it no longer be possible to type erase Float to float?

@RossTate
Copy link

RossTate commented Apr 7, 2018 via email

@gavinking
Copy link
Contributor

Why would it no longer be possible to type erase Float to float?

Because I could assign a value of type Length to a variable of type Float, but I would lose the concrete type.

@gavinking
Copy link
Contributor

To achieve the F# feature you describe, it’ll take a lot more than just subclassing.

Right, a proper units-of-measure system is really surprisingly complex. For example, F# lets you write numerical functions that abstract over the unit of measure.

@loldrup loldrup closed this as completed Apr 8, 2018
@xkr47
Copy link
Contributor

xkr47 commented Aug 28, 2018

To make things better, simply embedding a Float in a Length class like

shared class Length(shared variable Float val) {}

will take about the same amount of memory as a subclass of Float since the compiler can use the primitive type float to store the value. The downside of course is that you have to refer to the value as:

Length len = Length(5);
print(len.val);

@loldrup
Copy link
Author

loldrup commented Aug 28, 2018 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants