fuzion icon indicating copy to clipboard operation
fuzion copied to clipboard

recursive value type not allowed when "type casting" ref type

Open maxteufel opened this issue 3 years ago • 2 comments

It would be nice to do this inside the definition of the string feature:

substring(from, to i32) string
  pre
    debug: 0 <= from <= to <= string.this.byteLength
  is
    ref : string
      redef utf8 Sequence u8 is
        string.this.utf8.slice(from, to)

However, this does not work:

$FUZION/lib/string.fz:376:3: error 1: Recursive value type is not allowed
  substring(from, to i32) string
--^
Value type string<>.substring<> equals type of outer feature.
The chain of outer types that lead to this recursion is:
1: string<>.substring<> at $FUZION/lib/string.fz:376:3:
  substring(from, to i32) string
--^
2: string<>.substring<>.@1074599969<> at $FUZION/lib/string.fz:380:5:
    ref : string
----^
3: string<>.substring<> at $FUZION/lib/string.fz:376:3:
  substring(from, to i32) string
--^

To solve this, you could add a 'ref' after the arguments list at $FUZION/lib/string.fz:376:3:
  substring(from, to i32) string
--^

one error.

The solution is currently this:

substring(from, to i32) string
  pre
    debug: 0 <= from <= to <= string.this.byteLength
is
  substring0 from to

private substring0(from, to i32) ref : string
  pre
    debug: 0 <= from <= to <= string.this.byteLength
is
  redef utf8 Sequence u8 is
    string.this.utf8.slice(from, to)

but I would prefer if the first version was possible.

maxteufel avatar Dec 12 '22 12:12 maxteufel

Here is a small example to reproduce the problem without changing the base lib:

String.mysubstring(from, to i32) String
  pre
    debug: 0 <= from <= to <= String.this.byte_length
  is
    ref : String
      redef utf8 Sequence u8 is
        String.this.utf8.slice(from, to)


say ("abCDEf".mysubstring 2 5)

Currently, fz just crashes with a stack overflow:

 > ./build/bin/fz ex_776.fz

error 1: java.lang.StackOverflowError
	at java.base/java.util.stream.StreamOpFlag.fromCharacteristics(StreamOpFlag.java:750)
	at java.base/java.util.stream.StreamSupport.stream(StreamSupport.java:70)
	at java.base/java.util.stream.Stream.of(Stream.java:1398)
	at dev.flang.air.Clazz.selfAndOuters(Clazz.java:401)
	at dev.flang.air.Clazz.selfAndOuters(Clazz.java:388)
	at dev.flang.air.Clazzes.wouldCreateCycleInOuters(Clazzes.java:408)
	at dev.flang.air.Clazzes.create(Clazzes.java:362)
	at dev.flang.air.Clazzes.create(Clazzes.java:284)
	at dev.flang.air.Clazzes.clazz(Clazzes.java:1193)
	at dev.flang.air.Clazzes.create(Clazzes.java:364)
	at dev.flang.air.Clazzes.create(Clazzes.java:284)
	at dev.flang.air.Clazzes.clazz(Clazzes.java:1193)
...

fridis avatar Mar 16 '23 15:03 fridis

updated example:

String.mysubstring(from, to i32) String
=>
  ref : String
    redef utf8 Sequence u8 => []

say ("abCDEf".mysubstring 2 5)

michaellilltokiwa avatar May 14 '24 08:05 michaellilltokiwa