Git Product home page Git Product logo

Comments (8)

PoseidonEnergy avatar PoseidonEnergy commented on August 15, 2024 1

I would also like to highlight another discrepancy related to functions.

This does not compile:

var str = (() => "Hello World")(); // does not compile

However, this compiles:

var str = ((Func<string>)(() => "Hello World"))(); // compiles

from csharplang.

RikkiGibson avatar RikkiGibson commented on August 15, 2024

@cston for visibility. I think this is a subtle issue relating to lambda natural type.

from csharplang.

jaredpar avatar jaredpar commented on August 15, 2024

@cston ping for comment on whether or not this is about lambda natural type

from csharplang.

cston avatar cston commented on August 15, 2024

It looks like the best common type calculation for switch expressions currently ignores function types.

There is a similar issue with conditional expressions:

// error CS0173: Type of conditional expression cannot be determined
var func = (args.Length == 1) ? () => "Hello" : () => "World";

For switch expressions and conditional expressions, we could potentially use function types in the best common type calculation of the overall expression type.

from csharplang.

jaredpar avatar jaredpar commented on August 15, 2024

Moving to csharplang as this is a design question vs. compiler impl.

from csharplang.

CyrusNajmabadi avatar CyrusNajmabadi commented on August 15, 2024

Definitely seems odd. I can't come up with a rational as to why array BCT works differently than switch or conditional BCT vis-a-vis function-types.

from csharplang.

HaloFour avatar HaloFour commented on August 15, 2024

I would also like to highlight another discrepancy related to functions.

IIRC, the team decided against supporting IIFE in those scenarios:
https://github.com/dotnet/csharplang/blob/main/meetings/2021/LDM-2021-07-26.md#direct-invocation-of-lambdas

from csharplang.

PrinceSatish avatar PrinceSatish commented on August 15, 2024

By explicitly casting the lambdas to Func, you provide the necessary type information for the compiler to determine the return type of the switch expression, and it will compile without errors.

from csharplang.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.