मैंने एक हफ्ते पहले हास्केल का अध्ययन शुरू किया और एक अजीब समस्या है। मैंने एक साधारण डेटा प्रकार बनाया और इसे कंसोल में दिखाना चाहता हूं। मैंने अपने प्रकार के 2 रचनाकारों के लिए 2 फ़ंक्शन बनाए। यदि मैं 2 तर्कों के साथ एक कन्स्ट्रक्टर का उपयोग करता हूं तो संकलक फ़ंक्शन कॉल कर सकता है। लेकिन यह एक और समारोह नहीं कह सकता है जिसे 1 तर्क के साथ एक निर्माता को पकड़ना चाहिए।विभिन्न डेटा कन्स्ट्रक्टर के लिए कार्य
module Main (
main
) where
data MyContainter a b = FirstVersion a b
| SecondVersion a
deriving(Show,Eq)
showContainer (FirstVersion a b) = show b
showContainer (SecondVersion a) = show a
--startF = showContainer (FirstVersion 1 2) -- it works
startF = showContainer (SecondVersion 1) -- it doesn't work
main = putStr startF
compilers बताता है:
Ambiguous type variable `a0' in the constraint:
(Show a0) arising from a use of `showMaybe'
Probable fix: add a type signature that fixes these type variable(s)
In the expression: showMaybe (SecondVersion 1)
In an equation for `startF': startF = showMaybe (SecondVersion 1)
ऐसा क्यों है कि बता करता है? मैंने सीधे (सेकेंडवर्सन 1) बनाया और समझ में नहीं आया कि कंपाइलर शोकंटनर (सेकेंडवर्सन ए) क्यों नहीं कहता है।
showContainer :: (Show a, Show b) => MyContainer a b -> String
लेकिन जब आप SecondVersion 1
गुजरती हैं, यह पता नहीं है क्या b
है, क्योंकि b
के किसी भी प्रकार के लिएSecondVersion 1
काम करता है:
यह एहसास करने में मदद करता है कि त्रुटि संदेश में प्रकार चर 'a0'' बी' जैसा ही है, और 'a' के साथ कुछ भी नहीं है। (संकलक बस उस नाम को चुनने के लिए हुआ क्योंकि 'शो' क्लास' ए' नाम का उपयोग करता है)। – hammar