Comments (6)
Can you use {-# OPTIONS_GHC -Wno-x-partial #-}
in these files to assert “I acknowledge these warnings, no need to show them to my users?”
from microlens.
Sure. But I'd like standard warnings (-Wall
) to be those which are fixable rather than those that I have to switch off. (Pertaining to the discussion at https://gitlab.haskell.org/ghc/ghc/-/issues/23934.)
If I chose to opt into -Wx-partial
, I would have to switch it off for Internal.hs
.
It would be better if I could switch a warning off on a finer level, e.g. just for one definition rather than for the whole file. (In this case, though, I wouldn't need that fine control.)
from microlens.
Its a warning, not an error, so I think it's expected that in some cases the “fix” is telling the compiler they you know what you are doing (possibly by turning off the warning).
Finer grained control would be nice indeed, for now we have it at the module level.
from microlens.
Sure. But I'd like standard warnings (-Wall) to be those which are fixable rather than those that I have to switch off.
Looking at the code in question:
-- | On @template-haskell-2.11.0.0@ or later, if a 'GadtC' or 'RecGadtC' has
-- multiple 'Name's, the leftmost 'Name' will be chosen.
instance HasName Con where
name f (NormalC n tys) = (`NormalC` tys) <$> f n
name f (RecC n tys) = (`RecC` tys) <$> f n
name f (InfixC l n r) = (\n' -> InfixC l n' r) <$> f n
name f (ForallC bds ctx con) = ForallC bds ctx <$> name f con
#if MIN_VERSION_template_haskell(2,11,0)
name f (GadtC ns argTys retTy) =
(\n -> GadtC [n] argTys retTy) <$> f (head ns)
name f (RecGadtC ns argTys retTy) =
(\n -> RecGadtC [n] argTys retTy) <$> f (head ns)
#endif
I suspect that this is a bug - the form of having multiple names would be:
data GadtType where
GadtCon1, GadtCon2 :: Int -> CHar -> GadtType
But this would collapse that into a single constructor.
I do think you're guaranteed to have at least one Name
, so head ns
is safe. You can nevertheless provide a better error message to end users with:
case ns of
[] -> error "Provided an empty list of names in a GadtC, should never happen"
(n:_) -> n
-- or,
fromMaybe (error "Provided an empty list of names in a GadtC") $ listToMaybe ns
These are fixes, since error
doesn't have the partiality warning.
from microlens.
@parsonsmatt wrote:
I suspect that this is a bug - the form of having multiple names would be:
data GadtType where GadtCon1, GadtCon2 :: Int -> CHar -> GadtTypeBut this would collapse that into a single constructor.
Thanks @parsonsmatt, for this alert!, this should be investigated. Ping @stevenfontanella.
I do think you're guaranteed to have at least one
Name
, sohead ns
is safe.
Yes it is safe here.
You can nevertheless provide a better error message to end users with:
...
These are fixes, sinceerror
doesn't have the partiality warning.
One should only use head
where it is safe to use it.
Of course, it is better if we can prove this safety by using non-empty lists.
The x-partial
warnings can be understood as a nudge to refactor code to use non-empty lists where possible.
However, here it is not possible because it would be upstream's job to do this first.
I am not sure that replacing head
by error
gains us much (except for maybe getting a more precise information where the invariant failed, but I thought head
can now deliver this through the call stack, can't it?).
Isn't it even going in the wrong direction, by removing this nudge?
from microlens.
I think the larger issue is that HasName Con => Lens' Con Name
is just wrong. I think you need a separate class HasNames
which offers a Traversal
instead of a Lens
.
(except for maybe getting a more precise information where the invariant failed, but I thought head can now deliver this through the call stack, can't it?).
It looks like head
in GHC 9.2 and prior did not include callstack information. But GHC 9.4 and above will render a callstack.
The x-partial warnings can be understood as a nudge to refactor code to use non-empty lists where possible.
That is one understanding. There are many ways to avoid using partial functions, and I gave a few other alternatives.
In virtually all cases, I think writing fromMaybe (error "my specific and cool error message") . listToMaybe
is better than writing head
. *** Exception: Prelude.head: empty list
is almost totally useless, and knowing where head
is called is only slightly better - you'd be pointed to this definition in microlens
, and since name
lacks a HasCallStack
constraint, you wouldn't know what actually called the function that caused the problem without doing some digging.
But that's getting into the weeds on this specific case, where the real bug fix is to delete the instance, or warn on the instance, or introduce class HasNames con where names :: Traversal' con Name
.
from microlens.
Related Issues (20)
- microlens-th-0.4.3.7 fails to build with th-abstraction-0.3.*
- Port the HashMap and Set alterF improvements
- Add _Show HOT 4
- Add worded and lined HOT 2
- Add more FieldX instances for tuples HOT 3
- `Each` instance for Set missing HOT 3
- Min version of GHC supported?
- Drop support for GHC < 7.9 and base < 4.8? HOT 3
- Support mtl 2.3 and transformers 0.6 HOT 2
- Support for mtl-2.3 HOT 3
- Permit text-2.0 HOT 4
- Add tags for releases HOT 11
- `microlens-th`: support GHC 9.6 HOT 2
- th-abstraction 0.5 not supported HOT 1
- Version on Hackage does not compile with ghc-9.6.1 HOT 4
- Change might be needed for TemplateHaskell shipped with GHC 9.8 HOT 2
- Missing version bounds for `tagged` and `transformers` in microlens-th
- Updated build times including microlens-pro
- Move `Lens.Micro.ProCompat` into `Lens.Micro.Pro`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from microlens.