ClojureDart icon indicating copy to clipboard operation
ClojureDart copied to clipboard

Fail at compile time on dubious casts

Open lambdina opened this issue 2 years ago • 3 comments

Reproducing the mistake:

:appBar (m/AppBar :title "this is an invalid way")

In the documentation, :title expects a Text widget.

The compiler does not emitting any errors, instead, it casts the string to a Widget, knowing that the cast won't work.

Suggestion: not doing the cast and emit an error at compile time

lambdina avatar May 23 '22 08:05 lambdina

I actually get the expected error:

Error: Expected a value of type 'Widget?', but got one of type 'String'
    at Object.throw_ [as throw] (http://localhost:50992/dart_sdk.js:5067:11)
    at Object.castError (http://localhost:50992/dart_sdk.js:5026:15)

nrfm avatar May 23 '22 09:05 nrfm

@nrfm what @NaadiQmmr is complaining about is that she gets a runtime error while it could have been a compilation error.

The hard thing is deciding when to raise this error. Some classes (like String) can't be extended so for them it's easy. For constructors call too. But in general we must not crash just because a type hint appears incompatible. As a later def type may fill the gap.

A warning may be enough.

With possibly a ^:dont-warn... or be stricter in general and have a ^:trust-in-me for casts which seems wrong.

cgrand avatar May 23 '22 10:05 cgrand

Ah. I get it. Thanks for the explanation.

nrfm avatar May 23 '22 11:05 nrfm