x-motemen / gore

Yet another Go REPL that works nicely. Featured with line editing, code completion, and more.
MIT License
5.11k stars 148 forks source link

'undefined variableName' in function declaration #181

Closed xkortex closed 4 years ago

xkortex commented 4 years ago

Hi,

I observe undefined: encodedSlice with the following code:

:import strings
:import bytes
const encodeStd = "aBcDeFgHkMnRtUwY"
var encodeSlice = []byte(strings.ToLower(encodeStd))

works fine, then

func fromHexChar(c []byte) (byte, bool) {
    val := bytes.Index(encodeSlice, c)
    if val >= 0 {
        return byte(val), true
    }
    return 0, false
}

fails with undefined: encodedSlice. Yet, using var foo = func(blah){} notation works:

var fromHexChar = func (c []byte) (byte, bool) {
    val := bytes.Index(encodeSlice, c)
    if val >= 0 {
        return byte(val), true
    }
    return 0, false
}

Is this a bug or expected behavior?

itchyny commented 4 years ago

This is an expected behavior and hard to fix.

package main

import "strings"
import "bytes"

func main() {
    const encodeStd = "aBcDeFgHkMnRtUwY"
    var encodeSlice = []byte(strings.ToLower(encodeStd))
    var fromHexChar = func (c []byte) (byte, bool) {
        val := bytes.Index(encodeSlice, c)
        if val >= 0 {
            return byte(val), true
        }
        return 0, false
    }
    fromHexChar([]byte("a"))
}

compiles while

package main

import "strings"
import "bytes"

func main() {
    const encodeStd = "aBcDeFgHkMnRtUwY"
    var encodeSlice = []byte(strings.ToLower(encodeStd))
    _ = encodeSlice
    fromHexChar([]byte("a"))
}

func fromHexChar(c []byte) (byte, bool) {
    val := bytes.Index(encodeSlice, c)
    if val >= 0 {
        return byte(val), true
    }
    return 0, false
}

not.