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

Bounding allowable type values could leverage immutable reference type checks #3270

Closed
davidroeca opened this issue Apr 27, 2017 · 2 comments

Comments

@davidroeca
Copy link

davidroeca commented Apr 27, 2017

Likely out of scope, but worth considering as a simplifying assumption. Languages in the haskell family have immutable references built into their type systems, and this could enforce safer code. For example, fail the typecheck if reassignment of an immutable variable occurs, or classify the reference as mutable implicitly unless explicitly immutable.

var1 = 1  # type: Immutable[int]
var1 += 1  # typecheck fails; reassigned
var1 = 13  # typecheck fails; reassigned
def f1(x: int) -> int:
    return x
var1 = f1(12)  # typecheck fails; reassigned

Optionally, this could also extend to functionality involving allowed ranges--a number with typed bounds within an explicit range (e.g. IntegerGreaterThanZero), for example, must never be reassigned. The typechecker will have some amount of trouble in edge cases involving class attributes on reassignment. If the programmer wants the typechecker to respect a bounded number constraint, the typechecker could require that the reference is immutable. Type/function signatures/literals would suffice in a type check since assignment is only allowed once. Relates to #3062

EDIT 2:
Literals of int, str, etc. can be assumed as immutable function arguments by default since they're immutable objects, so the typechecker could implicitly be able to know that pow(1, 2) is an int (possible but definitely challenging) - immutable should merely subset.

EDIT:
Found some similar/duplicates, but I guess the main addition here is the link to bounded ints, etc.
#2795
#1214

@davidroeca davidroeca changed the title Explicit immutable references within type system Bounding allowable type values could leverage immutable reference type checks Apr 27, 2017
@ilevkivskyi
Copy link
Member

ilevkivskyi commented Apr 28, 2017

This is mostly a duplicate of #1214

IIUC what you also want to note is that constant types can be useful in conjunction with dependent types, namely we could not only support types that depend on literals but also on constants. Maybe you can add a comment in #1214 or/and in #3062? I am not sure we need to keep this issue open.

@davidroeca
Copy link
Author

Yep, makes sense. Will add a comment

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

No branches or pull requests

2 participants