Skip to content

Commit

Permalink
Make separator a required argument
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdrag00nv2 committed Sep 9, 2023
1 parent 72fdad6 commit b92d5e1
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 25 deletions.
13 changes: 3 additions & 10 deletions runtime/interpreter/value_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ func stringFunctionFromCharacters(invocation Invocation) Value {
return NewUnmeteredStringValue(builder.String())
}

var StringTypeJoinDefaultSeparator = NewUnmeteredStringValue(",")

func stringFunctionJoin(invocation Invocation) Value {
stringArray, ok := invocation.Arguments[0].(*ArrayValue)
if !ok {
Expand All @@ -123,14 +121,9 @@ func stringFunctionJoin(invocation Invocation) Value {
return stringArray.Get(inter, invocation.LocationRange, 0)
}

var separator *StringValue
if len(invocation.Arguments) > 1 {
separator, ok = invocation.Arguments[1].(*StringValue)
if !ok {
panic(errors.NewUnreachableError())
}
} else {
separator = StringTypeJoinDefaultSeparator
separator, ok := invocation.Arguments[1].(*StringValue)
if !ok {
panic(errors.NewUnreachableError())
}

// NewStringMemoryUsage already accounts for empty string.
Expand Down
6 changes: 2 additions & 4 deletions runtime/sema/string_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ Returns a string from the given array of characters

const StringTypeJoinFunctionName = "join"
const StringTypeJoinFunctionDocString = `
Returns a string after joining the array of strings with the optional separator if provided.
Uses ',' as the default separator when not provided.
Returns a string after joining the array of strings with the provided separator.
`

// StringType represents the string type
Expand Down Expand Up @@ -327,6 +326,5 @@ var StringTypeJoinFunctionType = &FunctionType{
},
},
ReturnTypeAnnotation: NewTypeAnnotation(StringType),
// separator is optional
Arity: &Arity{Min: 1, Max: 2},
Arity: &Arity{Min: 2, Max: 2},
}
6 changes: 0 additions & 6 deletions runtime/tests/checker/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,16 +360,10 @@ func TestCheckStringJoin(t *testing.T) {
t.Parallel()

checker, err := ParseAndCheck(t, `
let s_default_sep = String.join(["👪", "❤️", "Abc"])
let s = String.join(["👪", "❤️", "Abc"], separator: "/")
`)
require.NoError(t, err)

assert.Equal(t,
sema.StringType,
RequireGlobalValue(t, checker.Elaboration, "s_default_sep"),
)

assert.Equal(t,
sema.StringType,
RequireGlobalValue(t, checker.Elaboration, "s"),
Expand Down
5 changes: 0 additions & 5 deletions runtime/tests/interpreter/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,6 @@ func TestInterpretStringJoin(t *testing.T) {
t.Parallel()

inter := parseCheckAndInterpret(t, `
fun testDefaultSeparator(): String {
return String.join(["👪", "❤️"])
}
fun test(): String {
return String.join(["👪", "❤️"], separator: "//")
}
Expand Down Expand Up @@ -499,7 +495,6 @@ func TestInterpretStringJoin(t *testing.T) {
})
}

testCase(t, "testDefaultSeparator", interpreter.NewUnmeteredStringValue("👪,❤️"))
testCase(t, "test", interpreter.NewUnmeteredStringValue("👪//❤️"))
testCase(t, "testEmptyArray", interpreter.NewUnmeteredStringValue(""))
testCase(t, "testSingletonArray", interpreter.NewUnmeteredStringValue("pqrS"))
Expand Down

0 comments on commit b92d5e1

Please sign in to comment.