User Tools

Site Tools


codesnippets:recordsyntax

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
codesnippets:recordsyntax [2021/03/20 20:13] – [Risk of namespace colisions without strong coding rules] f2b216codesnippets:recordsyntax [2025/10/08 00:48] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== Record syntax ====== ====== Record syntax ======
  
 +  * algebraic sum type )<sup>1</sup>
 +  * provides access functions
 +  * fields in curly brackets, comma separated
 +
 +  * short example:
 +    * <code Haskell>
 +data MatrixCursor = MatrixCursor { rnRow :: Integer, rnCol :: Integer }
 +</code>
 +
 +----
 +
 +)<sup>1</sup> An algebraic sum type (see also https://en.wikipedia.org/wiki/Algebraic_data_type regarding sum types) is a type with different constructors seperated by ''|''. Record syntax can generate errors, if combined with sum types.
 ===== Advantages===== ===== Advantages=====
  
Line 32: Line 44:
  
 ==== With sum types combined: risk of runtime errors ==== ==== With sum types combined: risk of runtime errors ====
- 
-An algebraic sum type (see also https://en.wikipedia.org/wiki/Algebraic_data_type regarding sum types) is a type with different constructors seperated by ''|''. Record syntax can generate errors, if combined with sum types. 
  
   * example:   * example:
Line 47: Line 57:
 </code> </code>
  
-  * output:+  * output of runtime error:
     * <code>     * <code>
 Test2-exe.exe: No match in record selector nDiameterInMM Test2-exe.exe: No match in record selector nDiameterInMM
Line 55: Line 65:
  
 Fields from records can be extrated by the accessor functions that have been declared within the record. Alternatively, records can be extrated by pattern matching, as well. If you use both with the same name then the pattern matching will shadow the accessor function(s). Fields from records can be extrated by the accessor functions that have been declared within the record. Alternatively, records can be extrated by pattern matching, as well. If you use both with the same name then the pattern matching will shadow the accessor function(s).
 +
 +<WRAP center round info 62%>
 +Risk can be completely excluded by compiling with <wrap em>ghc parameter -Wall</wrap> and cleaning up all warnings.
 +</WRAP>
 +
 +<WRAP center round info 62%>
 +Risk can be mitigated by <wrap em>[[codesnippets:codingconventions#record_syntax|Coding rules]]</wrap>.
 +</WRAP>
 +
 +
  
   * example 1:   * example 1:
Line 62: Line 82:
 main :: IO () main :: IO ()
 main = do main = do
-    print $ nCol xc+    print $ nCol xcToPrint
  
 {-| to be able to print into a 'CharMatrix' {-| to be able to print into a 'CharMatrix'
Line 77: Line 97:
     deriving Show     deriving Show
  
-xc :: MatrixCursor +xcToPrint :: MatrixCursor 
-xc = incMatrixCursorCol 5 $ incMatrixCursorCol 2 $ incMatrixCursorCol 3 $ initMatrixCursor+xcToPrint = incMatrixCursorCol 5 $ incMatrixCursorCol 2 $ incMatrixCursorCol 3 $ initMatrixCursor
  
 incMatrixCursorCol :: Integer -> MatrixCursor -> MatrixCursor incMatrixCursorCol :: Integer -> MatrixCursor -> MatrixCursor
-incMatrixCursorCol n (MatrixCursor nRow nCol) = (MatrixCursor nRow (nCol+n))+incMatrixCursorCol n xc@(MatrixCursor nCol) = (MatrixCursor (nRow xc) (nCol+n))
  
 initMatrixCursor :: MatrixCursor initMatrixCursor :: MatrixCursor
Line 87: Line 107:
 </code> </code>
  
-  * compiler warning, using ''stack ghc -- -Wall ./app/Main.hs'': +  * compiler output (using ''stack ghc -- **-Wall** ./app/Main.hs'')
-    * <code>+    * <code Haskell>
 [1 of 1] Compiling Main             ( app\Main.hs, app\Main.o ) [1 of 1] Compiling Main             ( app\Main.hs, app\Main.o )
  
-app\Main.hs:25:36: warning: [-Wname-shadowing] +app\Main.hs:24:45: warning: [-Wname-shadowing]
-    This binding for `nRow' shadows the existing binding +
-      defined at app\Main.hs:16:+
-   | +
-25 | incMatrixCursorCol n (MatrixCursor nRow nCol) = (MatrixCursor nRow (nCol+n)) +
-                                      ^^^^ +
- +
-app\Main.hs:25:41: warning: [-Wname-shadowing]+
     This binding for `nCol' shadows the existing binding     This binding for `nCol' shadows the existing binding
-      defined at app\Main.hs:18:9+      defined at app\Main.hs:17:13
    |    |
-25 | incMatrixCursorCol n (MatrixCursor nRow nCol) = (MatrixCursor nRow (nCol+n)) +24     incMatrixCursorCol n xc@(MatrixCursor nCol) = (MatrixCursor (nRow xc) (nCol+n)) 
-                                           ^^^^+                                               ^^^^
 Linking app\Main.exe ... Linking app\Main.exe ...
 </code> </code>
 +
 +
  
   * example 2:   * example 2:
     * <code Haskell>     * <code Haskell>
 +module Main where
 +
 +main :: IO ()
 +main = do
 +    print $ nCol xcToPrint
 +
 {-| to be able to print into a 'CharMatrix' {-| to be able to print into a 'CharMatrix'
  
Line 122: Line 143:
     deriving Show     deriving Show
  
-xc :: MatrixCursor +xcToPrint :: MatrixCursor 
-xc = incMatrixCursorCol 5 $ incMatrixCursorCol 2 $ incMatrixCursorCol 3 $ initMatrixCursor+xcToPrint = incMatrixCursorCol 5 $ incMatrixCursorCol 2 $ incMatrixCursorCol 3 $ initMatrixCursor
  
 incMatrixCursorCol :: Integer -> MatrixCursor -> MatrixCursor incMatrixCursorCol :: Integer -> MatrixCursor -> MatrixCursor
Line 136: Line 157:
 [2 of 2] Compiling Main [2 of 2] Compiling Main
  
-app\Main.hs:25:67: error:+app\Main.hs:24:71: error:
     * Couldn't match expected type `MatrixCursor -> Integer'     * Couldn't match expected type `MatrixCursor -> Integer'
                   with actual type `Integer'                   with actual type `Integer'
Line 144: Line 165:
       In the expression: (MatrixCursor (nRow xc) (nCol + n))       In the expression: (MatrixCursor (nRow xc) (nCol + n))
    |    |
-25 | incMatrixCursorCol n xc@(MatrixCursor nRow nCol) = (MatrixCursor (nRow xc) (nCol+n)) +24     incMatrixCursorCol n xc@(MatrixCursor nRow nCol) = (MatrixCursor (nRow xc) (nCol+n)) 
-                                                                     ^^^^^^^+                                                                         ^^^^^^^
 </code> </code>
 +
 +
 +
 +  * example 3 (warning free with ''stack ghc -- **-Wall** ./app/Main.hs''):
 +    * <code Haskell>
 +module Main where
 +
 +main :: IO ()
 +main = do
 +    print $ rnCol xcToPrint
 +
 +{-| to be able to print into a 'CharMatrix'
 +
 +* prefix: xc
 +* instance of Formatting to print the stored characters
 +-}
 +data MatrixCursor = 
 +    MatrixCursor {
 +        -- | the row, starting at 0
 +        rnRow :: Integer, 
 +        -- | the column starting at 0
 +        rnCol :: Integer }
 +    deriving Show
 +
 +xcToPrint :: MatrixCursor
 +xcToPrint = incMatrixCursorCol 5 $ incMatrixCursorCol 2 $ incMatrixCursorCol 3 $ initMatrixCursor
 +
 +incMatrixCursorCol :: Integer -> MatrixCursor -> MatrixCursor
 +incMatrixCursorCol n xc@(MatrixCursor _ nCol) = (MatrixCursor (rnRow xc) (nCol+n))
 +
 +initMatrixCursor :: MatrixCursor
 +initMatrixCursor = (MatrixCursor 0 0)
 +</code>
 +
 +  * output:
 +    * <code>
 +10
 +</code>
 +
 +
 +===== ✎ =====
 +~~DISCUSSION~~
codesnippets/recordsyntax.1616267630.txt.gz · Last modified: (external edit)

Except where otherwise noted, content on this wiki is licensed under the following license: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki