Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

More about types and type inference.

  • Loading branch information...
commit 620a6390e93ce22b247b3e599305cd1a08dac4e3 1 parent 818eb6f
Bryan O'Sullivan authored June 21, 2011

Showing 1 changed file with 26 additions and 4 deletions. Show diff stats Hide diff stats

  1. 30  Database/MySQL/Simple.hs
30  Database/MySQL/Simple.hs
@@ -386,9 +386,11 @@ fmtError msg q xs = throw FormatError {
386 386
 -- >
387 387
 -- > import Database.MySQL.Simple
388 388
 -- >
  389
+-- > hello :: IO Int
389 390
 -- > hello = do
390 391
 -- >   conn <- connect defaultConnectInfo
391  
--- >   query_ conn "select 2 + 2"
  392
+-- >   [Only i] <- query_ conn "select 2 + 2"
  393
+-- >   return i
392 394
 --
393 395
 -- A 'Query' value does not represent the actual query that will be
394 396
 -- executed, but is a template for constructing the final query.
@@ -449,11 +451,27 @@ fmtError msg q xs = throw FormatError {
449 451
 -- The same kind of problem can arise with string literals if you have
450 452
 -- the @OverloadedStrings@ language extension enabled.  Again, just
451 453
 -- use an explicit type signature if this happens.
  454
+--
  455
+-- Finally, remember that the compiler must be able to infer the type
  456
+-- of a query's /results/ as well as its parameters.  We might like
  457
+-- the following example to work:
  458
+--
  459
+-- > print =<< query_ conn "select 2 + 2"
  460
+--
  461
+-- Unfortunately, while a quick glance tells us that the result type
  462
+-- should be a single row containing a single numeric column, the
  463
+-- compiler has no way to infer what the types are.  We can easily fix
  464
+-- this by providing an explicit type annotation:
  465
+--
  466
+-- > xs <- query_ conn "select 2 + 2"
  467
+-- > print (xs :: [Only Int])
  468
+
452 469
 
453 470
 -- $only_param
454 471
 --
455 472
 -- Haskell lacks a single-element tuple type, so if you have just one
456  
--- value you want substituted into a query, what should you do?
  473
+-- value you want substituted into a query or a single-column result,
  474
+-- what should you do?
457 475
 --
458 476
 -- The obvious approach would appear to be something like this:
459 477
 --
@@ -461,13 +479,17 @@ fmtError msg q xs = throw FormatError {
461 479
 -- >     ...
462 480
 --
463 481
 -- Unfortunately, this wreaks havoc with type inference, so we take a
464  
--- different tack. To represent a single value @val@ as a parameter, write
465  
--- a singleton list @[val]@, use 'Just' @val@, or use 'Only' @val@.
  482
+-- different tack. To represent a single value @val@ as a parameter,
  483
+-- write a singleton list @[val]@, use 'Just' @val@, or use 'Only'
  484
+-- @val@.
466 485
 --
467 486
 -- Here's an example using a singleton list:
468 487
 --
469 488
 -- > execute conn "insert into users (first_name) values (?)"
470 489
 -- >              ["Nuala"]
  490
+--
  491
+-- A row of /n/ query results is represented using an /n/-tuple, so
  492
+-- you should use 'Only' to represent a single-column result.
471 493
 
472 494
 -- $in
473 495
 --

0 notes on commit 620a639

Please sign in to comment.
Something went wrong with that request. Please try again.