Skip to content

Possible compiler issue #2672

Closed as not planned
Closed as not planned
@Moosems

Description

@Moosems

Bug description

I was trying to create a struct that can take a function as a parameter and after discussion with devs on the Discord, I believe there may be a compiler bug.

Steps to reproduce

Naive code (causes error in playground):

@always_inline("nodebug")
fn string_to_string(arg: String) -> String:
    return arg
 
struct ParseableArg[
    type: AnyType,
    type_converter: fn(arg: String) -> type = string_to_string,
    action: String = "",
    description: String = "",
    required: Bool = False,
]:
    ...
 
 
/source/prog.mojo:7:47: error: cannot implicitly convert 'fn(arg: String) -> String' value to 'fn(arg: String) -> type' in default parameter
    type_converter: fn(arg: String) -> type = string_to_string,
                                              ^~~~~~~~~~~~~~~~
/source/prog.mojo:15:5: error: use of unknown declaration 'arg1', 'fn' declarations require explicit variable declarations
    arg1 = ParseableArg[
    ^~~~
/source/prog.mojo:15:24: error: dynamic type values not permitted yet
    arg1 = ParseableArg[
           ~~~~~~~~~~~~^
mojo: error: failed to parse the provided Mojo source module

with trait:

trait FromString:
    fn __init__(inout self, s: String): ...

@always_inline("nodebug")
fn string_to_string[T: FromString](arg: String) -> T:
    return arg^ # returning T(arg) doesn’t work either


struct ParseableArg[
    type: FromString,
    type_converter: fn[type](arg: String) -> type = string_to_string,

With rebind:

type_converter: fn[rebind[type](FromString)](arg: String) -> rebind[type](FromString) = string_to_string,
 
/source/prog.mojo:11:93: error: cannot implicitly convert 'fn[FromString](arg: String) -> $0' value to 'fn[rebind[AnyRegType,AnyRegType](FromString)](arg: String) -> rebind[AnyRegType,AnyRegType](FromString)' in default parameter
    type_converter: fn[rebind[type](FromString)](arg: String) -> rebind[type](FromString) = string_to_string,
                                                                                            ^~~~~~~~~~~~~~~~
mojo: error: failed to parse the provided Mojo source module

System information

Mojo Playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingmojoIssues that are related to mojomojo-repoTag all issues with this label

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions