Skip to content
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

[Feature] Add integer type #443

Open
6 tasks
Ph0enixKM opened this issue Sep 3, 2024 · 7 comments
Open
6 tasks

[Feature] Add integer type #443

Ph0enixKM opened this issue Sep 3, 2024 · 7 comments
Assignees
Labels
compiler documentation Improvements or additions to documentation enhancement New feature or request hacktoberfest syntax

Comments

@Ph0enixKM
Copy link
Member

Is your feature request related to a problem? Please describe.
Amber has no way to represent integer types. Sometimes we may want to accept type that can be represented as an integer. Here are main pros:

  • Not every environment comes with bc installed
  • Sometimes some calculations have to be done quickly and so calling bc will only slow things down

Describe the solution you'd like

  • Add Int data type
  • Numeric value that contains . symbol is a Num
  • Numeric value that is an integer is an Int
  • Accessor expression accepts an Int instead of Num
  • Ranges now work on Int instead of Num
  • Update documentation covering Int data type

Describe alternatives you've considered
N/A

Additional context
#70

@Ph0enixKM Ph0enixKM added documentation Improvements or additions to documentation enhancement New feature or request syntax compiler labels Sep 3, 2024
@Ph0enixKM Ph0enixKM self-assigned this Sep 3, 2024
@github-project-automation github-project-automation bot moved this to 🆕 New in Amber Project Sep 3, 2024
@hdwalters
Copy link
Contributor

hdwalters commented Sep 3, 2024

Referring to #442, would you need union type support, e.g. for the standard library function that sums all numbers:

pub fun sum(list: [Num | Int]): Num {
    return unsafe $echo "{list}" | awk '\{s=0; for (i=1; i<=NF; i++) s+=\$i; print s}'$ as Num
}

Alternatively, what would it take to make the engine support something like class inheritance, where Num extends Int?

@Ph0enixKM
Copy link
Member Author

Ph0enixKM commented Sep 3, 2024

@hdwalters I think that Int should be able to be implicitly casted to Num because it actually is Num but without the fraction part. When it comes to classes - there is no such thing right now in Amber and probably we will have something else than classes in this language. Something that is more specific to the problem that Amber solves. The idea is yet to be crystallised

This means that we can just do:

pub fun sum(list: [Num]): Num {
    return unsafe $echo "{list}" | awk '\{s=0; for (i=1; i<=NF; i++) s+=\$i; print s}'$ as Num
}

Since Int can be easily treated as Num without any modifications (at least for now)

@b1ek
Copy link
Member

b1ek commented Sep 3, 2024

i think we should add it as an opt-in to tell the compiler to optimize away some things, while still setting whole numbers as Num's by default. i assume those two will be incompatible together and will be mostly unstable for now, so i'd like to avoid that for now

@b1ek
Copy link
Member

b1ek commented Sep 3, 2024

i suppose that every math operation with Int will use bash's (( 1 + 1 )) syntax.

how do we handle situations when an Int is doing operations with Num? are we doing bc again? throw a compile error for incompatible types?

@Ph0enixKM
Copy link
Member Author

Int + Num - then we use bc (or awk)

@Ph0enixKM
Copy link
Member Author

@b1ek How would you handle this situation under the hood:

let val = get_random_digit() as Num
echo val + 1

Where get_random_digit returns a Text with a digit symbol and we want to now treat it as Int internally. The value of this function is known at runtime and we cannot assume that it's Int in the compiler because nothing suggests that other than the name of this function.

@b1ek
Copy link
Member

b1ek commented Sep 4, 2024

i'd have the user cast the types to one and throw a compile error if they mismatch

@Ph0enixKM Ph0enixKM added this to the Amber 0.5.0-alpha milestone Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler documentation Improvements or additions to documentation enhancement New feature or request hacktoberfest syntax
Projects
None yet
Development

No branches or pull requests

3 participants