SORU
9 Mayıs 2011, PAZARTESİ


Neden bir Dize typeclass bir örnek yapabilir miyim?

Verildi:

data Foo =
  FooString String
  …

class Fooable a where --(is this a good way to name this?)
  toFoo :: a -> Foo

String Fooable bir örnek vermek istiyorum:

instance Fooable String where
  toFoo = FooString

DZD sonra da şikayet ediyor:

Illegal instance declaration for `Fooable String'
    (All instance types must be of the form (T t1 ... tn)
     where T is not a synonym.
     Use -XTypeSynonymInstances if you want to disable this.)
In the instance declaration for `Fooable String'

Bunun yerine eğer [Char] ı kullanın:

instance Fooable [Char] where
  toFoo = FooString

Şikayet DZD:

Illegal instance declaration for `Fooable [Char]'
   (All instance types must be of the form (T a1 ... an)
    where a1 ... an are type *variables*,
    and each type variable appears at most once in the instance head.
    Use -XFlexibleInstances if you want to disable this.)
In the instance declaration for `Fooable [Char]'

Soru:

  • Neden bir typeclass Dize ve örnek yapabilir miyim?
  • DZD eğer fazladan bir bayrak eklersem beni bu konuda haberdar etmeye istekli görünüyor. Bu iyi bir fikir mi?

CEVAP
9 Mayıs 2011, PAZARTESİ


Çünkü String sadece bir türü Diğer [Char], tam uygulama tipi kurucu [] tip Char, yani bu olacak şeklinde ([] Char). formun Char türü olduğundan (T a1 .. an) değişken değildir.

Bu kısıtlama nedeni çakışan örnekleri önlemektir. Örneğin, diyelim ki instance Fooable [Char], ve sonra birisi gelip instance Fooable [a] tanımlanmış bir zaman vardı ki. Şimdi derleyici kullanmak istedim, ve eğer bir hata verecek anlamak mümkün olmayacaktır.

-XFlexibleInstances, herhangi bir şekilde örneklerini tanımlamak olmayacak derleyici için temelde umut ederim.

Size ulaşmak için çalışıyoruz ne bağlı olarak, daha kapsayıcı bir tanımlama olabilir:

newtype Wrapper = Wrapper String
instance Fooable Wrapper where
    ...

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Anthony Le

    Anthony Le

    10 EKİM 2006
  • Creavite

    Creavite

    8 Mart 2009
  • HowcastSportsFitness

    HowcastSport

    11 Mayıs 2011