Skip to content

Commit

Permalink
fix: substitute type parameters for lambda parameters (#1050)
Browse files Browse the repository at this point in the history
Closes #1047

### Summary of Changes

Type of lambda parameters can be inferred from the context. Previously,
however, type parameter types that were contained in the inferred type
were not substituted. Now, they also get inferred from the context.
  • Loading branch information
lars-reimann authored Apr 15, 2024
1 parent db0c5d7 commit 46145dd
Show file tree
Hide file tree
Showing 7 changed files with 307 additions and 145 deletions.
342 changes: 205 additions & 137 deletions packages/safe-ds-lang/src/language/typing/safe-ds-type-computer.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tests.typing.declarations.parameters.ofBlockLambdas.thatArePassedAsArguments.withTypeParameters

class MySuperclass<T> {
@Pure fun myInheritedMethod(callback: (p: T) -> ())
}

class MyClass<T>(param: T) sub MySuperclass<T> {
@Pure fun myMethod(callback: (p: T) -> ())
}

@Pure fun myFunction<T>(p: T, callback: (p: T) -> ())

segment mySegment() {
// $TEST$ serialization literal<1>
MyClass(1).myMethod((»p«) {});

// $TEST$ serialization literal<1>
MyClass(1).myInheritedMethod((»p«) {});

// $TEST$ serialization literal<1>
myFunction(1, (»p«) {});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tests.typing.declarations.parameters.ofExpressionLambdas.thatArePassedAsArguments.withTypeParameters

class MySuperclass<T> {
@Pure fun myInheritedMethod(callback: (p: T) -> ())
}

class MyClass<T>(param: T) sub MySuperclass<T> {
@Pure fun myMethod(callback: (p: T) -> ())
}

@Pure fun myFunction<T>(p: T, callback: (p: T) -> ())

segment mySegment() {
// $TEST$ serialization literal<1>
MyClass(1).myMethod((»p«) -> "");

// $TEST$ serialization literal<1>
MyClass(1).myInheritedMethod((»p«) -> "");

// $TEST$ serialization literal<1>
myFunction(1, (»p«) -> "");
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package tests.typing.expressions.blockLambdas.thatArePassedAsArguments

fun higherOrderFunction1(param: (p: String) -> (r: Int, s: String))
fun higherOrderFunction2(param: () -> ())
fun normalFunction(param: Int)
fun parameterlessFunction()
@Pure fun higherOrderFunction1(param: (p: String) -> (r: Int, s: String))
@Pure fun higherOrderFunction2(param: () -> ())
@Pure fun normalFunction(param: Int)
@Pure fun parameterlessFunction()

segment mySegment() {
// $TEST$ serialization (p: String) -> (r: literal<1>, s: literal<"">)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package tests.typing.expressions.blockLambdas.thatArePassedAsArguments.withTypeParameters

class MySuperclass<T> {
@Pure fun myInheritedMethod(callback: (p: T) -> ())
}

class MyClass<T>(param: T) sub MySuperclass<T> {
@Pure fun myMethod(callback: (p: T) -> ())
}

@Pure fun myFunction<T>(p: T, id: (p: T) -> (r: T))

segment mySegment() {
// $TEST$ serialization (p: literal<1>) -> (r: literal<1>)
MyClass(1).myMethod(»(p) {
yield r = p;
}«);

// $TEST$ serialization (p: literal<1>) -> (r: literal<1>)
MyClass(1).myInheritedMethod(»(p) {
yield r = p;
}«);

// $TEST$ serialization (p: literal<1>) -> (r: literal<1>)
myFunction(1, »(p) {
yield r = p;
}«);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package tests.typing.expressions.expressionLambdas.thatArePassedAsArguments

fun higherOrderFunction1(param: (p: String) -> (r: Int))
fun higherOrderFunction2(param: () -> ())
fun normalFunction(param: Int)
fun parameterlessFunction()
@Pure fun higherOrderFunction1(param: (p: String) -> (r: Int))
@Pure fun higherOrderFunction2(param: () -> ())
@Pure fun normalFunction(param: Int)
@Pure fun parameterlessFunction()

segment mySegment() {
// $TEST$ serialization (p: String) -> (result: literal<1>)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tests.typing.expressions.expressionLambdas.thatArePassedAsArguments.withTypeParameters

class MySuperclass<T> {
@Pure fun myInheritedMethod(callback: (p: T) -> ())
}

class MyClass<T>(param: T) sub MySuperclass<T> {
@Pure fun myMethod(callback: (p: T) -> ())
}

@Pure fun myFunction<T>(p: T, id: (p: T) -> (r: T))

segment mySegment() {
// $TEST$ serialization (p: literal<1>) -> (result: literal<1>)
MyClass(1).myMethod(»(p) -> p«);

// $TEST$ serialization (p: literal<1>) -> (result: literal<1>)
MyClass(1).myInheritedMethod(»(p) -> p«);

// $TEST$ serialization (p: literal<1>) -> (result: literal<1>)
myFunction(1, »(p) -> p«);
}

0 comments on commit 46145dd

Please sign in to comment.