cx icon indicating copy to clipboard operation
cx copied to clipboard

ambiguous referencing same-named funcs from imported packages

Open vidya88 opened this issue 5 years ago • 1 comments

@0pcom commented on Aug 9

Describe the bug

A cx program is defined which contains reference to functions with the same name from imported packages:

package main


import "foo"
import "bar"
import "xyzzy"

func main () {
xyzzy.testprint()
}

package foo

func test1 () {
str.print("Hello World!")
}

package bar

func test1 () {
str.print("Goodbye World!")
}

package xyzzy
import "main"

func testprint () {
main.test1()
}

The result of this program depends on the order the packages are imported

This is non-desireable or ambiguous behavior

To Reproduce run the above program

Expected behavior The program should probably display an error in the case that a func (rather, a pointer to a func name) is redefined by a subsequently imported package.

In lieu of that, It might be better if it was required to directly reference a package function, rather than just referencing main or another package which has already imported the requisite packages.

Desktop

  • OS: Linux
  • CX Version 0.7.1

Additional context

This behavior is different for vars than funcs

package main

import "bar"
import "foo"
import "xyzzy"

func main () {
xyzzy.testprint()
}

package foo
var string1 str = "Hello World!"

package bar
var string1 str = "Goodbye World!"

package xyzzy
import "main"

func testprint () {
str.print(sprintf("String 1: %s", main.string1))
}

when run gives: panic: struct 'string1' not found in package 'main'

it appears while vars must be directly referenced, funcs can be referenced by proxy (i.e. referencing a package which has imported a package containing the func)

vidya88 avatar Sep 23 '19 05:09 vidya88

First part of code, does not give error. But second Additional context code produces same error. Can't call main.string1, but foo.string1 works

archerixx avatar Mar 16 '21 18:03 archerixx