How to disable all implicit conversion of primitive types?

OlivierBlanvillain

Scala seems to behave like Java when it comes to the magic conversion of primitives:

val a: Int = 1
val b: Double = 2.3
println(a + b) // 3.3 
println(Math.max(a, b)) // 2.3

More often than not, this has been a source of bugs in my code. Is there a way to disable these implicit conversions so that my example give a compilation warnning/error? I would really rather have to write

print(a.toDouble + b)
println(Math.max(a.toDouble, b))

every single time I need such conversions.

Alexey Romanov

Use WartRemover. A wart like that isn't built-in, but could be written (see "Writing Wart Rules" in README). Though now that I think, it's probably more work than I thought initially.

scalac also has -Ywarn-numeric-widen option (together with -Xfatal-warnings to turn the warnings to errors), but I don't know if there are any cases not covered by it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Why is implicit conversion not ambiguous for non-primitive types?

From Dev

g++ - how do I disable implicit conversion from 0 to pointer types?

From Dev

g++ - how do I disable implicit conversion from 0 to pointer types?

From Dev

How to restrict implicit conversion of typedef'ed types?

From Dev

Overriding conversion of primitive data types

From Dev

How "==" works on primitive types

From Dev

Is there a way to add conversion operators to primitive types?

From Dev

Disable implicit conversion of quoted values to integer

From Dev

Disable implicit conversion of quoted values to integer

From Dev

How to fix implicit conversion to Applicative?

From Dev

How to get rid of this implicit conversion?

From Dev

CATextLayer - how to disable implicit animations?

From Dev

CATextLayer - how to disable implicit animations?

From Dev

C++ overload ambiguity: conversion versus promotion with primitive types

From Dev

Why is this implicit conversion (between different pointer types) valid?

From Dev

Inconsistent behaviour of implicit conversion between unsigned and bigger signed types

From Java

How to avoid duplication of code regarding primitive types?

From Java

How are primitive data types made in C#?

From Dev

How does Java store primitive types in RAM?

From Dev

How can we use .class on primitive types?

From Dev

How do pointers work with primitive types in Java?

From Dev

How array of primitive types work in Java?

From Dev

how to return tuple of primitive data types in dapper

From Dev

How to avoid duplication of code regarding primitive types?

From Dev

How are the primitive data types in java defined/written?

From Dev

How method-overloading and primitive types works?

From Dev

How can we use .class on primitive types?

From Dev

How to store primitive data types in hbase and retrieve

From Dev

How to add an 'opApply' on primitive types in D?

Related Related

  1. 1

    Why is implicit conversion not ambiguous for non-primitive types?

  2. 2

    g++ - how do I disable implicit conversion from 0 to pointer types?

  3. 3

    g++ - how do I disable implicit conversion from 0 to pointer types?

  4. 4

    How to restrict implicit conversion of typedef'ed types?

  5. 5

    Overriding conversion of primitive data types

  6. 6

    How "==" works on primitive types

  7. 7

    Is there a way to add conversion operators to primitive types?

  8. 8

    Disable implicit conversion of quoted values to integer

  9. 9

    Disable implicit conversion of quoted values to integer

  10. 10

    How to fix implicit conversion to Applicative?

  11. 11

    How to get rid of this implicit conversion?

  12. 12

    CATextLayer - how to disable implicit animations?

  13. 13

    CATextLayer - how to disable implicit animations?

  14. 14

    C++ overload ambiguity: conversion versus promotion with primitive types

  15. 15

    Why is this implicit conversion (between different pointer types) valid?

  16. 16

    Inconsistent behaviour of implicit conversion between unsigned and bigger signed types

  17. 17

    How to avoid duplication of code regarding primitive types?

  18. 18

    How are primitive data types made in C#?

  19. 19

    How does Java store primitive types in RAM?

  20. 20

    How can we use .class on primitive types?

  21. 21

    How do pointers work with primitive types in Java?

  22. 22

    How array of primitive types work in Java?

  23. 23

    how to return tuple of primitive data types in dapper

  24. 24

    How to avoid duplication of code regarding primitive types?

  25. 25

    How are the primitive data types in java defined/written?

  26. 26

    How method-overloading and primitive types works?

  27. 27

    How can we use .class on primitive types?

  28. 28

    How to store primitive data types in hbase and retrieve

  29. 29

    How to add an 'opApply' on primitive types in D?

HotTag

Archive