Skip to content
Discussions/App Development/Why do we need a "for" clause in an interface instance declaration?Forum ↗

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

App Development8 posts216 views1 likesLast activity Nov 2022
LE
Leonid_RozenbergOP
Nov 2022

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?

GA
Gary_Verhaegen
Nov 2022
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
LE
Leonid_Rozenberg
Nov 2022

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

GA
Gary_Verhaegen
Nov 2022

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.

GA
Gary_Verhaegen
Nov 2022

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

CO
cocreature
Nov 2022

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.

LE
Leonid_Rozenberg
Nov 2022

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.

LE
Leonid_Rozenberg
Nov 2022

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

?

← Back to Discussions