-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Experimental] Platform dependent int #9617
Closed
Closed
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Co-authored-by: Sijawusz Pur Rahnama <sija@sija.pl>
* Hide Channel internal implementation methods * Add private/nodoc to more methods and types * Remove deprecated Channel#select
* Array rotate optimization Add special case for array rotate to optimize for small arrays. * fix typo in docs * rotate tmp array is now populated with filler * fix if statement in array#rotate! to use else * use uninitialized StaticArray for array#rotate! optimization * run crystal formatter * use case statment to optimize array#rotate! * fix formatting issue * update array#rotate docs * make array#rotate docs easierto read * add comment for ignore block * add large array for array#rotate * use if statment over case statment * add tests for array#rotate! * change array#rotate! to check for size - 1 * add comment for shifting in array#rotate! * update docs * remove comments
…9615) This avoid creating an intermediate string
* HTTP::Params: `#[]=` replaces all values. * Fix HTTP::Request specs
asterite
changed the title
Platform dependent int
[Experimental] Platform dependent int
Jul 18, 2020
@asterite feel free to push branches. I just suggested that we don't do it by default for every feature/fix branch we create for our PR's. But in specific circumstances like this one, where you might want to split the work differently or receive contributions to your branch, let's do whatever is more appropriate. |
Closed in favor of #9619 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
For #8111
Why?
As our ongoing quest to find the optimal language with also avoiding a massive breaking change, we discussed trying out the experiment of introducing a platform-dependent
Int
type.What's
Int
?Here
Int
is a different type than any other int type, meaning you can't pass anInt64
where anInt
is expected.However, this is based on #9569 and since
Int
will be represented asInt32
orInt64
depending on the platform, any int type that fits insideInt32
can be automatically passed toInt
. That means anInt32
variable can be passed to anInt
restriction.Given that most of the restrictions in the stdlib are
Int
, and becauseInt32
is the usual default int type used everything, changing this has a low chance of breaking everyone's code, assuming the std is fully migrated to this new approach.How to implement this?
Implementing this is a bit tricky. One problem is that
Int
is currently the base type of all ints. Now we want another base type, namelyIntBase
to serve that purpose, andInt
to be a subclass. This information is deeply embedded in the compiler so we must change that there. But we also need to change the stdlib code that assumes these things.To accomplish that there's a new compile-time flag,
platform_dependent_int
that will be true when compiling the new compiler (so you don't need to pass it), but obviously it's false in the current state. With that flag we can control what the stdlib looks like.To make things easier we introduce a couple of aliases:
IntBase
: well, this is actually the base type ifplatform_dependent_int
is true, and it's aliases toInt
if it's falseDefaultInt
: this isInt
whenplatform_dependent_int
is true, andInt32
when it's falseAlso, the type of an integer literal like 1 or 123 is now
Int
in the new approach.Next I tried compiling an empty program. That already didn't compile and so I started changing a lot of
Int32
toDefaultInt
. That means we aren't changing anything ifplatform_dependent_int
is false, but we are making it beInt
if the flag is on. Eventually I managed to compile an empty program (with the default prelude and runtime), so this already works fine:Obvious output:
Oh, a small note: for now
Int
is actually implemented aInt64
regardless of the platform, just to make things easier for me. Changing that toInt32
is very easy.Another note:
to_i
now returnsInt
(or, well,DefaultInt
), butto_i32
returnsInt32
, and these, again, are different types.Next I started to try compiling and running the std specs. So far I got
int_spec.cr
andstring_spec.cr
down. But making things work is very easy! You just try compiling something and the compiler will guide you.How to use it?
Run
make clean crystal
, then you can dobin/crystal spec spec/std/string_spec.cr
and see it pass.What's next?
I'll be continuing to make more and more code compile. I expect this will be a huge diff. Also, don't expect this to go out soon, or even, because it's just an experiment. And I'll do this slowly if I have time.
Rant
I wish we could push branches here. That way I could make a PR that targets that branch and the commits here will be cleaner. I see no reason why we have to restrict ourselves like that. /cc @waj @bcardiff