Clojure

records do not enforce type hints

Details

  • Type: Defect Defect
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Declined
  • Affects Version/s: Release 1.2, Release 1.3
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None

Description

Specify a record with a field as say ^String, but the constructor won't throw if you pass in a non-String.

Activity

Hide
Alan Malloy added a comment -

I doubt there's any desire to change this. Records only support typehints to enable performant storage of primitive types like ints, not to provide static typing in general. An Object is stored the same way as a String, so there's no need to pay attention to that typehint.

Functions behave the same way: ((fn [^String x] x) :test) works just fine. Only if you use a member method of the hinted type (such as .substring in this example) is an exception thrown then the compiler casts to String in order to call the method.

Show
Alan Malloy added a comment - I doubt there's any desire to change this. Records only support typehints to enable performant storage of primitive types like ints, not to provide static typing in general. An Object is stored the same way as a String, so there's no need to pay attention to that typehint. Functions behave the same way: ((fn [^String x] x) :test) works just fine. Only if you use a member method of the hinted type (such as .substring in this example) is an exception thrown then the compiler casts to String in order to call the method.
Hide
Chip Salzenberg added a comment -

I don't understand how you get from "this is how it is" to "this is how it is meant to be." The compiler can do a better job if the underlying field is properly typed: Type errors can be caught sooner, and cast operations can be omitted on use. This is a worthy enhancement.

Show
Chip Salzenberg added a comment - I don't understand how you get from "this is how it is" to "this is how it is meant to be." The compiler can do a better job if the underlying field is properly typed: Type errors can be caught sooner, and cast operations can be omitted on use. This is a worthy enhancement.
Hide
Timothy Baldridge added a comment -

Alan is correct, the reason type hints exist is to reduce reflection and to allow for the unboxing of primitives. String is not a primitive and therefore type-hinting it only reduces the amount of reflection performed.

It has always been Rich's policy that any type systems used in Clojure should not throw compile time errors, but instead should only enhance the performance of existing code. (see the implementation of Typed Clojure for more info on this).

Closing this issue, as it is by design.

Show
Timothy Baldridge added a comment - Alan is correct, the reason type hints exist is to reduce reflection and to allow for the unboxing of primitives. String is not a primitive and therefore type-hinting it only reduces the amount of reflection performed. It has always been Rich's policy that any type systems used in Clojure should not throw compile time errors, but instead should only enhance the performance of existing code. (see the implementation of Typed Clojure for more info on this). Closing this issue, as it is by design.
Hide
Timothy Baldridge added a comment -

By design.

Show
Timothy Baldridge added a comment - By design.
Timothy Baldridge made changes -
Field Original Value New Value
Resolution Declined [ 2 ]
Approval Not Approved [ 10008 ]
Status Open [ 1 ] Resolved [ 5 ]
Stuart Halloway made changes -
Status Resolved [ 5 ] Closed [ 6 ]

People

Vote (1)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: