Draco-lang / Language-suggestions

Collecting ideas for a new .NET language that could replace C#
75 stars 5 forks source link

Add code operator #66

Open Binto86 opened 2 years ago

Binto86 commented 2 years ago

So this is crazy feature, but hear me out.

what do i mean

Basically what i would want is code operator which would take string and act like it is source code. With the code operator would the below be valid code.

code("var") i = 5; 

And it would be the same as

var i = 5; 

why?

The usecase i showed earlyer would be useles but for example this would be useful to call methods or create classes. Imagine this: You have validate method that takes object as parameter Than you have some methods like validateInt32, validateString, etc And you want to call them from the validate method. Thanks to the code operator it could look like this:

func validate(value: object): bool {
    var objType = typeof(value).ToString();
    return code("validate$objType(value as $objType)");
}

possible problems

It won't be implementable

WhiteBlackGoose commented 2 years ago

Eh. Problem number 1: such syntax means we have to ship the compiler with the app. Problem number 2: your example doesn't justify it. There's myObject is SomeType and reflection's AssignableTo method.