Skip to content
CCPEDIAby Unity Nodes
App Development

Why do we need a "for" clause in an interface instance declaration?

8 posts256 views1 likesLast activity Nov 2022

  1. Leonid_RozenbergOP

    An interface instance clause
    interface instance MyInterface for NameOfTemplate where

    can only occur inside of a template definition, and it can only refer to the same template. It seems redundant, or do you envision lifting the second restriction?

  2. Gary_Verhaegen 1
    module Main where
    
    template T with
        s: Party
      where
        signatory s
    
    data View = View { a : Int }
      deriving (Show, Eq)
    
    interface I where
      viewtype View
      interface instance I for T where
        view = View 0
    
  3. Leonid_Rozenberg

    Sure but for the instances inside a template, it is redundant?

  4. Gary_Verhaegen

    I think the point is that the syntax as it exists right now is independent of whether it’s in a template or an interface.

    That means you can move it around easily while refactoring, and you don’t have to learn two different syntaxes for the same thing.

    That’s clearly not the only way to do it, and interfaces are still in preview so there’s time to change them to some extent I think, but I personally like that “location independence” property.

  5. Gary_Verhaegen

    (Note that by the same argument the interface name is also redundant when the block appears within an interface.)

  6. cocreature

    Gary is right, the syntax was chosen such that we can use the the same syntax regardless of whether it is in an interface or a template and in return we accepted some redundancy.

  7. Leonid_Rozenberg

    Ok, cool, that’s sensible.

    I guess I would have preferred a shorter version of

    interface instance [I|T]...
    

    to keep things terse, but I could understand how it complicates things downstream.

  8. Leonid_Rozenberg

    Thinking about this

    Gary_Verhaegen:

    That means you can move it around easily while refactoring, and you don’t have to learn two different syntaxes for the same thing.

    a little bit more. I think this cuts both ways. In a large code base you would want to have the ability to quickly search for the difference.

    To support both approaches, what if we make the redundant parts optional? So that in an template I can write

    interface instance MyInterface where
    

    and in an interface I can write

    interface for MyTemplate where
    

    ?