2010-10-18 15 views
8

निम्न कोड के लिए:स्कैला में घोषणा की अवैध शुरुआत क्यों?

> scalac FileOperations.scala 
FileOperations.scala:6: error: illegal start of declaration 
     "file:///" + p.replaceAll("\\", "/") 

क्यों:

package FileOperations 
import java.net.URL 

object FileOperations { 
    def processWindowsPath(p: String): String { 
     "file:///" + p.replaceAll("\\", "/") 
    } 
} 

संकलक एक त्रुटि देता है? कैसे ठीक करना है?

उत्तर

17

आप प्रक्रिया से एक = WindowPath विधि घोषणा खो रहे हैं।

package FileOperations 
import java.net.URL 

object FileOperations { 
    def processWindowsPath(p: String): String = { 
     "file:///" + p.replaceAll("\\", "/") 
    } 
} 
+0

सभी स्केला ट्यूटोरियल लिस्टिंग कि प्रतीक http://www.scala-lang.org/docu/files/ScalaTutorial.pdf – Basilevs

+1

हाँ की याद कर रहे हैं, वहाँ कोड का एक बहुत कुछ है वहां कोई मूल्य नहीं लौटाता है। यदि विधि एक मान देता है, तो आपको = चिह्न की आवश्यकता है। पृष्ठ 8 में लिंक किए गए दस्तावेज़ में पहला उदाहरण है। –

+0

इन फ़ंक्शन वापसी मूल्य को करें? अगर वे नहीं करते हैं तो उन्हें '=' का उपयोग नहीं करना चाहिए। –

7
object FileOperations { 
    def processWindowsPath(p: String): String = { 
    "file:///" + p.replaceAll("\\", "/") 
    } 
} 

एक लापता = नहीं है। स्काला में इस तरह के तरीके परिभाषित कर रहे हैं:

def methodName(arg1: Type1, arg2: Type2): ReturnType = // Method body 
संबंधित मुद्दे